1 | 'use client';
|
2 |
|
3 | import _extends from "@babel/runtime/helpers/esm/extends";
|
4 | import * as React from 'react';
|
5 | import { extractEventHandlers } from '../utils/extractEventHandlers';
|
6 | import { ListActionTypes } from './listActions.types';
|
7 | import { ListContext } from './ListContext';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export function useListItem(parameters) {
|
17 | const {
|
18 | handlePointerOverEvents = false,
|
19 | item
|
20 | } = parameters;
|
21 | const listContext = React.useContext(ListContext);
|
22 | if (!listContext) {
|
23 | throw new Error('useListItem must be used within a ListProvider');
|
24 | }
|
25 | const {
|
26 | dispatch,
|
27 | getItemState
|
28 | } = listContext;
|
29 | const {
|
30 | highlighted,
|
31 | selected,
|
32 | focusable
|
33 | } = getItemState(item);
|
34 | const createHandleClick = React.useCallback(externalHandlers => event => {
|
35 | externalHandlers.onClick?.(event);
|
36 | if (event.defaultPrevented) {
|
37 | return;
|
38 | }
|
39 | if (process.env.NODE_ENV !== 'production') {
|
40 | if (item === undefined) {
|
41 | throw new Error(['MUI: The `item` provided to useListItem() is undefined.', 'This should happen only during server-side rendering under React 17.'].join('\n'));
|
42 | }
|
43 | }
|
44 | dispatch({
|
45 | type: ListActionTypes.itemClick,
|
46 | item: item,
|
47 | event
|
48 | });
|
49 | }, [dispatch, item]);
|
50 | const createHandlePointerOver = React.useCallback(externalHandlers => event => {
|
51 | externalHandlers.onMouseOver?.(event);
|
52 | if (event.defaultPrevented) {
|
53 | return;
|
54 | }
|
55 | if (process.env.NODE_ENV !== 'production') {
|
56 | if (item === undefined) {
|
57 | throw new Error(['MUI: The `item` provided to useListItem() is undefined.', 'This should happen only during server-side rendering under React 17.'].join('\n'));
|
58 | }
|
59 | }
|
60 | dispatch({
|
61 | type: ListActionTypes.itemHover,
|
62 | item: item,
|
63 | event
|
64 | });
|
65 | }, [dispatch, item]);
|
66 | let tabIndex;
|
67 | if (focusable) {
|
68 | tabIndex = highlighted ? 0 : -1;
|
69 | }
|
70 | const getRootProps = (externalProps = {}) => {
|
71 | const externalEventHandlers = extractEventHandlers(externalProps);
|
72 | return _extends({}, externalProps, {
|
73 | onClick: createHandleClick(externalEventHandlers),
|
74 | onPointerOver: handlePointerOverEvents ? createHandlePointerOver(externalEventHandlers) : undefined,
|
75 | tabIndex
|
76 | });
|
77 | };
|
78 | return {
|
79 | getRootProps,
|
80 | highlighted,
|
81 | selected
|
82 | };
|
83 | } |
\ | No newline at end of file |