UNPKG

4.66 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = useListItem;
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9var React = _interopRequireWildcard(require("react"));
10var _utils = require("@mui/utils");
11var _useForcedRerendering = _interopRequireDefault(require("../utils/useForcedRerendering"));
12var _listActions = require("./listActions.types");
13var _ListContext = require("./ListContext");
14function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
16/**
17 * Contains the logic for an item of a list-like component (e.g. Select, Menu, etc.).
18 * It provides information about the item's state (selected, highlighted) and
19 * handles the item's mouse events.
20 *
21 * @template ItemValue The type of the item's value. This should be consistent with the type of useList's `items` parameter.
22 * @ignore - internal hook.
23 */
24function useListItem(parameters) {
25 const {
26 handlePointerOverEvents = false,
27 item,
28 rootRef: externalRef
29 } = parameters;
30 const itemRef = React.useRef(null);
31 const handleRef = (0, _utils.unstable_useForkRef)(itemRef, externalRef);
32 const listContext = React.useContext(_ListContext.ListContext);
33 if (!listContext) {
34 throw new Error('useListItem must be used within a ListProvider');
35 }
36 const {
37 dispatch,
38 getItemState,
39 registerHighlightChangeHandler,
40 registerSelectionChangeHandler
41 } = listContext;
42 const {
43 highlighted,
44 selected,
45 focusable
46 } = getItemState(item);
47 const rerender = (0, _useForcedRerendering.default)();
48 (0, _utils.unstable_useEnhancedEffect)(() => {
49 function updateHighlightedState(highlightedItem) {
50 if (highlightedItem === item && !highlighted) {
51 rerender();
52 } else if (highlightedItem !== item && highlighted) {
53 rerender();
54 }
55 }
56 return registerHighlightChangeHandler(updateHighlightedState);
57 });
58 (0, _utils.unstable_useEnhancedEffect)(() => {
59 function updateSelectedState(selectedItems) {
60 if (!selected) {
61 if (selectedItems.includes(item)) {
62 rerender();
63 }
64 } else if (!selectedItems.includes(item)) {
65 rerender();
66 }
67 }
68 return registerSelectionChangeHandler(updateSelectedState);
69 }, [registerSelectionChangeHandler, rerender, selected, item]);
70 const createHandleClick = React.useCallback(other => event => {
71 var _other$onClick;
72 (_other$onClick = other.onClick) == null ? void 0 : _other$onClick.call(other, event);
73 if (event.defaultPrevented) {
74 return;
75 }
76 dispatch({
77 type: _listActions.ListActionTypes.itemClick,
78 item,
79 event
80 });
81 }, [dispatch, item]);
82 const createHandlePointerOver = React.useCallback(other => event => {
83 var _other$onMouseOver;
84 (_other$onMouseOver = other.onMouseOver) == null ? void 0 : _other$onMouseOver.call(other, event);
85 if (event.defaultPrevented) {
86 return;
87 }
88 dispatch({
89 type: _listActions.ListActionTypes.itemHover,
90 item,
91 event
92 });
93 }, [dispatch, item]);
94 let tabIndex;
95 if (focusable) {
96 tabIndex = highlighted ? 0 : -1;
97 }
98 const getRootProps = (otherHandlers = {}) => (0, _extends2.default)({}, otherHandlers, {
99 onClick: createHandleClick(otherHandlers),
100 onPointerOver: handlePointerOverEvents ? createHandlePointerOver(otherHandlers) : undefined,
101 ref: handleRef,
102 tabIndex
103 });
104 return {
105 getRootProps,
106 highlighted,
107 rootRef: handleRef,
108 selected
109 };
110}
\No newline at end of file