-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
/
ListItem.js
420 lines (393 loc) · 12.6 KB
/
ListItem.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
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import elementTypeAcceptingRef from '@mui/utils/elementTypeAcceptingRef';
import chainPropTypes from '@mui/utils/chainPropTypes';
import isHostComponent from '../utils/isHostComponent';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import isMuiElement from '../utils/isMuiElement';
import useForkRef from '../utils/useForkRef';
import ListContext from '../List/ListContext';
import { getListItemUtilityClass } from './listItemClasses';
import { listItemButtonClasses } from '../ListItemButton';
import ListItemSecondaryAction from '../ListItemSecondaryAction';
export const overridesResolver = (props, styles) => {
const { ownerState } = props;
return [
styles.root,
ownerState.dense && styles.dense,
ownerState.alignItems === 'flex-start' && styles.alignItemsFlexStart,
ownerState.divider && styles.divider,
!ownerState.disableGutters && styles.gutters,
!ownerState.disablePadding && styles.padding,
ownerState.hasSecondaryAction && styles.secondaryAction,
];
};
const useUtilityClasses = (ownerState) => {
const {
alignItems,
classes,
dense,
disableGutters,
disablePadding,
divider,
hasSecondaryAction,
} = ownerState;
const slots = {
root: [
'root',
dense && 'dense',
!disableGutters && 'gutters',
!disablePadding && 'padding',
divider && 'divider',
alignItems === 'flex-start' && 'alignItemsFlexStart',
hasSecondaryAction && 'secondaryAction',
],
container: ['container'],
};
return composeClasses(slots, getListItemUtilityClass, classes);
};
export const ListItemRoot = styled('div', {
name: 'MuiListItem',
slot: 'Root',
overridesResolver,
})(
memoTheme(({ theme }) => ({
display: 'flex',
justifyContent: 'flex-start',
alignItems: 'center',
position: 'relative',
textDecoration: 'none',
width: '100%',
boxSizing: 'border-box',
textAlign: 'left',
variants: [
{
props: ({ ownerState }) => !ownerState.disablePadding,
style: {
paddingTop: 8,
paddingBottom: 8,
},
},
{
props: ({ ownerState }) => !ownerState.disablePadding && ownerState.dense,
style: {
paddingTop: 4,
paddingBottom: 4,
},
},
{
props: ({ ownerState }) => !ownerState.disablePadding && !ownerState.disableGutters,
style: {
paddingLeft: 16,
paddingRight: 16,
},
},
{
props: ({ ownerState }) => !ownerState.disablePadding && !!ownerState.secondaryAction,
style: {
// Add some space to avoid collision as `ListItemSecondaryAction`
// is absolutely positioned.
paddingRight: 48,
},
},
{
props: ({ ownerState }) => !!ownerState.secondaryAction,
style: {
[`& > .${listItemButtonClasses.root}`]: {
paddingRight: 48,
},
},
},
{
props: {
alignItems: 'flex-start',
},
style: {
alignItems: 'flex-start',
},
},
{
props: ({ ownerState }) => ownerState.divider,
style: {
borderBottom: `1px solid ${(theme.vars || theme).palette.divider}`,
backgroundClip: 'padding-box',
},
},
{
props: ({ ownerState }) => ownerState.button,
style: {
transition: theme.transitions.create('background-color', {
duration: theme.transitions.duration.shortest,
}),
'&:hover': {
textDecoration: 'none',
backgroundColor: (theme.vars || theme).palette.action.hover,
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
backgroundColor: 'transparent',
},
},
},
},
{
props: ({ ownerState }) => ownerState.hasSecondaryAction,
style: {
// Add some space to avoid collision as `ListItemSecondaryAction`
// is absolutely positioned.
paddingRight: 48,
},
},
],
})),
);
const ListItemContainer = styled('li', {
name: 'MuiListItem',
slot: 'Container',
overridesResolver: (props, styles) => styles.container,
})({
position: 'relative',
});
/**
* Uses an additional container component if `ListItemSecondaryAction` is the last child.
*/
const ListItem = React.forwardRef(function ListItem(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiListItem' });
const {
alignItems = 'center',
children: childrenProp,
className,
component: componentProp,
components = {},
componentsProps = {},
ContainerComponent = 'li',
ContainerProps: { className: ContainerClassName, ...ContainerProps } = {},
dense = false,
disableGutters = false,
disablePadding = false,
divider = false,
secondaryAction,
slotProps = {},
slots = {},
...other
} = props;
const context = React.useContext(ListContext);
const childContext = React.useMemo(
() => ({
dense: dense || context.dense || false,
alignItems,
disableGutters,
}),
[alignItems, context.dense, dense, disableGutters],
);
const listItemRef = React.useRef(null);
const children = React.Children.toArray(childrenProp);
// v4 implementation, deprecated in v6, will be removed in v7
const hasSecondaryAction =
children.length && isMuiElement(children[children.length - 1], ['ListItemSecondaryAction']);
const ownerState = {
...props,
alignItems,
dense: childContext.dense,
disableGutters,
disablePadding,
divider,
hasSecondaryAction,
};
const classes = useUtilityClasses(ownerState);
const handleRef = useForkRef(listItemRef, ref);
const Root = slots.root || components.Root || ListItemRoot;
const rootProps = slotProps.root || componentsProps.root || {};
const componentProps = {
className: clsx(classes.root, rootProps.className, className),
...other,
};
let Component = componentProp || 'li';
// v4 implementation, deprecated in v6, will be removed in v7
if (hasSecondaryAction) {
// Use div by default.
Component = !componentProps.component && !componentProp ? 'div' : Component;
// Avoid nesting of li > li.
if (ContainerComponent === 'li') {
if (Component === 'li') {
Component = 'div';
} else if (componentProps.component === 'li') {
componentProps.component = 'div';
}
}
return (
<ListContext.Provider value={childContext}>
<ListItemContainer
as={ContainerComponent}
className={clsx(classes.container, ContainerClassName)}
ref={handleRef}
ownerState={ownerState}
{...ContainerProps}
>
<Root
{...rootProps}
{...(!isHostComponent(Root) && {
as: Component,
ownerState: { ...ownerState, ...rootProps.ownerState },
})}
{...componentProps}
>
{children}
</Root>
{children.pop()}
</ListItemContainer>
</ListContext.Provider>
);
}
return (
<ListContext.Provider value={childContext}>
<Root
{...rootProps}
as={Component}
ref={handleRef}
{...(!isHostComponent(Root) && {
ownerState: { ...ownerState, ...rootProps.ownerState },
})}
{...componentProps}
>
{children}
{secondaryAction && <ListItemSecondaryAction>{secondaryAction}</ListItemSecondaryAction>}
</Root>
</ListContext.Provider>
);
});
ListItem.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`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Defines the `align-items` style property.
* @default 'center'
*/
alignItems: PropTypes.oneOf(['center', 'flex-start']),
/**
* The content of the component if a `ListItemSecondaryAction` is used it must
* be the last child.
*/
children: chainPropTypes(PropTypes.node, (props) => {
const children = React.Children.toArray(props.children);
// React.Children.toArray(props.children).findLastIndex(isListItemSecondaryAction)
let secondaryActionIndex = -1;
for (let i = children.length - 1; i >= 0; i -= 1) {
const child = children[i];
if (isMuiElement(child, ['ListItemSecondaryAction'])) {
secondaryActionIndex = i;
break;
}
}
// is ListItemSecondaryAction the last child of ListItem
if (secondaryActionIndex !== -1 && secondaryActionIndex !== children.length - 1) {
return new Error(
'MUI: You used an element after ListItemSecondaryAction. ' +
'For ListItem to detect that it has a secondary action ' +
'you must pass it as the last child to ListItem.',
);
}
return null;
}),
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* The components used for each slot inside.
*
* @deprecated Use the `slots` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
* @default {}
*/
components: PropTypes.shape({
Root: PropTypes.elementType,
}),
/**
* The extra props for the slot components.
* You can override the existing props or add new ones.
*
* @deprecated Use the `slotProps` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
* @default {}
*/
componentsProps: PropTypes.shape({
root: PropTypes.object,
}),
/**
* The container component used when a `ListItemSecondaryAction` is the last child.
* @default 'li'
* @deprecated Use the `component` or `slots.root` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
ContainerComponent: elementTypeAcceptingRef,
/**
* Props applied to the container component if used.
* @default {}
* @deprecated Use the `slotProps.root` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
ContainerProps: PropTypes.object,
/**
* If `true`, compact vertical padding designed for keyboard and mouse input is used.
* The prop defaults to the value inherited from the parent List component.
* @default false
*/
dense: PropTypes.bool,
/**
* If `true`, the left and right padding is removed.
* @default false
*/
disableGutters: PropTypes.bool,
/**
* If `true`, all padding is removed.
* @default false
*/
disablePadding: PropTypes.bool,
/**
* If `true`, a 1px light border is added to the bottom of the list item.
* @default false
*/
divider: PropTypes.bool,
/**
* The element to display at the end of ListItem.
*/
secondaryAction: PropTypes.node,
/**
* The extra props for the slot components.
* You can override the existing props or add new ones.
*
* @default {}
*/
slotProps: PropTypes.shape({
root: PropTypes.object,
}),
/**
* The components used for each slot inside.
*
* @default {}
*/
slots: PropTypes.shape({
root: PropTypes.elementType,
}),
/**
* 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,
]),
};
export default ListItem;