-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathButton.tsx
More file actions
113 lines (99 loc) · 3.08 KB
/
Copy pathButton.tsx
File metadata and controls
113 lines (99 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { alpha, Button as BaseButton, ButtonProps as BaseButtonProps } from '@mui/material';
import React, { SyntheticEvent } from 'react';
import { useNavigate } from 'react-router-dom';
import styled from 'styled-components';
const StyledBaseButton = styled(({ nowrap: boolean, selected, ...rest }) => <BaseButton {...rest} />)`
white-space: ${({ nowrap }) => (nowrap ? 'nowrap' : 'normal')};
${({ selected, theme }) => {
if (!selected) {
return '';
}
const isDark = theme.palette.mode === 'dark';
const primary = theme.palette.primary.main;
const text = theme.palette.text.primary;
return `
background-color: ${alpha(primary, isDark ? 0.24 : 0.1)};
border-color: ${alpha(primary, isDark ? 0.55 : 0.4)} !important;
color: ${text} !important;
.MuiSvgIcon-root {
color: ${text} !important;
}
`;
}}
`;
function getColor(theme, variant) {
switch (variant) {
case 'contained':
return theme.palette.danger.contrastText;
default:
return theme.palette.danger.main;
}
}
const DangerButton = styled(StyledBaseButton)`
color: ${({ theme, variant }) => getColor(theme, variant)};
${({ theme, variant }) => (variant === 'contained' ? `background-color: ${theme.palette.danger.main};` : undefined)}
&:hover {
color: ${({ theme, variant }) => getColor(theme, variant)};
${({ theme, variant }) => (variant === 'contained' ? `background-color: ${theme.palette.danger.main};` : undefined)}
}
`;
export type ButtonProps = Omit<BaseButtonProps, 'color'> & {
color?: BaseButtonProps['color'] | 'danger';
to?: string | Object;
nowrap?: boolean;
selected?: boolean;
};
export default function Button(props: ButtonProps) {
const { color = 'secondary', to, onClick, disableElevation = true, ...rest } = props;
const navigate = useNavigate();
const handleClick = React.useCallback(
(...args: any[]) => {
if (to) {
navigate(to);
}
if (onClick) {
onClick(...args);
}
},
[to, navigate, onClick],
);
const onAuxClick = React.useMemo(() => {
if (!rest.href) {
return undefined;
}
return (event: SyntheticEvent, ...restArgs: any[]) => {
event.preventDefault();
handleClick(...restArgs);
};
}, [rest.href, handleClick]);
switch (color) {
case 'danger':
return (
<DangerButton {...rest} onClick={handleClick} onAuxClick={onAuxClick} disableElevation={disableElevation} />
);
case 'primary':
return (
<StyledBaseButton
{...rest}
onClick={handleClick}
onAuxClick={onAuxClick}
disableElevation={disableElevation}
color="primary"
/>
);
case 'secondary':
return (
<StyledBaseButton
{...rest}
onClick={handleClick}
onAuxClick={onAuxClick}
disableElevation={disableElevation}
color="secondary"
/>
);
default:
return (
<StyledBaseButton {...rest} onClick={handleClick} onAuxClick={onAuxClick} disableElevation={disableElevation} />
);
}
}