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