UNPKG

4.61 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import clsx from 'clsx';
5import PropTypes from 'prop-types';
6import composeClasses from '@mui/utils/composeClasses';
7import NativeSelectInput from "./NativeSelectInput.js";
8import formControlState from "../FormControl/formControlState.js";
9import useFormControl from "../FormControl/useFormControl.js";
10import ArrowDropDownIcon from "../internal/svg-icons/ArrowDropDown.js";
11import Input from "../Input/index.js";
12import { useDefaultProps } from "../DefaultPropsProvider/index.js";
13import { getNativeSelectUtilityClasses } from "./nativeSelectClasses.js";
14import { jsx as _jsx } from "react/jsx-runtime";
15const useUtilityClasses = ownerState => {
16 const {
17 classes
18 } = ownerState;
19 const slots = {
20 root: ['root']
21 };
22 return composeClasses(slots, getNativeSelectUtilityClasses, classes);
23};
24const defaultInput = /*#__PURE__*/_jsx(Input, {});
25/**
26 * An alternative to `<Select native />` with a much smaller bundle size footprint.
27 */
28const NativeSelect = /*#__PURE__*/React.forwardRef(function NativeSelect(inProps, ref) {
29 const props = useDefaultProps({
30 name: 'MuiNativeSelect',
31 props: inProps
32 });
33 const {
34 className,
35 children,
36 classes: classesProp = {},
37 IconComponent = ArrowDropDownIcon,
38 input = defaultInput,
39 inputProps,
40 variant,
41 ...other
42 } = props;
43 const muiFormControl = useFormControl();
44 const fcs = formControlState({
45 props,
46 muiFormControl,
47 states: ['variant']
48 });
49 const ownerState = {
50 ...props,
51 classes: classesProp
52 };
53 const classes = useUtilityClasses(ownerState);
54 const {
55 root,
56 ...otherClasses
57 } = classesProp;
58 return /*#__PURE__*/_jsx(React.Fragment, {
59 children: /*#__PURE__*/React.cloneElement(input, {
60 // Most of the logic is implemented in `NativeSelectInput`.
61 // The `Select` component is a simple API wrapper to expose something better to play with.
62 inputComponent: NativeSelectInput,
63 inputProps: {
64 children,
65 classes: otherClasses,
66 IconComponent,
67 variant: fcs.variant,
68 type: undefined,
69 // We render a select. We can ignore the type provided by the `Input`.
70 ...inputProps,
71 ...(input ? input.props.inputProps : {})
72 },
73 ref,
74 ...other,
75 className: clsx(classes.root, input.props.className, className)
76 })
77 });
78});
79process.env.NODE_ENV !== "production" ? NativeSelect.propTypes /* remove-proptypes */ = {
80 // ┌────────────────────────────── Warning ──────────────────────────────┐
81 // │ These PropTypes are generated from the TypeScript type definitions. │
82 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
83 // └─────────────────────────────────────────────────────────────────────┘
84 /**
85 * The option elements to populate the select with.
86 * Can be some `<option>` elements.
87 */
88 children: PropTypes.node,
89 /**
90 * Override or extend the styles applied to the component.
91 * @default {}
92 */
93 classes: PropTypes.object,
94 /**
95 * @ignore
96 */
97 className: PropTypes.string,
98 /**
99 * The icon that displays the arrow.
100 * @default ArrowDropDownIcon
101 */
102 IconComponent: PropTypes.elementType,
103 /**
104 * An `Input` element; does not have to be a material-ui specific `Input`.
105 * @default <Input />
106 */
107 input: PropTypes.element,
108 /**
109 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#attributes) applied to the `select` element.
110 */
111 inputProps: PropTypes.object,
112 /**
113 * Callback fired when a menu item is selected.
114 *
115 * @param {React.ChangeEvent<HTMLSelectElement>} event The event source of the callback.
116 * You can pull out the new value by accessing `event.target.value` (string).
117 */
118 onChange: PropTypes.func,
119 /**
120 * The system prop that allows defining system overrides as well as additional CSS styles.
121 */
122 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
123 /**
124 * The `input` value. The DOM API casts this to a string.
125 */
126 value: PropTypes.any,
127 /**
128 * The variant to use.
129 */
130 variant: PropTypes.oneOf(['filled', 'outlined', 'standard'])
131} : void 0;
132NativeSelect.muiName = 'Select';
133export default NativeSelect;
\No newline at end of file