forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chip.js
588 lines (561 loc) · 18.7 KB
/
Chip.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { unstable_composeClasses as composeClasses } from '@mui/base';
import { alpha } from '@mui/system';
import CancelIcon from '../internal/svg-icons/Cancel';
import useForkRef from '../utils/useForkRef';
import unsupportedProp from '../utils/unsupportedProp';
import capitalize from '../utils/capitalize';
import ButtonBase from '../ButtonBase';
import useThemeProps from '../styles/useThemeProps';
import styled from '../styles/styled';
import chipClasses, { getChipUtilityClass } from './chipClasses';
const useUtilityClasses = (ownerState) => {
const { classes, disabled, size, color, iconColor, onDelete, clickable, variant } = ownerState;
const slots = {
root: [
'root',
variant,
disabled && 'disabled',
`size${capitalize(size)}`,
`color${capitalize(color)}`,
clickable && 'clickable',
clickable && `clickableColor${capitalize(color)}`,
onDelete && 'deletable',
onDelete && `deletableColor${capitalize(color)}`,
`${variant}${capitalize(color)}`,
],
label: ['label', `label${capitalize(size)}`],
avatar: ['avatar', `avatar${capitalize(size)}`, `avatarColor${capitalize(color)}`],
icon: ['icon', `icon${capitalize(size)}`, `iconColor${capitalize(iconColor)}`],
deleteIcon: [
'deleteIcon',
`deleteIcon${capitalize(size)}`,
`deleteIconColor${capitalize(color)}`,
`deleteIcon${capitalize(variant)}Color${capitalize(color)}`,
],
};
return composeClasses(slots, getChipUtilityClass, classes);
};
const ChipRoot = styled('div', {
name: 'MuiChip',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
const { color, iconColor, clickable, onDelete, size, variant } = ownerState;
return [
{ [`& .${chipClasses.avatar}`]: styles.avatar },
{ [`& .${chipClasses.avatar}`]: styles[`avatar${capitalize(size)}`] },
{ [`& .${chipClasses.avatar}`]: styles[`avatarColor${capitalize(color)}`] },
{ [`& .${chipClasses.icon}`]: styles.icon },
{ [`& .${chipClasses.icon}`]: styles[`icon${capitalize(size)}`] },
{ [`& .${chipClasses.icon}`]: styles[`iconColor${capitalize(iconColor)}`] },
{ [`& .${chipClasses.deleteIcon}`]: styles.deleteIcon },
{ [`& .${chipClasses.deleteIcon}`]: styles[`deleteIcon${capitalize(size)}`] },
{ [`& .${chipClasses.deleteIcon}`]: styles[`deleteIconColor${capitalize(color)}`] },
{
[`& .${chipClasses.deleteIcon}`]:
styles[`deleteIcon${capitalize(variant)}Color${capitalize(color)}`],
},
styles.root,
styles[`size${capitalize(size)}`],
styles[`color${capitalize(color)}`],
clickable && styles.clickable,
clickable && color !== 'default' && styles[`clickableColor${capitalize(color)})`],
onDelete && styles.deletable,
onDelete && color !== 'default' && styles[`deletableColor${capitalize(color)}`],
styles[variant],
styles[`${variant}${capitalize(color)}`],
];
},
})(
({ theme, ownerState }) => {
const deleteIconColor = alpha(theme.palette.text.primary, 0.26);
const textColor =
theme.palette.mode === 'light' ? theme.palette.grey[700] : theme.palette.grey[300];
return {
maxWidth: '100%',
fontFamily: theme.typography.fontFamily,
fontSize: theme.typography.pxToRem(13),
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
height: 32,
color: (theme.vars || theme).palette.text.primary,
backgroundColor: (theme.vars || theme).palette.action.selected,
borderRadius: 32 / 2,
whiteSpace: 'nowrap',
transition: theme.transitions.create(['background-color', 'box-shadow']),
// label will inherit this from root, then `clickable` class overrides this for both
cursor: 'default',
// We disable the focus ring for mouse, touch and keyboard users.
outline: 0,
textDecoration: 'none',
border: 0, // Remove `button` border
padding: 0, // Remove `button` padding
verticalAlign: 'middle',
boxSizing: 'border-box',
[`&.${chipClasses.disabled}`]: {
opacity: (theme.vars || theme).palette.action.disabledOpacity,
pointerEvents: 'none',
},
[`& .${chipClasses.avatar}`]: {
marginLeft: 5,
marginRight: -6,
width: 24,
height: 24,
color: theme.vars ? theme.vars.palette.Chip.defaultAvatarColor : textColor,
fontSize: theme.typography.pxToRem(12),
},
[`& .${chipClasses.avatarColorPrimary}`]: {
color: (theme.vars || theme).palette.primary.contrastText,
backgroundColor: (theme.vars || theme).palette.primary.dark,
},
[`& .${chipClasses.avatarColorSecondary}`]: {
color: (theme.vars || theme).palette.secondary.contrastText,
backgroundColor: (theme.vars || theme).palette.secondary.dark,
},
[`& .${chipClasses.avatarSmall}`]: {
marginLeft: 4,
marginRight: -4,
width: 18,
height: 18,
fontSize: theme.typography.pxToRem(10),
},
[`& .${chipClasses.icon}`]: {
marginLeft: 5,
marginRight: -6,
...(ownerState.size === 'small' && {
fontSize: 18,
marginLeft: 4,
marginRight: -4,
}),
...(ownerState.iconColor === ownerState.color && {
color: theme.vars ? theme.vars.palette.Chip.defaultIconColor : textColor,
...(ownerState.color !== 'default' && {
color: 'inherit',
}),
}),
},
[`& .${chipClasses.deleteIcon}`]: {
WebkitTapHighlightColor: 'transparent',
color: theme.vars
? `rgba(${theme.vars.palette.text.primaryChannel} / 0.26)`
: deleteIconColor,
fontSize: 22,
cursor: 'pointer',
margin: '0 5px 0 -6px',
'&:hover': {
color: theme.vars
? `rgba(${theme.vars.palette.text.primaryChannel} / 0.4)`
: alpha(deleteIconColor, 0.4),
},
...(ownerState.size === 'small' && {
fontSize: 16,
marginRight: 4,
marginLeft: -4,
}),
...(ownerState.color !== 'default' && {
color: theme.vars
? `rgba(${theme.vars.palette[ownerState.color].contrastTextChannel} / 0.7)`
: alpha(theme.palette[ownerState.color].contrastText, 0.7),
'&:hover, &:active': {
color: (theme.vars || theme).palette[ownerState.color].contrastText,
},
}),
},
...(ownerState.size === 'small' && {
height: 24,
}),
...(ownerState.color !== 'default' && {
backgroundColor: (theme.vars || theme).palette[ownerState.color].main,
color: (theme.vars || theme).palette[ownerState.color].contrastText,
}),
...(ownerState.onDelete && {
[`&.${chipClasses.focusVisible}`]: {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette.action.selectedChannel} / calc(${
theme.vars.palette.action.selectedOpacity + theme.vars.palette.action.focusOpacity
}))`
: alpha(
theme.palette.action.selected,
theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity,
),
},
}),
...(ownerState.onDelete &&
ownerState.color !== 'default' && {
[`&.${chipClasses.focusVisible}`]: {
backgroundColor: (theme.vars || theme).palette[ownerState.color].dark,
},
}),
};
},
({ theme, ownerState }) => ({
...(ownerState.clickable && {
userSelect: 'none',
WebkitTapHighlightColor: 'transparent',
cursor: 'pointer',
'&:hover': {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette.action.selectedChannel} / calc(${
theme.vars.palette.action.selectedOpacity + theme.vars.palette.action.hoverOpacity
}))`
: alpha(
theme.palette.action.selected,
theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity,
),
},
[`&.${chipClasses.focusVisible}`]: {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette.action.selectedChannel} / calc(${
theme.vars.palette.action.selectedOpacity + theme.vars.palette.action.focusOpacity
}))`
: alpha(
theme.palette.action.selected,
theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity,
),
},
'&:active': {
boxShadow: (theme.vars || theme).shadows[1],
},
}),
...(ownerState.clickable &&
ownerState.color !== 'default' && {
[`&:hover, &.${chipClasses.focusVisible}`]: {
backgroundColor: (theme.vars || theme).palette[ownerState.color].dark,
},
}),
}),
({ theme, ownerState }) => ({
...(ownerState.variant === 'outlined' && {
backgroundColor: 'transparent',
border: theme.vars
? `1px solid ${theme.vars.palette.Chip.defaultBorder}`
: `1px solid ${
theme.palette.mode === 'light' ? theme.palette.grey[400] : theme.palette.grey[700]
}`,
[`&.${chipClasses.clickable}:hover`]: {
backgroundColor: (theme.vars || theme).palette.action.hover,
},
[`&.${chipClasses.focusVisible}`]: {
backgroundColor: (theme.vars || theme).palette.action.focus,
},
[`& .${chipClasses.avatar}`]: {
marginLeft: 4,
},
[`& .${chipClasses.avatarSmall}`]: {
marginLeft: 2,
},
[`& .${chipClasses.icon}`]: {
marginLeft: 4,
},
[`& .${chipClasses.iconSmall}`]: {
marginLeft: 2,
},
[`& .${chipClasses.deleteIcon}`]: {
marginRight: 5,
},
[`& .${chipClasses.deleteIconSmall}`]: {
marginRight: 3,
},
}),
...(ownerState.variant === 'outlined' &&
ownerState.color !== 'default' && {
color: (theme.vars || theme).palette[ownerState.color].main,
border: `1px solid ${
theme.vars
? `rgba(${theme.vars.palette[ownerState.color].mainChannel} / 0.7)`
: alpha(theme.palette[ownerState.color].main, 0.7)
}`,
[`&.${chipClasses.clickable}:hover`]: {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette[ownerState.color].mainChannel} / ${
theme.vars.palette.action.hoverOpacity
})`
: alpha(theme.palette[ownerState.color].main, theme.palette.action.hoverOpacity),
},
[`&.${chipClasses.focusVisible}`]: {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette[ownerState.color].mainChannel} / ${
theme.vars.palette.action.focusOpacity
})`
: alpha(theme.palette[ownerState.color].main, theme.palette.action.focusOpacity),
},
[`& .${chipClasses.deleteIcon}`]: {
color: theme.vars
? `rgba(${theme.vars.palette[ownerState.color].mainChannel} / 0.7)`
: alpha(theme.palette[ownerState.color].main, 0.7),
'&:hover, &:active': {
color: (theme.vars || theme).palette[ownerState.color].main,
},
},
}),
}),
);
const ChipLabel = styled('span', {
name: 'MuiChip',
slot: 'Label',
overridesResolver: (props, styles) => {
const { ownerState } = props;
const { size } = ownerState;
return [styles.label, styles[`label${capitalize(size)}`]];
},
})(({ ownerState }) => ({
overflow: 'hidden',
textOverflow: 'ellipsis',
paddingLeft: 12,
paddingRight: 12,
whiteSpace: 'nowrap',
...(ownerState.size === 'small' && {
paddingLeft: 8,
paddingRight: 8,
}),
}));
function isDeleteKeyboardEvent(keyboardEvent) {
return keyboardEvent.key === 'Backspace' || keyboardEvent.key === 'Delete';
}
/**
* Chips represent complex entities in small blocks, such as a contact.
*/
const Chip = React.forwardRef(function Chip(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiChip' });
const {
avatar: avatarProp,
className,
clickable: clickableProp,
color = 'default',
component: ComponentProp,
deleteIcon: deleteIconProp,
disabled = false,
icon: iconProp,
label,
onClick,
onDelete,
onKeyDown,
onKeyUp,
size = 'medium',
variant = 'filled',
tabIndex,
...other
} = props;
const chipRef = React.useRef(null);
const handleRef = useForkRef(chipRef, ref);
const handleDeleteIconClick = (event) => {
// Stop the event from bubbling up to the `Chip`
event.stopPropagation();
if (onDelete) {
onDelete(event);
}
};
const handleKeyDown = (event) => {
// Ignore events from children of `Chip`.
if (event.currentTarget === event.target && isDeleteKeyboardEvent(event)) {
// Will be handled in keyUp, otherwise some browsers
// might init navigation
event.preventDefault();
}
if (onKeyDown) {
onKeyDown(event);
}
};
const handleKeyUp = (event) => {
// Ignore events from children of `Chip`.
if (event.currentTarget === event.target) {
if (onDelete && isDeleteKeyboardEvent(event)) {
onDelete(event);
} else if (event.key === 'Escape' && chipRef.current) {
chipRef.current.blur();
}
}
if (onKeyUp) {
onKeyUp(event);
}
};
const clickable = clickableProp !== false && onClick ? true : clickableProp;
const component = clickable || onDelete ? ButtonBase : ComponentProp || 'div';
const ownerState = {
...props,
component,
disabled,
size,
color,
iconColor: React.isValidElement(iconProp) ? iconProp.props.color || color : color,
onDelete: !!onDelete,
clickable,
variant,
};
const classes = useUtilityClasses(ownerState);
const moreProps =
component === ButtonBase
? {
component: ComponentProp || 'div',
focusVisibleClassName: classes.focusVisible,
...(onDelete && { disableRipple: true }),
}
: {};
let deleteIcon = null;
if (onDelete) {
deleteIcon =
deleteIconProp && React.isValidElement(deleteIconProp) ? (
React.cloneElement(deleteIconProp, {
className: clsx(deleteIconProp.props.className, classes.deleteIcon),
onClick: handleDeleteIconClick,
})
) : (
<CancelIcon className={clsx(classes.deleteIcon)} onClick={handleDeleteIconClick} />
);
}
let avatar = null;
if (avatarProp && React.isValidElement(avatarProp)) {
avatar = React.cloneElement(avatarProp, {
className: clsx(classes.avatar, avatarProp.props.className),
});
}
let icon = null;
if (iconProp && React.isValidElement(iconProp)) {
icon = React.cloneElement(iconProp, {
className: clsx(classes.icon, iconProp.props.className),
});
}
if (process.env.NODE_ENV !== 'production') {
if (avatar && icon) {
console.error(
'MUI: The Chip component can not handle the avatar ' +
'and the icon prop at the same time. Pick one.',
);
}
}
return (
<ChipRoot
as={component}
className={clsx(classes.root, className)}
disabled={clickable && disabled ? true : undefined}
onClick={onClick}
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
ref={handleRef}
tabIndex={disabled ? tabIndex ?? -1 : tabIndex}
ownerState={ownerState}
{...moreProps}
{...other}
>
{avatar || icon}
<ChipLabel className={clsx(classes.label)} ownerState={ownerState}>
{label}
</ChipLabel>
{deleteIcon}
</ChipRoot>
);
});
Chip.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The Avatar element to display.
*/
avatar: PropTypes.element,
/**
* 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 chip will appear clickable, and will raise when pressed,
* even if the onClick prop is not defined.
* If `false`, the chip will not appear clickable, even if onClick prop is defined.
* This can be used, for example,
* along with the component prop to indicate an anchor Chip is clickable.
* Note: this controls the UI and does not affect the onClick event.
*/
clickable: PropTypes.bool,
/**
* The color of the component.
* It supports both default and custom theme colors, which can be added as shown in the
* [palette customization guide](https://mui.com/material-ui/customization/palette/#adding-new-colors).
* @default 'default'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['default', 'primary', 'secondary', 'error', 'info', 'success', 'warning']),
PropTypes.string,
]),
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* Override the default delete icon element. Shown only if `onDelete` is set.
*/
deleteIcon: PropTypes.element,
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* Icon element.
*/
icon: PropTypes.element,
/**
* The content of the component.
*/
label: PropTypes.node,
/**
* @ignore
*/
onClick: PropTypes.func,
/**
* Callback fired when the delete icon is clicked.
* If set, the delete icon will be shown.
*/
onDelete: PropTypes.func,
/**
* @ignore
*/
onKeyDown: PropTypes.func,
/**
* @ignore
*/
onKeyUp: PropTypes.func,
/**
* The size of the component.
* @default 'medium'
*/
size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['medium', 'small']),
PropTypes.string,
]),
/**
* 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,
]),
/**
* tab order of an element
*/
tabIndex: PropTypes.number,
/**
* The variant to use.
* @default 'filled'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['filled', 'outlined']),
PropTypes.string,
]),
};
export default Chip;