-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat/redesign-synthesis
- Loading branch information
Showing
8 changed files
with
141 additions
and
60 deletions.
There are no files selected for viewing
74 changes: 63 additions & 11 deletions
74
packages/client/src/modules/common/components/Button/Button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,95 @@ | ||
import { Button as ButtonLib, CircularProgress } from "@mui/material"; | ||
import { Button as ButtonLib, CircularProgress, useTheme } from "@mui/material"; | ||
import { useMemo } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { Icon, IconName } from "../Icon"; | ||
import { Typography } from "../Typography"; | ||
|
||
export { Button }; | ||
|
||
interface ButtonProps { | ||
type?: "primary" | "secondary"; | ||
htmlType?: "button" | "submit"; | ||
disabled?: boolean; | ||
loading?: boolean; | ||
onClick: () => void | Promise<void>; | ||
to?: string; | ||
iconName?: IconName; | ||
onClick?: () => void | Promise<void>; | ||
children: React.ReactNode; | ||
} | ||
|
||
function Button({ | ||
type = "primary", | ||
htmlType = "button", | ||
disabled = false, | ||
loading = false, | ||
onClick, | ||
to, | ||
iconName, | ||
onClick: onClickProp, | ||
children, | ||
}: ButtonProps) { | ||
const navigate = useNavigate(); | ||
const theme = useTheme(); | ||
|
||
const buttonProps = useMemo(() => { | ||
if (disabled) { | ||
return { | ||
props: { | ||
variant: "contained", | ||
}, | ||
sx: { | ||
backgroundColor: `${theme.palette.components.button.disabled.backgroundColor} !important`, | ||
color: `${theme.palette.components.button.disabled.color} !important`, | ||
}, | ||
}; | ||
} | ||
if (type === "primary") { | ||
return { | ||
color: "secondary", | ||
variant: "contained", | ||
props: { | ||
variant: "contained", | ||
color: "secondary", | ||
}, | ||
}; | ||
} | ||
return { | ||
color: "primary", | ||
props: { | ||
color: "primary", | ||
}, | ||
}; | ||
}, [type]); | ||
}, [disabled, type]); | ||
const loaderColor = useMemo( | ||
() => (type === "primary" ? "primary" : "secondary"), | ||
[type] | ||
); | ||
|
||
const onClick = useMemo(() => { | ||
if (to) { | ||
return () => navigate(to); | ||
} | ||
return onClickProp; | ||
}, [to, navigate, onClickProp]); | ||
|
||
return ( | ||
<ButtonLib {...(buttonProps as any)} disabled={loading} onClick={onClick}> | ||
{loading && ( | ||
<CircularProgress color={loaderColor} size={16} sx={{ mr: 1 }} /> | ||
<ButtonLib | ||
type={htmlType} | ||
{...((buttonProps.props || {}) as any)} | ||
disabled={loading || disabled} | ||
onClick={onClick} | ||
sx={{ | ||
...(buttonProps.sx || {}), | ||
display: "flex", | ||
gap: 1, | ||
}} | ||
> | ||
{loading && <CircularProgress color={loaderColor} size={16} />} | ||
{iconName && !loading && ( | ||
<Icon | ||
name={iconName} | ||
sx={{ width: "1rem", height: "1rem", color: buttonProps.sx?.color }} | ||
/> | ||
)} | ||
{children} | ||
<Typography as="span" sx={{ color: buttonProps.sx?.color }}> | ||
{children} | ||
</Typography> | ||
</ButtonLib> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.