UNPKG

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