-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathButton.tsx
167 lines (155 loc) · 4.4 KB
/
Button.tsx
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import * as React from 'react';
import {
Button as MuiButton,
ButtonProps as MuiButtonProps,
Tooltip,
IconButton,
useMediaQuery,
Theme,
} from '@mui/material';
import { styled } from '@mui/material/styles';
import { To } from 'history';
import PropTypes from 'prop-types';
import { useTranslate } from 'ra-core';
import { Path } from 'react-router';
/**
* A generic Button with side icon. Only the icon is displayed on small screens.
*
* The component translates the label. Pass the icon as child.
* The icon displays on the left side of the button by default. Set alignIcon prop to 'right' to inverse.
*
* @example
*
* <Button label="Edit" color="secondary" onClick={doEdit}>
* <ContentCreate />
* </Button>
*
*/
export const Button = <RootComponent extends React.ElementType = 'button'>(
props: ButtonProps<RootComponent>
) => {
const {
alignIcon = 'left',
children,
className,
disabled,
label,
color = 'primary',
size = 'small',
to: locationDescriptor,
...rest
} = props;
const translate = useTranslate();
const translatedLabel = label ? translate(label, { _: label }) : undefined;
const linkParams = getLinkParams(locationDescriptor);
const isXSmall = useMediaQuery((theme: Theme) =>
theme.breakpoints.down('sm')
);
return isXSmall ? (
label && !disabled ? (
<Tooltip title={translatedLabel}>
<IconButton
aria-label={translatedLabel}
className={className}
color={color}
size="large"
{...linkParams}
{...rest}
>
{children}
</IconButton>
</Tooltip>
) : (
<IconButton
className={className}
color={color}
disabled={disabled}
size="large"
{...linkParams}
{...rest}
>
{children}
</IconButton>
)
) : (
<StyledButton
className={className}
color={color}
size={size}
aria-label={translatedLabel}
disabled={disabled}
startIcon={alignIcon === 'left' && children ? children : undefined}
endIcon={alignIcon === 'right' && children ? children : undefined}
{...linkParams}
{...rest}
>
{translatedLabel}
</StyledButton>
);
};
interface Props<RootComponent extends React.ElementType> {
alignIcon?: 'left' | 'right';
children?: React.ReactElement;
className?: string;
component?: RootComponent;
to?: LocationDescriptor | To;
disabled?: boolean;
label?: string;
size?: 'small' | 'medium' | 'large';
variant?: string;
}
export type ButtonProps<
RootComponent extends React.ElementType = 'button'
> = Props<RootComponent> & MuiButtonProps<RootComponent>;
Button.propTypes = {
alignIcon: PropTypes.oneOf(['left', 'right']),
children: PropTypes.element,
className: PropTypes.string,
color: PropTypes.oneOfType([
PropTypes.oneOf([
'inherit',
'default',
'primary',
'secondary',
'error',
'info',
'success',
'warning',
]),
PropTypes.string,
]),
disabled: PropTypes.bool,
label: PropTypes.string,
size: PropTypes.oneOf(['small', 'medium', 'large']),
};
const PREFIX = 'RaButton';
const StyledButton = styled(MuiButton, {
name: PREFIX,
overridesResolver: (props, styles) => styles.root,
})({
'&.MuiButton-sizeSmall': {
// fix for icon misalignment on small buttons, see https://github.com/mui/material-ui/pull/30240
lineHeight: 1.5,
},
});
const getLinkParams = (locationDescriptor?: LocationDescriptor | string) => {
// eslint-disable-next-line eqeqeq
if (locationDescriptor == undefined) {
return undefined;
}
if (typeof locationDescriptor === 'string') {
return { to: locationDescriptor };
}
const { redirect, replace, state, ...to } = locationDescriptor;
return {
to,
redirect,
replace,
state,
};
};
export type LocationDescriptor = Partial<Path> & {
redirect?: boolean;
state?: any;
replace?: boolean;
};