1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports.default = void 0;
|
8 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
9 | var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
10 | var React = _interopRequireWildcard(require("react"));
|
11 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
12 | var _utils = require("@mui/utils");
|
13 | var _useSelect = _interopRequireDefault(require("../useSelect"));
|
14 | var _utils2 = require("../utils");
|
15 | var _Popper = _interopRequireDefault(require("../Popper"));
|
16 | var _composeClasses = _interopRequireDefault(require("../composeClasses"));
|
17 | var _selectClasses = require("./selectClasses");
|
18 | var _defaultOptionStringifier = _interopRequireDefault(require("../useSelect/defaultOptionStringifier"));
|
19 | var _ClassNameConfigurator = require("../utils/ClassNameConfigurator");
|
20 | var _SelectProvider = _interopRequireDefault(require("../useSelect/SelectProvider"));
|
21 | var _jsxRuntime = require("react/jsx-runtime");
|
22 | const _excluded = ["autoFocus", "children", "defaultValue", "defaultListboxOpen", "disabled", "getSerializedValue", "listboxId", "listboxOpen", "multiple", "name", "onChange", "onListboxOpenChange", "getOptionAsString", "renderValue", "slotProps", "slots", "value"];
|
23 | function _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); }
|
24 | function _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; }
|
25 | function defaultRenderValue(selectedOptions) {
|
26 | var _selectedOptions$labe;
|
27 | if (Array.isArray(selectedOptions)) {
|
28 | return (0, _jsxRuntime.jsx)(React.Fragment, {
|
29 | children: selectedOptions.map(o => o.label).join(', ')
|
30 | });
|
31 | }
|
32 | return (_selectedOptions$labe = selectedOptions == null ? void 0 : selectedOptions.label) != null ? _selectedOptions$labe : '';
|
33 | }
|
34 | function defaultFormValueProvider(selectedOption) {
|
35 | if (Array.isArray(selectedOption)) {
|
36 | if (selectedOption.length === 0) {
|
37 | return '';
|
38 | }
|
39 | if (selectedOption.every(o => typeof o.value === 'string' || typeof o.value === 'number' || typeof o.value === 'boolean')) {
|
40 | return selectedOption.map(o => String(o.value));
|
41 | }
|
42 | return JSON.stringify(selectedOption.map(o => o.value));
|
43 | }
|
44 | if ((selectedOption == null ? void 0 : selectedOption.value) == null) {
|
45 | return '';
|
46 | }
|
47 | if (typeof selectedOption.value === 'string' || typeof selectedOption.value === 'number') {
|
48 | return selectedOption.value;
|
49 | }
|
50 | return JSON.stringify(selectedOption.value);
|
51 | }
|
52 | function useUtilityClasses(ownerState) {
|
53 | const {
|
54 | active,
|
55 | disabled,
|
56 | open,
|
57 | focusVisible
|
58 | } = ownerState;
|
59 | const slots = {
|
60 | root: ['root', disabled && 'disabled', focusVisible && 'focusVisible', active && 'active', open && 'expanded'],
|
61 | listbox: ['listbox', disabled && 'disabled'],
|
62 | popper: ['popper']
|
63 | };
|
64 | return (0, _composeClasses.default)(slots, (0, _ClassNameConfigurator.useClassNamesOverride)(_selectClasses.getSelectUtilityClass));
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | const Select = React.forwardRef(function Select(props, forwardedRef) {
|
79 | var _slots$root, _slots$listbox, _slots$popper;
|
80 | const {
|
81 | autoFocus,
|
82 | children,
|
83 | defaultValue,
|
84 | defaultListboxOpen = false,
|
85 | disabled: disabledProp,
|
86 | getSerializedValue = defaultFormValueProvider,
|
87 | listboxId,
|
88 | listboxOpen: listboxOpenProp,
|
89 | multiple = false,
|
90 | name,
|
91 | onChange,
|
92 | onListboxOpenChange,
|
93 | getOptionAsString = _defaultOptionStringifier.default,
|
94 | renderValue: renderValueProp,
|
95 | slotProps = {},
|
96 | slots = {},
|
97 | value: valueProp
|
98 | } = props,
|
99 | other = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
|
100 | const renderValue = renderValueProp != null ? renderValueProp : defaultRenderValue;
|
101 | const [buttonDefined, setButtonDefined] = React.useState(false);
|
102 | const buttonRef = React.useRef(null);
|
103 | const listboxRef = React.useRef(null);
|
104 | const Button = (_slots$root = slots.root) != null ? _slots$root : 'button';
|
105 | const ListboxRoot = (_slots$listbox = slots.listbox) != null ? _slots$listbox : 'ul';
|
106 | const PopperComponent = (_slots$popper = slots.popper) != null ? _slots$popper : _Popper.default;
|
107 | const handleButtonRefChange = React.useCallback(element => {
|
108 | setButtonDefined(element != null);
|
109 | }, []);
|
110 | const handleButtonRef = (0, _utils.unstable_useForkRef)(forwardedRef, buttonRef, handleButtonRefChange);
|
111 | React.useEffect(() => {
|
112 | if (autoFocus) {
|
113 | buttonRef.current.focus();
|
114 | }
|
115 | }, [autoFocus]);
|
116 | const {
|
117 | buttonActive,
|
118 | buttonFocusVisible,
|
119 | contextValue,
|
120 | disabled,
|
121 | getButtonProps,
|
122 | getListboxProps,
|
123 | getOptionMetadata,
|
124 | value,
|
125 | open
|
126 | } = (0, _useSelect.default)({
|
127 | buttonRef: handleButtonRef,
|
128 | defaultOpen: defaultListboxOpen,
|
129 | defaultValue,
|
130 | disabled: disabledProp,
|
131 | listboxId,
|
132 | multiple,
|
133 | open: listboxOpenProp,
|
134 | onChange,
|
135 | onOpenChange: onListboxOpenChange,
|
136 | getOptionAsString,
|
137 | value: valueProp
|
138 | });
|
139 | const ownerState = (0, _extends2.default)({}, props, {
|
140 | active: buttonActive,
|
141 | defaultListboxOpen,
|
142 | disabled,
|
143 | focusVisible: buttonFocusVisible,
|
144 | open,
|
145 | multiple,
|
146 | renderValue,
|
147 | value
|
148 | });
|
149 | const classes = useUtilityClasses(ownerState);
|
150 | const buttonProps = (0, _utils2.useSlotProps)({
|
151 | elementType: Button,
|
152 | getSlotProps: getButtonProps,
|
153 | externalSlotProps: slotProps.root,
|
154 | externalForwardedProps: other,
|
155 | ownerState,
|
156 | className: classes.root
|
157 | });
|
158 | const listboxProps = (0, _utils2.useSlotProps)({
|
159 | elementType: ListboxRoot,
|
160 | getSlotProps: getListboxProps,
|
161 | externalSlotProps: slotProps.listbox,
|
162 | additionalProps: {
|
163 | ref: listboxRef
|
164 | },
|
165 | ownerState,
|
166 | className: classes.listbox
|
167 | });
|
168 | const popperProps = (0, _utils2.useSlotProps)({
|
169 | elementType: PopperComponent,
|
170 | externalSlotProps: slotProps.popper,
|
171 | additionalProps: {
|
172 | anchorEl: buttonRef.current,
|
173 | keepMounted: true,
|
174 | open,
|
175 | placement: 'bottom-start',
|
176 | role: undefined
|
177 | },
|
178 | ownerState,
|
179 | className: classes.popper
|
180 | });
|
181 | let selectedOptionsMetadata;
|
182 | if (multiple) {
|
183 | selectedOptionsMetadata = value.map(v => getOptionMetadata(v)).filter(o => o !== undefined);
|
184 | } else {
|
185 | var _getOptionMetadata;
|
186 | selectedOptionsMetadata = (_getOptionMetadata = getOptionMetadata(value)) != null ? _getOptionMetadata : null;
|
187 | }
|
188 | return (0, _jsxRuntime.jsxs)(React.Fragment, {
|
189 | children: [(0, _jsxRuntime.jsx)(Button, (0, _extends2.default)({}, buttonProps, {
|
190 | children: renderValue(selectedOptionsMetadata)
|
191 | })), buttonDefined && (0, _jsxRuntime.jsx)(PopperComponent, (0, _extends2.default)({}, popperProps, {
|
192 | children: (0, _jsxRuntime.jsx)(ListboxRoot, (0, _extends2.default)({}, listboxProps, {
|
193 | children: (0, _jsxRuntime.jsx)(_SelectProvider.default, {
|
194 | value: contextValue,
|
195 | children: children
|
196 | })
|
197 | }))
|
198 | })), name && (0, _jsxRuntime.jsx)("input", {
|
199 | type: "hidden",
|
200 | name: name,
|
201 | value: getSerializedValue(selectedOptionsMetadata)
|
202 | })]
|
203 | });
|
204 | });
|
205 | process.env.NODE_ENV !== "production" ? Select.propTypes = {
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | |
211 |
|
212 |
|
213 |
|
214 | autoFocus: _propTypes.default.bool,
|
215 | |
216 |
|
217 |
|
218 | children: _propTypes.default.node,
|
219 | |
220 |
|
221 |
|
222 |
|
223 | defaultListboxOpen: _propTypes.default.bool,
|
224 | |
225 |
|
226 |
|
227 | defaultValue: _propTypes.default.any,
|
228 | |
229 |
|
230 |
|
231 |
|
232 | disabled: _propTypes.default.bool,
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | getOptionAsString: _propTypes.default.func,
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 | getSerializedValue: _propTypes.default.func,
|
247 | |
248 |
|
249 |
|
250 | listboxId: _propTypes.default.string,
|
251 | |
252 |
|
253 |
|
254 |
|
255 | listboxOpen: _propTypes.default.bool,
|
256 | |
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | multiple: _propTypes.default.bool,
|
263 | |
264 |
|
265 |
|
266 |
|
267 | name: _propTypes.default.string,
|
268 | |
269 |
|
270 |
|
271 | onChange: _propTypes.default.func,
|
272 | |
273 |
|
274 |
|
275 |
|
276 | onListboxOpenChange: _propTypes.default.func,
|
277 | |
278 |
|
279 |
|
280 | renderValue: _propTypes.default.func,
|
281 | |
282 |
|
283 |
|
284 |
|
285 | slotProps: _propTypes.default .shape({
|
286 | listbox: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]),
|
287 | popper: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]),
|
288 | root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
|
289 | }),
|
290 | |
291 |
|
292 |
|
293 |
|
294 |
|
295 | slots: _propTypes.default .shape({
|
296 | listbox: _propTypes.default.elementType,
|
297 | popper: _propTypes.default.elementType,
|
298 | root: _propTypes.default.elementType
|
299 | }),
|
300 | |
301 |
|
302 |
|
303 |
|
304 | value: _propTypes.default.any
|
305 | } : void 0;
|
306 | var _default = Select;
|
307 | exports.default = _default; |
\ | No newline at end of file |