UNPKG

11.6 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
5import * as React from 'react';
6import PropTypes from 'prop-types';
7import clsx from 'clsx';
8import deepmerge from '@mui/utils/deepmerge';
9import SelectInput from './SelectInput';
10import formControlState from '../FormControl/formControlState';
11import useFormControl from '../FormControl/useFormControl';
12import ArrowDropDownIcon from '../internal/svg-icons/ArrowDropDown';
13import Input from '../Input';
14import NativeSelectInput from '../NativeSelect/NativeSelectInput';
15import FilledInput from '../FilledInput';
16import OutlinedInput from '../OutlinedInput';
17import { useDefaultProps } from '../DefaultPropsProvider';
18import useForkRef from '../utils/useForkRef';
19import styled, { rootShouldForwardProp } from '../styles/styled';
20import { jsx as _jsx } from "react/jsx-runtime";
21var useUtilityClasses = function useUtilityClasses(ownerState) {
22 var classes = ownerState.classes;
23 return classes;
24};
25var styledRootConfig = {
26 name: 'MuiSelect',
27 overridesResolver: function overridesResolver(props, styles) {
28 return styles.root;
29 },
30 shouldForwardProp: function shouldForwardProp(prop) {
31 return rootShouldForwardProp(prop) && prop !== 'variant';
32 },
33 slot: 'Root'
34};
35var StyledInput = styled(Input, styledRootConfig)('');
36var StyledOutlinedInput = styled(OutlinedInput, styledRootConfig)('');
37var StyledFilledInput = styled(FilledInput, styledRootConfig)('');
38var Select = /*#__PURE__*/React.forwardRef(function Select(inProps, ref) {
39 var props = useDefaultProps({
40 name: 'MuiSelect',
41 props: inProps
42 });
43 var _props$autoWidth = props.autoWidth,
44 autoWidth = _props$autoWidth === void 0 ? false : _props$autoWidth,
45 children = props.children,
46 _props$classes = props.classes,
47 classesProp = _props$classes === void 0 ? {} : _props$classes,
48 className = props.className,
49 _props$defaultOpen = props.defaultOpen,
50 defaultOpen = _props$defaultOpen === void 0 ? false : _props$defaultOpen,
51 _props$displayEmpty = props.displayEmpty,
52 displayEmpty = _props$displayEmpty === void 0 ? false : _props$displayEmpty,
53 _props$IconComponent = props.IconComponent,
54 IconComponent = _props$IconComponent === void 0 ? ArrowDropDownIcon : _props$IconComponent,
55 id = props.id,
56 input = props.input,
57 inputProps = props.inputProps,
58 label = props.label,
59 labelId = props.labelId,
60 MenuProps = props.MenuProps,
61 _props$multiple = props.multiple,
62 multiple = _props$multiple === void 0 ? false : _props$multiple,
63 _props$native = props.native,
64 native = _props$native === void 0 ? false : _props$native,
65 onClose = props.onClose,
66 onOpen = props.onOpen,
67 open = props.open,
68 renderValue = props.renderValue,
69 SelectDisplayProps = props.SelectDisplayProps,
70 _props$variant = props.variant,
71 variantProp = _props$variant === void 0 ? 'outlined' : _props$variant,
72 other = _objectWithoutProperties(props, ["autoWidth", "children", "classes", "className", "defaultOpen", "displayEmpty", "IconComponent", "id", "input", "inputProps", "label", "labelId", "MenuProps", "multiple", "native", "onClose", "onOpen", "open", "renderValue", "SelectDisplayProps", "variant"]);
73 var inputComponent = native ? NativeSelectInput : SelectInput;
74 var muiFormControl = useFormControl();
75 var fcs = formControlState({
76 props: props,
77 muiFormControl: muiFormControl,
78 states: ['variant', 'error']
79 });
80 var variant = fcs.variant || variantProp;
81 var ownerState = _extends({}, props, {
82 variant: variant,
83 classes: classesProp
84 });
85 var classes = useUtilityClasses(ownerState);
86 var root = classes.root,
87 restOfClasses = _objectWithoutProperties(classes, ["root"]);
88 var InputComponent = input || {
89 standard: /*#__PURE__*/_jsx(StyledInput, {
90 ownerState: ownerState
91 }),
92 outlined: /*#__PURE__*/_jsx(StyledOutlinedInput, {
93 label: label,
94 ownerState: ownerState
95 }),
96 filled: /*#__PURE__*/_jsx(StyledFilledInput, {
97 ownerState: ownerState
98 })
99 }[variant];
100 var inputComponentRef = useForkRef(ref, InputComponent.ref);
101 return /*#__PURE__*/_jsx(React.Fragment, {
102 children: /*#__PURE__*/React.cloneElement(InputComponent, _extends({
103 // Most of the logic is implemented in `SelectInput`.
104 // The `Select` component is a simple API wrapper to expose something better to play with.
105 inputComponent: inputComponent,
106 inputProps: _extends({
107 children: children,
108 error: fcs.error,
109 IconComponent: IconComponent,
110 variant: variant,
111 type: undefined,
112 // We render a select. We can ignore the type provided by the `Input`.
113 multiple: multiple
114 }, native ? {
115 id: id
116 } : {
117 autoWidth: autoWidth,
118 defaultOpen: defaultOpen,
119 displayEmpty: displayEmpty,
120 labelId: labelId,
121 MenuProps: MenuProps,
122 onClose: onClose,
123 onOpen: onOpen,
124 open: open,
125 renderValue: renderValue,
126 SelectDisplayProps: _extends({
127 id: id
128 }, SelectDisplayProps)
129 }, inputProps, {
130 classes: inputProps ? deepmerge(restOfClasses, inputProps.classes) : restOfClasses
131 }, input ? input.props.inputProps : {})
132 }, (multiple && native || displayEmpty) && variant === 'outlined' ? {
133 notched: true
134 } : {}, {
135 ref: inputComponentRef,
136 className: clsx(InputComponent.props.className, className, classes.root)
137 }, !input && {
138 variant: variant
139 }, other))
140 });
141});
142process.env.NODE_ENV !== "production" ? Select.propTypes /* remove-proptypes */ = {
143 // ┌────────────────────────────── Warning ──────────────────────────────┐
144 // │ These PropTypes are generated from the TypeScript type definitions. │
145 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
146 // └─────────────────────────────────────────────────────────────────────┘
147 /**
148 * If `true`, the width of the popover will automatically be set according to the items inside the
149 * menu, otherwise it will be at least the width of the select input.
150 * @default false
151 */
152 autoWidth: PropTypes.bool,
153 /**
154 * The option elements to populate the select with.
155 * Can be some `MenuItem` when `native` is false and `option` when `native` is true.
156 *
157 * ⚠️The `MenuItem` elements **must** be direct descendants when `native` is false.
158 */
159 children: PropTypes.node,
160 /**
161 * Override or extend the styles applied to the component.
162 * @default {}
163 */
164 classes: PropTypes.object,
165 /**
166 * @ignore
167 */
168 className: PropTypes.string,
169 /**
170 * If `true`, the component is initially open. Use when the component open state is not controlled (i.e. the `open` prop is not defined).
171 * You can only use it when the `native` prop is `false` (default).
172 * @default false
173 */
174 defaultOpen: PropTypes.bool,
175 /**
176 * The default value. Use when the component is not controlled.
177 */
178 defaultValue: PropTypes.any,
179 /**
180 * If `true`, a value is displayed even if no items are selected.
181 *
182 * In order to display a meaningful value, a function can be passed to the `renderValue` prop which
183 * returns the value to be displayed when no items are selected.
184 *
185 * ⚠️ When using this prop, make sure the label doesn't overlap with the empty displayed value.
186 * The label should either be hidden or forced to a shrunk state.
187 * @default false
188 */
189 displayEmpty: PropTypes.bool,
190 /**
191 * The icon that displays the arrow.
192 * @default ArrowDropDownIcon
193 */
194 IconComponent: PropTypes.elementType,
195 /**
196 * The `id` of the wrapper element or the `select` element when `native`.
197 */
198 id: PropTypes.string,
199 /**
200 * An `Input` element; does not have to be a material-ui specific `Input`.
201 */
202 input: PropTypes.element,
203 /**
204 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
205 * When `native` is `true`, the attributes are applied on the `select` element.
206 */
207 inputProps: PropTypes.object,
208 /**
209 * See [OutlinedInput#label](/material-ui/api/outlined-input/#props)
210 */
211 label: PropTypes.node,
212 /**
213 * The ID of an element that acts as an additional label. The Select will
214 * be labelled by the additional label and the selected value.
215 */
216 labelId: PropTypes.string,
217 /**
218 * Props applied to the [`Menu`](/material-ui/api/menu/) element.
219 */
220 MenuProps: PropTypes.object,
221 /**
222 * If `true`, `value` must be an array and the menu will support multiple selections.
223 * @default false
224 */
225 multiple: PropTypes.bool,
226 /**
227 * If `true`, the component uses a native `select` element.
228 * @default false
229 */
230 native: PropTypes.bool,
231 /**
232 * Callback fired when a menu item is selected.
233 *
234 * @param {SelectChangeEvent<Value>} event The event source of the callback.
235 * You can pull out the new value by accessing `event.target.value` (any).
236 * **Warning**: This is a generic event, not a change event, unless the change event is caused by browser autofill.
237 * @param {object} [child] The react element that was selected when `native` is `false` (default).
238 */
239 onChange: PropTypes.func,
240 /**
241 * Callback fired when the component requests to be closed.
242 * Use it in either controlled (see the `open` prop), or uncontrolled mode (to detect when the Select collapses).
243 *
244 * @param {object} event The event source of the callback.
245 */
246 onClose: PropTypes.func,
247 /**
248 * Callback fired when the component requests to be opened.
249 * Use it in either controlled (see the `open` prop), or uncontrolled mode (to detect when the Select expands).
250 *
251 * @param {object} event The event source of the callback.
252 */
253 onOpen: PropTypes.func,
254 /**
255 * If `true`, the component is shown.
256 * You can only use it when the `native` prop is `false` (default).
257 */
258 open: PropTypes.bool,
259 /**
260 * Render the selected value.
261 * You can only use it when the `native` prop is `false` (default).
262 *
263 * @param {any} value The `value` provided to the component.
264 * @returns {ReactNode}
265 */
266 renderValue: PropTypes.func,
267 /**
268 * Props applied to the clickable div element.
269 */
270 SelectDisplayProps: PropTypes.object,
271 /**
272 * The system prop that allows defining system overrides as well as additional CSS styles.
273 */
274 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
275 /**
276 * The `input` value. Providing an empty string will select no options.
277 * Set to an empty string `''` if you don't want any of the available options to be selected.
278 *
279 * If the value is an object it must have reference equality with the option in order to be selected.
280 * If the value is not an object, the string representation must match with the string representation of the option in order to be selected.
281 */
282 value: PropTypes.oneOfType([PropTypes.oneOf(['']), PropTypes.any]),
283 /**
284 * The variant to use.
285 * @default 'outlined'
286 */
287 variant: PropTypes.oneOf(['filled', 'outlined', 'standard'])
288} : void 0;
289Select.muiName = 'Select';
290export default Select;
\No newline at end of file