92 lines
3.8 KiB
JavaScript
92 lines
3.8 KiB
JavaScript
"use strict";
|
|
'use client';
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.useListItem = useListItem;
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
var React = _interopRequireWildcard(require("react"));
|
|
var _extractEventHandlers = require("../utils/extractEventHandlers");
|
|
var _listActions = require("./listActions.types");
|
|
var _ListContext = require("./ListContext");
|
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
/**
|
|
* Contains the logic for an item of a list-like component (for example Select, Menu, etc.).
|
|
* It handles the item's mouse events and tab index.
|
|
*
|
|
* @template ItemValue The type of the item's value. This should be consistent with the type of useList's `items` parameter.
|
|
* @ignore - internal hook.
|
|
*/
|
|
function useListItem(parameters) {
|
|
const {
|
|
handlePointerOverEvents = false,
|
|
item
|
|
} = parameters;
|
|
const listContext = React.useContext(_ListContext.ListContext);
|
|
if (!listContext) {
|
|
throw new Error('useListItem must be used within a ListProvider');
|
|
}
|
|
const {
|
|
dispatch,
|
|
getItemState
|
|
} = listContext;
|
|
const {
|
|
highlighted,
|
|
selected,
|
|
focusable
|
|
} = getItemState(item);
|
|
const createHandleClick = React.useCallback(externalHandlers => event => {
|
|
var _externalHandlers$onC;
|
|
(_externalHandlers$onC = externalHandlers.onClick) == null || _externalHandlers$onC.call(externalHandlers, event);
|
|
if (event.defaultPrevented) {
|
|
return;
|
|
}
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
if (item === undefined) {
|
|
throw new Error(['MUI: The `item` provided to useListItem() is undefined.', 'This should happen only during server-side rendering under React 17.'].join('\n'));
|
|
}
|
|
}
|
|
dispatch({
|
|
type: _listActions.ListActionTypes.itemClick,
|
|
item: item,
|
|
event
|
|
});
|
|
}, [dispatch, item]);
|
|
const createHandlePointerOver = React.useCallback(externalHandlers => event => {
|
|
var _externalHandlers$onM;
|
|
(_externalHandlers$onM = externalHandlers.onMouseOver) == null || _externalHandlers$onM.call(externalHandlers, event);
|
|
if (event.defaultPrevented) {
|
|
return;
|
|
}
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
if (item === undefined) {
|
|
throw new Error(['MUI: The `item` provided to useListItem() is undefined.', 'This should happen only during server-side rendering under React 17.'].join('\n'));
|
|
}
|
|
}
|
|
dispatch({
|
|
type: _listActions.ListActionTypes.itemHover,
|
|
item: item,
|
|
event
|
|
});
|
|
}, [dispatch, item]);
|
|
let tabIndex;
|
|
if (focusable) {
|
|
tabIndex = highlighted ? 0 : -1;
|
|
}
|
|
const getRootProps = (externalProps = {}) => {
|
|
const externalEventHandlers = (0, _extractEventHandlers.extractEventHandlers)(externalProps);
|
|
return (0, _extends2.default)({}, externalProps, {
|
|
onClick: createHandleClick(externalEventHandlers),
|
|
onPointerOver: handlePointerOverEvents ? createHandlePointerOver(externalEventHandlers) : undefined,
|
|
tabIndex
|
|
});
|
|
};
|
|
return {
|
|
getRootProps,
|
|
highlighted,
|
|
selected
|
|
};
|
|
} |