forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChipDelete.tsx
203 lines (187 loc) · 6.31 KB
/
ChipDelete.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
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
import * as React from 'react';
import PropTypes from 'prop-types';
import { OverridableComponent } from '@mui/types';
import { unstable_capitalize as capitalize, unstable_useForkRef as useForkRef } from '@mui/utils';
import { unstable_composeClasses as composeClasses, useButton } from '@mui/base';
import { useSlotProps } from '@mui/base/utils';
import { useThemeProps } from '../styles';
import styled from '../styles/styled';
import Cancel from '../internal/svg-icons/Cancel';
import chipDeleteClasses, { getChipDeleteUtilityClass } from './chipDeleteClasses';
import { ChipDeleteProps, ChipDeleteOwnerState, ChipDeleteTypeMap } from './ChipDeleteProps';
import ChipContext from '../Chip/ChipContext';
const useUtilityClasses = (ownerState: ChipDeleteOwnerState) => {
const { focusVisible, variant, color, disabled } = ownerState;
const slots = {
root: [
'root',
disabled && 'disabled',
focusVisible && 'focusVisible',
variant && `variant${capitalize(variant)}`,
color && `color${capitalize(color)}`,
],
};
return composeClasses(slots, getChipDeleteUtilityClass, {});
};
const ChipDeleteRoot = styled('button', {
name: 'JoyChipDelete',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})<{ ownerState: ChipDeleteOwnerState }>(({ theme, ownerState }) => [
{
'--Icon-margin': 'initial', // prevent overrides from parent
pointerEvents: 'visible', // force the ChipDelete to be hoverable because the decorator can have pointerEvents 'none'
width: 'var(--Chip-delete-size, 2rem)',
height: 'var(--Chip-delete-size, 2rem)',
borderRadius: 'var(--Chip-delete-radius, 50%)',
margin: 'var(--Chip-delete-margin)',
display: 'inline-flex',
justifyContent: 'center',
alignItems: 'center',
zIndex: 1, // overflow above sibling button or anchor
border: 'none', // reset user agent stylesheet
background: 'none', // reset user agent stylesheet
padding: '0px', // reset user agent stylesheet
[theme.focus.selector]: theme.focus.default,
},
theme.variants[ownerState.variant!]?.[ownerState.color!],
{ '&:hover': theme.variants[`${ownerState.variant!}Hover`]?.[ownerState.color!] },
{ '&:active': theme.variants[`${ownerState.variant!}Active`]?.[ownerState.color!] },
{
[`&.${chipDeleteClasses.disabled}`]:
theme.variants[`${ownerState.variant!}Disabled`]?.[ownerState.color!],
},
]);
const chipVariantMapping = {
plain: 'outlined',
outlined: 'soft',
soft: 'solid',
solid: 'solid',
} as const;
const ChipDelete = React.forwardRef(function ChipDelete(inProps, ref) {
const props = useThemeProps<typeof inProps & ChipDeleteProps>({
props: inProps,
name: 'JoyChipDelete',
});
const {
component,
children,
variant: variantProp,
color: colorProp,
disabled: disabledProp,
onKeyDown,
onDelete,
onClick,
...other
} = props;
const chipContext = React.useContext(ChipContext);
const color = colorProp || chipContext.color || 'primary';
const variant = variantProp || chipVariantMapping[chipContext.variant!] || 'solid';
const disabled = disabledProp ?? chipContext.disabled;
const buttonRef = React.useRef<HTMLElement | null>(null);
const handleRef = useForkRef(buttonRef, ref);
const { focusVisible, getRootProps } = useButton({
...props,
disabled,
ref: handleRef,
});
const ownerState = {
...props,
disabled,
variant,
color,
focusVisible,
};
const classes = useUtilityClasses(ownerState);
const handleClickDelete = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
if (!disabled && onDelete) {
onDelete(event);
}
if (onClick) {
onClick(event);
}
};
const handleKeyDelete = (event: React.KeyboardEvent<HTMLButtonElement>) => {
event.preventDefault();
if (['Backspace', 'Enter', 'Delete'].includes(event.key)) {
if (!disabled && onDelete) {
onDelete(event);
}
}
if (onKeyDown) {
onKeyDown(event);
}
};
const rootProps = useSlotProps({
elementType: ChipDeleteRoot,
getSlotProps: getRootProps,
externalSlotProps: {},
externalForwardedProps: other,
ownerState,
additionalProps: {
as: component,
onKeyDown: handleKeyDelete,
onClick: handleClickDelete,
},
className: classes.root,
});
const { onDelete: excludeOnDelete, ...restOfRootProps } = rootProps;
return <ChipDeleteRoot {...restOfRootProps}>{children ?? <Cancel />}</ChipDeleteRoot>;
}) as OverridableComponent<ChipDeleteTypeMap>;
ChipDelete.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* If provided, it will replace the default icon.
*/
children: PropTypes.node,
/**
* The color of the component. It supports those theme colors that make sense for this component.
* @default 'primary'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['danger', 'info', 'neutral', 'primary', '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,
/**
* If `true`, the component is disabled.
* If `undefined`, the value inherits from the parent chip via a React context.
*/
disabled: PropTypes.bool,
/**
* @ignore
*/
onClick: PropTypes.func,
/**
* Callback fired when component is not disabled and `Backspace`, `Enter` or `Delete` is pressed or when chip is clicked.
*/
onDelete: PropTypes.func,
/**
* @ignore
*/
onKeyDown: 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,
]),
/**
* The variant to use.
* @default 'solid'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
PropTypes.string,
]),
} as any;
export default ChipDelete;