UNPKG

5.65 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = void 0;
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
10var React = _interopRequireWildcard(require("react"));
11var _propTypes = _interopRequireDefault(require("prop-types"));
12var _utils = require("@mui/utils");
13var _composeClasses = _interopRequireDefault(require("../composeClasses"));
14var _optionClasses = require("./optionClasses");
15var _utils2 = require("../utils");
16var _useOption = _interopRequireDefault(require("../useOption"));
17var _ClassNameConfigurator = require("../utils/ClassNameConfigurator");
18var _jsxRuntime = require("react/jsx-runtime");
19const _excluded = ["children", "disabled", "label", "slotProps", "slots", "value"];
20function _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); }
21function _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; }
22function useUtilityClasses(ownerState) {
23 const {
24 disabled,
25 highlighted,
26 selected
27 } = ownerState;
28 const slots = {
29 root: ['root', disabled && 'disabled', highlighted && 'highlighted', selected && 'selected']
30 };
31 return (0, _composeClasses.default)(slots, (0, _ClassNameConfigurator.useClassNamesOverride)(_optionClasses.getOptionUtilityClass));
32}
33
34/**
35 * An unstyled option to be used within a Select.
36 */
37const Option = /*#__PURE__*/React.forwardRef(function Option(props, forwardedRef) {
38 var _slots$root, _optionRef$current;
39 const {
40 children,
41 disabled = false,
42 label,
43 slotProps = {},
44 slots = {},
45 value
46 } = props,
47 other = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
48 const Root = (_slots$root = slots.root) != null ? _slots$root : 'li';
49 const optionRef = React.useRef(null);
50 const combinedRef = (0, _utils.unstable_useForkRef)(optionRef, forwardedRef);
51
52 // If `label` is not explicitly provided, the `children` are used for convenience.
53 // This is used to populate the select's trigger with the selected option's label.
54 const computedLabel = label != null ? label : typeof children === 'string' ? children : (_optionRef$current = optionRef.current) == null ? void 0 : _optionRef$current.innerText;
55 const {
56 getRootProps,
57 selected,
58 highlighted,
59 index
60 } = (0, _useOption.default)({
61 disabled,
62 label: computedLabel,
63 rootRef: combinedRef,
64 value
65 });
66 const ownerState = (0, _extends2.default)({}, props, {
67 disabled,
68 highlighted,
69 index,
70 selected
71 });
72 const classes = useUtilityClasses(ownerState);
73 const rootProps = (0, _utils2.useSlotProps)({
74 getSlotProps: getRootProps,
75 elementType: Root,
76 externalSlotProps: slotProps.root,
77 externalForwardedProps: other,
78 className: classes.root,
79 ownerState
80 });
81 return /*#__PURE__*/(0, _jsxRuntime.jsx)(Root, (0, _extends2.default)({}, rootProps, {
82 children: children
83 }));
84});
85process.env.NODE_ENV !== "production" ? Option.propTypes /* remove-proptypes */ = {
86 // ----------------------------- Warning --------------------------------
87 // | These PropTypes are generated from the TypeScript type definitions |
88 // | To update them edit TypeScript types and run "yarn proptypes" |
89 // ----------------------------------------------------------------------
90 /**
91 * @ignore
92 */
93 children: _propTypes.default.node,
94 /**
95 * If `true`, the option will be disabled.
96 * @default false
97 */
98 disabled: _propTypes.default.bool,
99 /**
100 * A text representation of the option's content.
101 * Used for keyboard text navigation matching.
102 */
103 label: _propTypes.default.string,
104 /**
105 * The props used for each slot inside the Option.
106 * @default {}
107 */
108 slotProps: _propTypes.default.shape({
109 root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
110 }),
111 /**
112 * The components used for each slot inside the Option.
113 * Either a string to use a HTML element or a component.
114 * @default {}
115 */
116 slots: _propTypes.default.shape({
117 root: _propTypes.default.elementType
118 }),
119 /**
120 * The value of the option.
121 */
122 value: _propTypes.default.any.isRequired
123} : void 0;
124
125/**
126 * An unstyled option to be used within a Select.
127 *
128 * Demos:
129 *
130 * - [Select](https://mui.com/base/react-select/)
131 *
132 * API:
133 *
134 * - [Option API](https://mui.com/base/react-select/components-api/#option)
135 */
136var _default = /*#__PURE__*/React.memo(Option);
137exports.default = _default;
\No newline at end of file