UNPKG

2.77 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = useOption;
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9var React = _interopRequireWildcard(require("react"));
10var _utils = require("@mui/utils");
11var _useList = require("../useList");
12var _useCompoundItem = require("../utils/useCompoundItem");
13function _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); }
14function _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; }
15/**
16 *
17 * Demos:
18 *
19 * - [Select](https://mui.com/base/react-select/#hooks)
20 *
21 * API:
22 *
23 * - [useOption API](https://mui.com/base/react-select/hooks-api/#use-option)
24 */
25function useOption(params) {
26 const {
27 value,
28 label,
29 disabled,
30 rootRef: optionRefParam,
31 id: idParam
32 } = params;
33 const {
34 getRootProps: getListItemProps,
35 rootRef: listItemRefHandler,
36 highlighted,
37 selected
38 } = (0, _useList.useListItem)({
39 item: value
40 });
41 const id = (0, _utils.unstable_useId)(idParam);
42 const optionRef = React.useRef(null);
43 const selectOption = React.useMemo(() => ({
44 disabled,
45 label,
46 value,
47 ref: optionRef,
48 id
49 }), [disabled, label, value, id]);
50 const {
51 index
52 } = (0, _useCompoundItem.useCompoundItem)(value, selectOption);
53 const handleRef = (0, _utils.unstable_useForkRef)(optionRefParam, optionRef, listItemRefHandler);
54 return {
55 getRootProps: (otherHandlers = {}) => (0, _extends2.default)({}, otherHandlers, getListItemProps(otherHandlers), {
56 id,
57 ref: handleRef,
58 role: 'option',
59 'aria-selected': selected
60 }),
61 highlighted,
62 index,
63 selected,
64 rootRef: handleRef
65 };
66}
\No newline at end of file