UNPKG

9.4 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import { chainPropTypes } from '@material-ui/utils';
7import withStyles from '../styles/withStyles';
8import ButtonBase from '../ButtonBase';
9import isMuiElement from '../utils/isMuiElement';
10import useForkRef from '../utils/useForkRef';
11import ListContext from '../List/ListContext';
12import * as ReactDOM from 'react-dom';
13export const styles = theme => ({
14 /* Styles applied to the (normally root) `component` element. May be wrapped by a `container`. */
15 root: {
16 display: 'flex',
17 justifyContent: 'flex-start',
18 alignItems: 'center',
19 position: 'relative',
20 textDecoration: 'none',
21 width: '100%',
22 boxSizing: 'border-box',
23 textAlign: 'left',
24 paddingTop: 8,
25 paddingBottom: 8,
26 '&$focusVisible': {
27 backgroundColor: theme.palette.action.selected
28 },
29 '&$selected, &$selected:hover': {
30 backgroundColor: theme.palette.action.selected
31 },
32 '&$disabled': {
33 opacity: 0.5
34 }
35 },
36
37 /* Styles applied to the `container` element if `children` includes `ListItemSecondaryAction`. */
38 container: {
39 position: 'relative'
40 },
41
42 /* Pseudo-class applied to the `component`'s `focusVisibleClassName` prop if `button={true}`. */
43 focusVisible: {},
44
45 /* Styles applied to the `component` element if dense. */
46 dense: {
47 paddingTop: 4,
48 paddingBottom: 4
49 },
50
51 /* Styles applied to the `component` element if `alignItems="flex-start"`. */
52 alignItemsFlexStart: {
53 alignItems: 'flex-start'
54 },
55
56 /* Pseudo-class applied to the inner `component` element if `disabled={true}`. */
57 disabled: {},
58
59 /* Styles applied to the inner `component` element if `divider={true}`. */
60 divider: {
61 borderBottom: `1px solid ${theme.palette.divider}`,
62 backgroundClip: 'padding-box'
63 },
64
65 /* Styles applied to the inner `component` element if `disableGutters={false}`. */
66 gutters: {
67 paddingLeft: 16,
68 paddingRight: 16
69 },
70
71 /* Styles applied to the inner `component` element if `button={true}`. */
72 button: {
73 transition: theme.transitions.create('background-color', {
74 duration: theme.transitions.duration.shortest
75 }),
76 '&:hover': {
77 textDecoration: 'none',
78 backgroundColor: theme.palette.action.hover,
79 // Reset on touch devices, it doesn't add specificity
80 '@media (hover: none)': {
81 backgroundColor: 'transparent'
82 }
83 }
84 },
85
86 /* Styles applied to the `component` element if `children` includes `ListItemSecondaryAction`. */
87 secondaryAction: {
88 // Add some space to avoid collision as `ListItemSecondaryAction`
89 // is absolutely positioned.
90 paddingRight: 48
91 },
92
93 /* Pseudo-class applied to the root element if `selected={true}`. */
94 selected: {}
95});
96const useEnhancedEffect = typeof window === 'undefined' ? React.useEffect : React.useLayoutEffect;
97/**
98 * Uses an additional container component if `ListItemSecondaryAction` is the last child.
99 */
100
101const ListItem = /*#__PURE__*/React.forwardRef(function ListItem(props, ref) {
102 const {
103 alignItems = 'center',
104 autoFocus = false,
105 button = false,
106 children: childrenProp,
107 classes,
108 className,
109 component: componentProp,
110 ContainerComponent = 'li',
111 ContainerProps: {
112 className: ContainerClassName
113 } = {},
114 dense = false,
115 disabled = false,
116 disableGutters = false,
117 divider = false,
118 focusVisibleClassName,
119 selected = false
120 } = props,
121 ContainerProps = _objectWithoutPropertiesLoose(props.ContainerProps, ["className"]),
122 other = _objectWithoutPropertiesLoose(props, ["alignItems", "autoFocus", "button", "children", "classes", "className", "component", "ContainerComponent", "ContainerProps", "dense", "disabled", "disableGutters", "divider", "focusVisibleClassName", "selected"]);
123
124 const context = React.useContext(ListContext);
125 const childContext = {
126 dense: dense || context.dense || false,
127 alignItems
128 };
129 const listItemRef = React.useRef(null);
130 useEnhancedEffect(() => {
131 if (autoFocus) {
132 if (listItemRef.current) {
133 listItemRef.current.focus();
134 } else if (process.env.NODE_ENV !== 'production') {
135 console.error('Material-UI: Unable to set focus to a ListItem whose component has not been rendered.');
136 }
137 }
138 }, [autoFocus]);
139 const children = React.Children.toArray(childrenProp);
140 const hasSecondaryAction = children.length && isMuiElement(children[children.length - 1], ['ListItemSecondaryAction']);
141 const handleOwnRef = React.useCallback(instance => {
142 // #StrictMode ready
143 listItemRef.current = ReactDOM.findDOMNode(instance);
144 }, []);
145 const handleRef = useForkRef(handleOwnRef, ref);
146
147 const componentProps = _extends({
148 className: clsx(classes.root, className, childContext.dense && classes.dense, !disableGutters && classes.gutters, divider && classes.divider, disabled && classes.disabled, button && classes.button, alignItems !== "center" && classes.alignItemsFlexStart, hasSecondaryAction && classes.secondaryAction, selected && classes.selected),
149 disabled
150 }, other);
151
152 let Component = componentProp || 'li';
153
154 if (button) {
155 componentProps.component = componentProp || 'div';
156 componentProps.focusVisibleClassName = clsx(classes.focusVisible, focusVisibleClassName);
157 Component = ButtonBase;
158 }
159
160 if (hasSecondaryAction) {
161 // Use div by default.
162 Component = !componentProps.component && !componentProp ? 'div' : Component; // Avoid nesting of li > li.
163
164 if (ContainerComponent === 'li') {
165 if (Component === 'li') {
166 Component = 'div';
167 } else if (componentProps.component === 'li') {
168 componentProps.component = 'div';
169 }
170 }
171
172 return /*#__PURE__*/React.createElement(ListContext.Provider, {
173 value: childContext
174 }, /*#__PURE__*/React.createElement(ContainerComponent, _extends({
175 className: clsx(classes.container, ContainerClassName),
176 ref: handleRef
177 }, ContainerProps), /*#__PURE__*/React.createElement(Component, componentProps, children), children.pop()));
178 }
179
180 return /*#__PURE__*/React.createElement(ListContext.Provider, {
181 value: childContext
182 }, /*#__PURE__*/React.createElement(Component, _extends({
183 ref: handleRef
184 }, componentProps), children));
185});
186process.env.NODE_ENV !== "production" ? ListItem.propTypes = {
187 /**
188 * Defines the `align-items` style property.
189 */
190 alignItems: PropTypes.oneOf(['flex-start', 'center']),
191
192 /**
193 * If `true`, the list item will be focused during the first mount.
194 * Focus will also be triggered if the value changes from false to true.
195 */
196 autoFocus: PropTypes.bool,
197
198 /**
199 * If `true`, the list item will be a button (using `ButtonBase`). Props intended
200 * for `ButtonBase` can then be applied to `ListItem`.
201 */
202 button: PropTypes.bool,
203
204 /**
205 * The content of the component. If a `ListItemSecondaryAction` is used it must
206 * be the last child.
207 */
208 children: chainPropTypes(PropTypes.node, props => {
209 const children = React.Children.toArray(props.children); // React.Children.toArray(props.children).findLastIndex(isListItemSecondaryAction)
210
211 let secondaryActionIndex = -1;
212
213 for (let i = children.length - 1; i >= 0; i -= 1) {
214 const child = children[i];
215
216 if (isMuiElement(child, ['ListItemSecondaryAction'])) {
217 secondaryActionIndex = i;
218 break;
219 }
220 } // is ListItemSecondaryAction the last child of ListItem
221
222
223 if (secondaryActionIndex !== -1 && secondaryActionIndex !== children.length - 1) {
224 return new Error('Material-UI: 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.');
225 }
226
227 return null;
228 }),
229
230 /**
231 * Override or extend the styles applied to the component.
232 * See [CSS API](#css) below for more details.
233 */
234 classes: PropTypes.object.isRequired,
235
236 /**
237 * @ignore
238 */
239 className: PropTypes.string,
240
241 /**
242 * The component used for the root node.
243 * Either a string to use a HTML element or a component.
244 * By default, it's a `li` when `button` is `false` and a `div` when `button` is `true`.
245 */
246 component: PropTypes
247 /* @typescript-to-proptypes-ignore */
248 .elementType,
249
250 /**
251 * The container component used when a `ListItemSecondaryAction` is the last child.
252 */
253 ContainerComponent: PropTypes.elementType,
254
255 /**
256 * Props applied to the container component if used.
257 */
258 ContainerProps: PropTypes.object,
259
260 /**
261 * If `true`, compact vertical padding designed for keyboard and mouse input will be used.
262 */
263 dense: PropTypes.bool,
264
265 /**
266 * If `true`, the list item will be disabled.
267 */
268 disabled: PropTypes.bool,
269
270 /**
271 * If `true`, the left and right padding is removed.
272 */
273 disableGutters: PropTypes.bool,
274
275 /**
276 * If `true`, a 1px light border is added to the bottom of the list item.
277 */
278 divider: PropTypes.bool,
279
280 /**
281 * @ignore
282 */
283 focusVisibleClassName: PropTypes.string,
284
285 /**
286 * Use to apply selected styling.
287 */
288 selected: PropTypes.bool
289} : void 0;
290export default withStyles(styles, {
291 name: 'MuiListItem'
292})(ListItem);
\No newline at end of file