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