UNPKG

12.1 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 _useSelect = _interopRequireDefault(require("../useSelect"));
14var _utils2 = require("../utils");
15var _Popper = _interopRequireDefault(require("../Popper"));
16var _composeClasses = _interopRequireDefault(require("../composeClasses"));
17var _selectClasses = require("./selectClasses");
18var _defaultOptionStringifier = _interopRequireDefault(require("../useSelect/defaultOptionStringifier"));
19var _ClassNameConfigurator = require("../utils/ClassNameConfigurator");
20var _SelectProvider = _interopRequireDefault(require("../useSelect/SelectProvider"));
21var _jsxRuntime = require("react/jsx-runtime");
22const _excluded = ["autoFocus", "children", "defaultValue", "defaultListboxOpen", "disabled", "getSerializedValue", "listboxId", "listboxOpen", "multiple", "name", "onChange", "onListboxOpenChange", "getOptionAsString", "renderValue", "slotProps", "slots", "value"];
23function _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); }
24function _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; }
25function defaultRenderValue(selectedOptions) {
26 var _selectedOptions$labe;
27 if (Array.isArray(selectedOptions)) {
28 return /*#__PURE__*/(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}
34function 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}
52function 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 * The foundation for building custom-styled select components.
69 *
70 * Demos:
71 *
72 * - [Select](https://mui.com/base/react-select/)
73 *
74 * API:
75 *
76 * - [Select API](https://mui.com/base/react-select/components-api/#select)
77 */
78const Select = /*#__PURE__*/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 /*#__PURE__*/(0, _jsxRuntime.jsxs)(React.Fragment, {
189 children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Button, (0, _extends2.default)({}, buttonProps, {
190 children: renderValue(selectedOptionsMetadata)
191 })), buttonDefined && /*#__PURE__*/(0, _jsxRuntime.jsx)(PopperComponent, (0, _extends2.default)({}, popperProps, {
192 children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ListboxRoot, (0, _extends2.default)({}, listboxProps, {
193 children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_SelectProvider.default, {
194 value: contextValue,
195 children: children
196 })
197 }))
198 })), name && /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
199 type: "hidden",
200 name: name,
201 value: getSerializedValue(selectedOptionsMetadata)
202 })]
203 });
204});
205process.env.NODE_ENV !== "production" ? Select.propTypes /* remove-proptypes */ = {
206 // ----------------------------- Warning --------------------------------
207 // | These PropTypes are generated from the TypeScript type definitions |
208 // | To update them edit TypeScript types and run "yarn proptypes" |
209 // ----------------------------------------------------------------------
210 /**
211 * If `true`, the select element is focused during the first mount
212 * @default false
213 */
214 autoFocus: _propTypes.default.bool,
215 /**
216 * @ignore
217 */
218 children: _propTypes.default.node,
219 /**
220 * If `true`, the select will be initially open.
221 * @default false
222 */
223 defaultListboxOpen: _propTypes.default.bool,
224 /**
225 * The default selected value. Use when the component is not controlled.
226 */
227 defaultValue: _propTypes.default.any,
228 /**
229 * If `true`, the select is disabled.
230 * @default false
231 */
232 disabled: _propTypes.default.bool,
233 /**
234 * A function used to convert the option label to a string.
235 * It's useful when labels are elements and need to be converted to plain text
236 * to enable navigation using character keys on a keyboard.
237 *
238 * @default defaultOptionStringifier
239 */
240 getOptionAsString: _propTypes.default.func,
241 /**
242 * A function to convert the currently selected value to a string.
243 * Used to set a value of a hidden input associated with the select,
244 * so that the selected value can be posted with a form.
245 */
246 getSerializedValue: _propTypes.default.func,
247 /**
248 * `id` attribute of the listbox element.
249 */
250 listboxId: _propTypes.default.string,
251 /**
252 * Controls the open state of the select's listbox.
253 * @default undefined
254 */
255 listboxOpen: _propTypes.default.bool,
256 /**
257 * If `true`, selecting multiple values is allowed.
258 * This affects the type of the `value`, `defaultValue`, and `onChange` props.
259 *
260 * @default false
261 */
262 multiple: _propTypes.default.bool,
263 /**
264 * Name of the element. For example used by the server to identify the fields in form submits.
265 * If the name is provided, the component will render a hidden input element that can be submitted to a server.
266 */
267 name: _propTypes.default.string,
268 /**
269 * Callback fired when an option is selected.
270 */
271 onChange: _propTypes.default.func,
272 /**
273 * Callback fired when the component requests to be opened.
274 * Use in controlled mode (see listboxOpen).
275 */
276 onListboxOpenChange: _propTypes.default.func,
277 /**
278 * Function that customizes the rendering of the selected value.
279 */
280 renderValue: _propTypes.default.func,
281 /**
282 * The props used for each slot inside the Input.
283 * @default {}
284 */
285 slotProps: _propTypes.default /* @typescript-to-proptypes-ignore */.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 * The components used for each slot inside the Select.
292 * Either a string to use a HTML element or a component.
293 * @default {}
294 */
295 slots: _propTypes.default /* @typescript-to-proptypes-ignore */.shape({
296 listbox: _propTypes.default.elementType,
297 popper: _propTypes.default.elementType,
298 root: _propTypes.default.elementType
299 }),
300 /**
301 * The selected value.
302 * Set to `null` to deselect all options.
303 */
304 value: _propTypes.default.any
305} : void 0;
306var _default = Select;
307exports.default = _default;
\No newline at end of file