Skip to content

Commit 3c9fe4f

Browse files
committed
add Icon component and use it in Button, IconButton and Tabs
1 parent 9be5d04 commit 3c9fe4f

5 files changed

Lines changed: 28 additions & 8 deletions

File tree

chartlets.js/packages/lib/src/plugins/mui/Button.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { type MouseEvent } from "react";
22
import MuiButton from "@mui/material/Button";
3-
import MuiIcon from "@mui/material/Icon";
43

54
import type { ComponentProps, ComponentState } from "@/index";
65
import { Tooltip } from "./Tooltip";
6+
import { Icon } from "./Icon";
77

88
interface ButtonState extends ComponentState {
99
text?: string;
@@ -55,8 +55,8 @@ export function Button({
5555
variant={variant}
5656
color={color}
5757
disabled={disabled}
58-
startIcon={startIcon && <MuiIcon>{startIcon}</MuiIcon>}
59-
endIcon={endIcon && <MuiIcon>{endIcon}</MuiIcon>}
58+
startIcon={startIcon && <Icon iconName={startIcon}></Icon>}
59+
endIcon={endIcon && <Icon iconName={endIcon}></Icon>}
6060
onClick={handleClick}
6161
>
6262
{text}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as MuiIcons from "@mui/icons-material";
2+
3+
interface IconProps {
4+
iconName?: string;
5+
}
6+
7+
export const Icon = ({ iconName }: IconProps) => {
8+
if (!iconName) return null;
9+
10+
const IconComponent = (MuiIcons as Record<string, React.ElementType>)[
11+
iconName
12+
];
13+
14+
if (!IconComponent) {
15+
console.warn(`Icon "${iconName}" not found in @mui/icons-material`);
16+
return null;
17+
}
18+
19+
return <IconComponent />;
20+
};

chartlets.js/packages/lib/src/plugins/mui/IconButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { type MouseEvent } from "react";
22
import MuiIconButton from "@mui/material/IconButton";
3-
import MuiIcon from "@mui/material/Icon";
43

54
import type { ComponentState, ComponentProps } from "@/index";
5+
import { Icon } from "./Icon";
66
import { Tooltip } from "./Tooltip";
77

88
interface IconButtonState extends ComponentState {
@@ -53,7 +53,7 @@ export function IconButton({
5353
disabled={disabled}
5454
onClick={handleClick}
5555
>
56-
<MuiIcon>{icon}</MuiIcon>
56+
<Icon iconName={icon}></Icon>
5757
</MuiIconButton>
5858
</Tooltip>
5959
);

chartlets.js/packages/lib/src/plugins/mui/Tabs.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import MuiBox from "@mui/material/Box";
2-
import MuiIcon from "@mui/material/Icon";
32
import MuiTabs from "@mui/material/Tabs";
43
import MuiTab from "@mui/material/Tab";
54

65
import type { ComponentProps, ComponentState } from "@/index";
76
import type { SyntheticEvent } from "react";
87
import { Box } from "@/plugins/mui/Box";
8+
import { Icon } from "./Icon";
99
import { isString } from "@/utils/isString";
1010
import { isComponentState } from "@/types/state/component";
1111

@@ -57,7 +57,7 @@ export function Tabs({
5757
label={tabState ? tabState.label : isString(tab) ? tab : ""}
5858
icon={
5959
tabState &&
60-
tabState.icon && <MuiIcon>{tabState.icon}</MuiIcon>
60+
tabState.icon && <Icon iconName={tabState.icon}></Icon>
6161
}
6262
disabled={disabled || (tabState && tabState.disabled)}
6363
/>

chartlets.py/demo/my_extension/my_panel_5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
def render_panel(
1313
ctx: Context,
1414
) -> Component:
15-
open_button = Button(id="open_button", text="Open Dialog")
15+
open_button = Button(id="open_button", text="Open Dialog", startIcon="ChatBubble")
1616
okay_button = Button(id="okay_button", text="Okay")
1717
not_okay_button = Button(id="not_okay_button", text="Not okay")
1818
dialog = Dialog(

0 commit comments

Comments
 (0)