1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import { chainPropTypes } from '@material-ui/utils';
|
7 | import withStyles from '../styles/withStyles';
|
8 | import ButtonBase from '../ButtonBase';
|
9 | import isMuiElement from '../utils/isMuiElement';
|
10 | import useForkRef from '../utils/useForkRef';
|
11 | import ListContext from '../List/ListContext';
|
12 | import * as ReactDOM from 'react-dom';
|
13 | export const styles = theme => ({
|
14 |
|
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 |
|
38 | container: {
|
39 | position: 'relative'
|
40 | },
|
41 |
|
42 |
|
43 | focusVisible: {},
|
44 |
|
45 |
|
46 | dense: {
|
47 | paddingTop: 4,
|
48 | paddingBottom: 4
|
49 | },
|
50 |
|
51 |
|
52 | alignItemsFlexStart: {
|
53 | alignItems: 'flex-start'
|
54 | },
|
55 |
|
56 |
|
57 | disabled: {},
|
58 |
|
59 |
|
60 | divider: {
|
61 | borderBottom: `1px solid ${theme.palette.divider}`,
|
62 | backgroundClip: 'padding-box'
|
63 | },
|
64 |
|
65 |
|
66 | gutters: {
|
67 | paddingLeft: 16,
|
68 | paddingRight: 16
|
69 | },
|
70 |
|
71 |
|
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 |
|
80 | '@media (hover: none)': {
|
81 | backgroundColor: 'transparent'
|
82 | }
|
83 | }
|
84 | },
|
85 |
|
86 |
|
87 | secondaryAction: {
|
88 |
|
89 |
|
90 | paddingRight: 48
|
91 | },
|
92 |
|
93 |
|
94 | selected: {}
|
95 | });
|
96 | const useEnhancedEffect = typeof window === 'undefined' ? React.useEffect : React.useLayoutEffect;
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | const ListItem = 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 |
|
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 |
|
162 | Component = !componentProps.component && !componentProp ? 'div' : Component;
|
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 React.createElement(ListContext.Provider, {
|
173 | value: childContext
|
174 | }, React.createElement(ContainerComponent, _extends({
|
175 | className: clsx(classes.container, ContainerClassName),
|
176 | ref: handleRef
|
177 | }, ContainerProps), React.createElement(Component, componentProps, children), children.pop()));
|
178 | }
|
179 |
|
180 | return React.createElement(ListContext.Provider, {
|
181 | value: childContext
|
182 | }, React.createElement(Component, _extends({
|
183 | ref: handleRef
|
184 | }, componentProps), children));
|
185 | });
|
186 | process.env.NODE_ENV !== "production" ? ListItem.propTypes = {
|
187 | |
188 |
|
189 |
|
190 | alignItems: PropTypes.oneOf(['flex-start', 'center']),
|
191 |
|
192 | |
193 |
|
194 |
|
195 |
|
196 | autoFocus: PropTypes.bool,
|
197 |
|
198 | |
199 |
|
200 |
|
201 |
|
202 | button: PropTypes.bool,
|
203 |
|
204 | |
205 |
|
206 |
|
207 |
|
208 | children: chainPropTypes(PropTypes.node, props => {
|
209 | const children = React.Children.toArray(props.children);
|
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 | }
|
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 |
|
232 |
|
233 |
|
234 | classes: PropTypes.object.isRequired,
|
235 |
|
236 | |
237 |
|
238 |
|
239 | className: PropTypes.string,
|
240 |
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 | component: PropTypes
|
247 |
|
248 | .elementType,
|
249 |
|
250 | |
251 |
|
252 |
|
253 | ContainerComponent: PropTypes.elementType,
|
254 |
|
255 | |
256 |
|
257 |
|
258 | ContainerProps: PropTypes.object,
|
259 |
|
260 | |
261 |
|
262 |
|
263 | dense: PropTypes.bool,
|
264 |
|
265 | |
266 |
|
267 |
|
268 | disabled: PropTypes.bool,
|
269 |
|
270 | |
271 |
|
272 |
|
273 | disableGutters: PropTypes.bool,
|
274 |
|
275 | |
276 |
|
277 |
|
278 | divider: PropTypes.bool,
|
279 |
|
280 | |
281 |
|
282 |
|
283 | focusVisibleClassName: PropTypes.string,
|
284 |
|
285 | |
286 |
|
287 |
|
288 | selected: PropTypes.bool
|
289 | } : void 0;
|
290 | export default withStyles(styles, {
|
291 | name: 'MuiListItem'
|
292 | })(ListItem); |
\ | No newline at end of file |