-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathTab.js
369 lines (354 loc) · 9.58 KB
/
Tab.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import ButtonBase from '../ButtonBase';
import capitalize from '../utils/capitalize';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import unsupportedProp from '../utils/unsupportedProp';
import tabClasses, { getTabUtilityClass } from './tabClasses';
const useUtilityClasses = (ownerState) => {
const { classes, textColor, fullWidth, wrapped, icon, label, selected, disabled } = ownerState;
const slots = {
root: [
'root',
icon && label && 'labelIcon',
`textColor${capitalize(textColor)}`,
fullWidth && 'fullWidth',
wrapped && 'wrapped',
selected && 'selected',
disabled && 'disabled',
],
icon: ['iconWrapper', 'icon'],
};
return composeClasses(slots, getTabUtilityClass, classes);
};
const TabRoot = styled(ButtonBase, {
name: 'MuiTab',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.root,
ownerState.label && ownerState.icon && styles.labelIcon,
styles[`textColor${capitalize(ownerState.textColor)}`],
ownerState.fullWidth && styles.fullWidth,
ownerState.wrapped && styles.wrapped,
{
[`& .${tabClasses.iconWrapper}`]: styles.iconWrapper,
},
{
[`& .${tabClasses.icon}`]: styles.icon,
},
];
},
})(
memoTheme(({ theme }) => ({
...theme.typography.button,
maxWidth: 360,
minWidth: 90,
position: 'relative',
minHeight: 48,
flexShrink: 0,
padding: '12px 16px',
overflow: 'hidden',
whiteSpace: 'normal',
textAlign: 'center',
lineHeight: 1.25,
variants: [
{
props: ({ ownerState }) =>
ownerState.label &&
(ownerState.iconPosition === 'top' || ownerState.iconPosition === 'bottom'),
style: {
flexDirection: 'column',
},
},
{
props: ({ ownerState }) =>
ownerState.label &&
ownerState.iconPosition !== 'top' &&
ownerState.iconPosition !== 'bottom',
style: {
flexDirection: 'row',
},
},
{
props: ({ ownerState }) => ownerState.icon && ownerState.label,
style: {
minHeight: 72,
paddingTop: 9,
paddingBottom: 9,
},
},
{
props: ({ ownerState, iconPosition }) =>
ownerState.icon && ownerState.label && iconPosition === 'top',
style: {
[`& > .${tabClasses.icon}`]: {
marginBottom: 6,
},
},
},
{
props: ({ ownerState, iconPosition }) =>
ownerState.icon && ownerState.label && iconPosition === 'bottom',
style: {
[`& > .${tabClasses.icon}`]: {
marginTop: 6,
},
},
},
{
props: ({ ownerState, iconPosition }) =>
ownerState.icon && ownerState.label && iconPosition === 'start',
style: {
[`& > .${tabClasses.icon}`]: {
marginRight: theme.spacing(1),
},
},
},
{
props: ({ ownerState, iconPosition }) =>
ownerState.icon && ownerState.label && iconPosition === 'end',
style: {
[`& > .${tabClasses.icon}`]: {
marginLeft: theme.spacing(1),
},
},
},
{
props: {
textColor: 'inherit',
},
style: {
color: 'inherit',
opacity: 0.6, // same opacity as theme.palette.text.secondary
[`&.${tabClasses.selected}`]: {
opacity: 1,
},
[`&.${tabClasses.disabled}`]: {
opacity: (theme.vars || theme).palette.action.disabledOpacity,
},
},
},
{
props: {
textColor: 'primary',
},
style: {
color: (theme.vars || theme).palette.text.secondary,
[`&.${tabClasses.selected}`]: {
color: (theme.vars || theme).palette.primary.main,
},
[`&.${tabClasses.disabled}`]: {
color: (theme.vars || theme).palette.text.disabled,
},
},
},
{
props: {
textColor: 'secondary',
},
style: {
color: (theme.vars || theme).palette.text.secondary,
[`&.${tabClasses.selected}`]: {
color: (theme.vars || theme).palette.secondary.main,
},
[`&.${tabClasses.disabled}`]: {
color: (theme.vars || theme).palette.text.disabled,
},
},
},
{
props: ({ ownerState }) => ownerState.fullWidth,
style: {
flexShrink: 1,
flexGrow: 1,
flexBasis: 0,
maxWidth: 'none',
},
},
{
props: ({ ownerState }) => ownerState.wrapped,
style: {
fontSize: theme.typography.pxToRem(12),
},
},
],
})),
);
const Tab = React.forwardRef(function Tab(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiTab' });
const {
className,
disabled = false,
disableFocusRipple = false,
// eslint-disable-next-line react/prop-types
fullWidth,
icon: iconProp,
iconPosition = 'top',
// eslint-disable-next-line react/prop-types
indicator,
label,
onChange,
onClick,
onFocus,
// eslint-disable-next-line react/prop-types
selected,
// eslint-disable-next-line react/prop-types
selectionFollowsFocus,
// eslint-disable-next-line react/prop-types
textColor = 'inherit',
value,
wrapped = false,
...other
} = props;
const ownerState = {
...props,
disabled,
disableFocusRipple,
selected,
icon: !!iconProp,
iconPosition,
label: !!label,
fullWidth,
textColor,
wrapped,
};
const classes = useUtilityClasses(ownerState);
const icon =
iconProp && label && React.isValidElement(iconProp)
? React.cloneElement(iconProp, {
className: clsx(classes.icon, iconProp.props.className),
})
: iconProp;
const handleClick = (event) => {
if (!selected && onChange) {
onChange(event, value);
}
if (onClick) {
onClick(event);
}
};
const handleFocus = (event) => {
if (selectionFollowsFocus && !selected && onChange) {
onChange(event, value);
}
if (onFocus) {
onFocus(event);
}
};
return (
<TabRoot
focusRipple={!disableFocusRipple}
className={clsx(classes.root, className)}
ref={ref}
role="tab"
aria-selected={selected}
disabled={disabled}
onClick={handleClick}
onFocus={handleFocus}
ownerState={ownerState}
tabIndex={selected ? 0 : -1}
{...other}
>
{iconPosition === 'top' || iconPosition === 'start' ? (
<React.Fragment>
{icon}
{label}
</React.Fragment>
) : (
<React.Fragment>
{label}
{icon}
</React.Fragment>
)}
{indicator}
</TabRoot>
);
});
Tab.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* This prop isn't supported.
* Use the `component` prop if you need to change the children structure.
*/
children: unsupportedProp,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* If `true`, the keyboard focus ripple is disabled.
* @default false
*/
disableFocusRipple: PropTypes.bool,
/**
* If `true`, the ripple effect is disabled.
*
* ⚠️ Without a ripple there is no styling for :focus-visible by default. Be sure
* to highlight the element by applying separate styles with the `.Mui-focusVisible` class.
* @default false
*/
disableRipple: PropTypes.bool,
/**
* The icon to display.
*/
icon: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
/**
* The position of the icon relative to the label.
* @default 'top'
*/
iconPosition: PropTypes.oneOf(['bottom', 'end', 'start', 'top']),
/**
* The label element.
*/
label: PropTypes.node,
/**
* @ignore
*/
onChange: PropTypes.func,
/**
* @ignore
*/
onClick: PropTypes.func,
/**
* @ignore
*/
onFocus: PropTypes.func,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* You can provide your own value. Otherwise, we fallback to the child position index.
*/
value: PropTypes.any,
/**
* Tab labels appear in a single row.
* They can use a second line if needed.
* @default false
*/
wrapped: PropTypes.bool,
};
export default Tab;