1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import composeClasses from '@mui/utils/composeClasses';
|
7 | import elementTypeAcceptingRef from '@mui/utils/elementTypeAcceptingRef';
|
8 | import chainPropTypes from '@mui/utils/chainPropTypes';
|
9 | import isHostComponent from "../utils/isHostComponent.js";
|
10 | import { styled } from "../zero-styled/index.js";
|
11 | import memoTheme from "../utils/memoTheme.js";
|
12 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
13 | import isMuiElement from "../utils/isMuiElement.js";
|
14 | import useForkRef from "../utils/useForkRef.js";
|
15 | import ListContext from "../List/ListContext.js";
|
16 | import { getListItemUtilityClass } from "./listItemClasses.js";
|
17 | import { listItemButtonClasses } from "../ListItemButton/index.js";
|
18 | import ListItemSecondaryAction from "../ListItemSecondaryAction/index.js";
|
19 | import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
20 | export const overridesResolver = (props, styles) => {
|
21 | const {
|
22 | ownerState
|
23 | } = props;
|
24 | 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];
|
25 | };
|
26 | const useUtilityClasses = ownerState => {
|
27 | const {
|
28 | alignItems,
|
29 | classes,
|
30 | dense,
|
31 | disableGutters,
|
32 | disablePadding,
|
33 | divider,
|
34 | hasSecondaryAction
|
35 | } = ownerState;
|
36 | const slots = {
|
37 | root: ['root', dense && 'dense', !disableGutters && 'gutters', !disablePadding && 'padding', divider && 'divider', alignItems === 'flex-start' && 'alignItemsFlexStart', hasSecondaryAction && 'secondaryAction'],
|
38 | container: ['container']
|
39 | };
|
40 | return composeClasses(slots, getListItemUtilityClass, classes);
|
41 | };
|
42 | export const ListItemRoot = styled('div', {
|
43 | name: 'MuiListItem',
|
44 | slot: 'Root',
|
45 | overridesResolver
|
46 | })(memoTheme(({
|
47 | theme
|
48 | }) => ({
|
49 | display: 'flex',
|
50 | justifyContent: 'flex-start',
|
51 | alignItems: 'center',
|
52 | position: 'relative',
|
53 | textDecoration: 'none',
|
54 | width: '100%',
|
55 | boxSizing: 'border-box',
|
56 | textAlign: 'left',
|
57 | variants: [{
|
58 | props: ({
|
59 | ownerState
|
60 | }) => !ownerState.disablePadding,
|
61 | style: {
|
62 | paddingTop: 8,
|
63 | paddingBottom: 8
|
64 | }
|
65 | }, {
|
66 | props: ({
|
67 | ownerState
|
68 | }) => !ownerState.disablePadding && ownerState.dense,
|
69 | style: {
|
70 | paddingTop: 4,
|
71 | paddingBottom: 4
|
72 | }
|
73 | }, {
|
74 | props: ({
|
75 | ownerState
|
76 | }) => !ownerState.disablePadding && !ownerState.disableGutters,
|
77 | style: {
|
78 | paddingLeft: 16,
|
79 | paddingRight: 16
|
80 | }
|
81 | }, {
|
82 | props: ({
|
83 | ownerState
|
84 | }) => !ownerState.disablePadding && !!ownerState.secondaryAction,
|
85 | style: {
|
86 |
|
87 |
|
88 | paddingRight: 48
|
89 | }
|
90 | }, {
|
91 | props: ({
|
92 | ownerState
|
93 | }) => !!ownerState.secondaryAction,
|
94 | style: {
|
95 | [`& > .${listItemButtonClasses.root}`]: {
|
96 | paddingRight: 48
|
97 | }
|
98 | }
|
99 | }, {
|
100 | props: {
|
101 | alignItems: 'flex-start'
|
102 | },
|
103 | style: {
|
104 | alignItems: 'flex-start'
|
105 | }
|
106 | }, {
|
107 | props: ({
|
108 | ownerState
|
109 | }) => ownerState.divider,
|
110 | style: {
|
111 | borderBottom: `1px solid ${(theme.vars || theme).palette.divider}`,
|
112 | backgroundClip: 'padding-box'
|
113 | }
|
114 | }, {
|
115 | props: ({
|
116 | ownerState
|
117 | }) => ownerState.button,
|
118 | style: {
|
119 | transition: theme.transitions.create('background-color', {
|
120 | duration: theme.transitions.duration.shortest
|
121 | }),
|
122 | '&:hover': {
|
123 | textDecoration: 'none',
|
124 | backgroundColor: (theme.vars || theme).palette.action.hover,
|
125 |
|
126 | '@media (hover: none)': {
|
127 | backgroundColor: 'transparent'
|
128 | }
|
129 | }
|
130 | }
|
131 | }, {
|
132 | props: ({
|
133 | ownerState
|
134 | }) => ownerState.hasSecondaryAction,
|
135 | style: {
|
136 |
|
137 |
|
138 | paddingRight: 48
|
139 | }
|
140 | }]
|
141 | })));
|
142 | const ListItemContainer = styled('li', {
|
143 | name: 'MuiListItem',
|
144 | slot: 'Container',
|
145 | overridesResolver: (props, styles) => styles.container
|
146 | })({
|
147 | position: 'relative'
|
148 | });
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | const ListItem = React.forwardRef(function ListItem(inProps, ref) {
|
154 | const props = useDefaultProps({
|
155 | props: inProps,
|
156 | name: 'MuiListItem'
|
157 | });
|
158 | const {
|
159 | alignItems = 'center',
|
160 | children: childrenProp,
|
161 | className,
|
162 | component: componentProp,
|
163 | components = {},
|
164 | componentsProps = {},
|
165 | ContainerComponent = 'li',
|
166 | ContainerProps: {
|
167 | className: ContainerClassName,
|
168 | ...ContainerProps
|
169 | } = {},
|
170 | dense = false,
|
171 | disableGutters = false,
|
172 | disablePadding = false,
|
173 | divider = false,
|
174 | secondaryAction,
|
175 | slotProps = {},
|
176 | slots = {},
|
177 | ...other
|
178 | } = props;
|
179 | const context = React.useContext(ListContext);
|
180 | const childContext = React.useMemo(() => ({
|
181 | dense: dense || context.dense || false,
|
182 | alignItems,
|
183 | disableGutters
|
184 | }), [alignItems, context.dense, dense, disableGutters]);
|
185 | const listItemRef = React.useRef(null);
|
186 | const children = React.Children.toArray(childrenProp);
|
187 |
|
188 |
|
189 | const hasSecondaryAction = children.length && isMuiElement(children[children.length - 1], ['ListItemSecondaryAction']);
|
190 | const ownerState = {
|
191 | ...props,
|
192 | alignItems,
|
193 | dense: childContext.dense,
|
194 | disableGutters,
|
195 | disablePadding,
|
196 | divider,
|
197 | hasSecondaryAction
|
198 | };
|
199 | const classes = useUtilityClasses(ownerState);
|
200 | const handleRef = useForkRef(listItemRef, ref);
|
201 | const Root = slots.root || components.Root || ListItemRoot;
|
202 | const rootProps = slotProps.root || componentsProps.root || {};
|
203 | const componentProps = {
|
204 | className: clsx(classes.root, rootProps.className, className),
|
205 | ...other
|
206 | };
|
207 | let Component = componentProp || 'li';
|
208 |
|
209 |
|
210 | if (hasSecondaryAction) {
|
211 |
|
212 | Component = !componentProps.component && !componentProp ? 'div' : Component;
|
213 |
|
214 |
|
215 | if (ContainerComponent === 'li') {
|
216 | if (Component === 'li') {
|
217 | Component = 'div';
|
218 | } else if (componentProps.component === 'li') {
|
219 | componentProps.component = 'div';
|
220 | }
|
221 | }
|
222 | return _jsx(ListContext.Provider, {
|
223 | value: childContext,
|
224 | children: _jsxs(ListItemContainer, {
|
225 | as: ContainerComponent,
|
226 | className: clsx(classes.container, ContainerClassName),
|
227 | ref: handleRef,
|
228 | ownerState: ownerState,
|
229 | ...ContainerProps,
|
230 | children: [_jsx(Root, {
|
231 | ...rootProps,
|
232 | ...(!isHostComponent(Root) && {
|
233 | as: Component,
|
234 | ownerState: {
|
235 | ...ownerState,
|
236 | ...rootProps.ownerState
|
237 | }
|
238 | }),
|
239 | ...componentProps,
|
240 | children: children
|
241 | }), children.pop()]
|
242 | })
|
243 | });
|
244 | }
|
245 | return _jsx(ListContext.Provider, {
|
246 | value: childContext,
|
247 | children: _jsxs(Root, {
|
248 | ...rootProps,
|
249 | as: Component,
|
250 | ref: handleRef,
|
251 | ...(!isHostComponent(Root) && {
|
252 | ownerState: {
|
253 | ...ownerState,
|
254 | ...rootProps.ownerState
|
255 | }
|
256 | }),
|
257 | ...componentProps,
|
258 | children: [children, secondaryAction && _jsx(ListItemSecondaryAction, {
|
259 | children: secondaryAction
|
260 | })]
|
261 | })
|
262 | });
|
263 | });
|
264 | process.env.NODE_ENV !== "production" ? ListItem.propTypes = {
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | |
270 |
|
271 |
|
272 |
|
273 | alignItems: PropTypes.oneOf(['center', 'flex-start']),
|
274 | |
275 |
|
276 |
|
277 |
|
278 | children: chainPropTypes(PropTypes.node, props => {
|
279 | const children = React.Children.toArray(props.children);
|
280 |
|
281 |
|
282 | let secondaryActionIndex = -1;
|
283 | for (let i = children.length - 1; i >= 0; i -= 1) {
|
284 | const child = children[i];
|
285 | if (isMuiElement(child, ['ListItemSecondaryAction'])) {
|
286 | secondaryActionIndex = i;
|
287 | break;
|
288 | }
|
289 | }
|
290 |
|
291 |
|
292 | if (secondaryActionIndex !== -1 && secondaryActionIndex !== children.length - 1) {
|
293 | 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.');
|
294 | }
|
295 | return null;
|
296 | }),
|
297 | |
298 |
|
299 |
|
300 | classes: PropTypes.object,
|
301 | |
302 |
|
303 |
|
304 | className: PropTypes.string,
|
305 | |
306 |
|
307 |
|
308 |
|
309 | component: PropTypes.elementType,
|
310 | |
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | components: PropTypes.shape({
|
317 | Root: PropTypes.elementType
|
318 | }),
|
319 | |
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | componentsProps: PropTypes.shape({
|
327 | root: PropTypes.object
|
328 | }),
|
329 | |
330 |
|
331 |
|
332 |
|
333 |
|
334 | ContainerComponent: elementTypeAcceptingRef,
|
335 | |
336 |
|
337 |
|
338 |
|
339 |
|
340 | ContainerProps: PropTypes.object,
|
341 | |
342 |
|
343 |
|
344 |
|
345 |
|
346 | dense: PropTypes.bool,
|
347 | |
348 |
|
349 |
|
350 |
|
351 | disableGutters: PropTypes.bool,
|
352 | |
353 |
|
354 |
|
355 |
|
356 | disablePadding: PropTypes.bool,
|
357 | |
358 |
|
359 |
|
360 |
|
361 | divider: PropTypes.bool,
|
362 | |
363 |
|
364 |
|
365 | secondaryAction: PropTypes.node,
|
366 | |
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | slotProps: PropTypes.shape({
|
373 | root: PropTypes.object
|
374 | }),
|
375 | |
376 |
|
377 |
|
378 |
|
379 |
|
380 | slots: PropTypes.shape({
|
381 | root: PropTypes.elementType
|
382 | }),
|
383 | |
384 |
|
385 |
|
386 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
|
387 | } : void 0;
|
388 | export default ListItem; |
\ | No newline at end of file |