UNPKG

9.41 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = exports.styles = void 0;
9
10var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
12var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
14var _react = _interopRequireDefault(require("react"));
15
16var _propTypes = _interopRequireDefault(require("prop-types"));
17
18var _styles = require("@material-ui/styles");
19
20var _SelectInput = _interopRequireDefault(require("./SelectInput"));
21
22var _formControlState = _interopRequireDefault(require("../FormControl/formControlState"));
23
24var _useFormControl = _interopRequireDefault(require("../FormControl/useFormControl"));
25
26var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
27
28var _ArrowDropDown = _interopRequireDefault(require("../internal/svg-icons/ArrowDropDown"));
29
30var _Input = _interopRequireDefault(require("../Input"));
31
32var _NativeSelect = require("../NativeSelect/NativeSelect");
33
34var _NativeSelectInput = _interopRequireDefault(require("../NativeSelect/NativeSelectInput"));
35
36var _FilledInput = _interopRequireDefault(require("../FilledInput"));
37
38var _OutlinedInput = _interopRequireDefault(require("../OutlinedInput"));
39
40var styles = _NativeSelect.styles;
41exports.styles = styles;
42
43var _ref = _react.default.createElement(_Input.default, null);
44
45var _ref2 = _react.default.createElement(_FilledInput.default, null);
46
47var Select = _react.default.forwardRef(function Select(props, ref) {
48 var _props$autoWidth = props.autoWidth,
49 autoWidth = _props$autoWidth === void 0 ? false : _props$autoWidth,
50 children = props.children,
51 classes = props.classes,
52 _props$displayEmpty = props.displayEmpty,
53 displayEmpty = _props$displayEmpty === void 0 ? false : _props$displayEmpty,
54 _props$IconComponent = props.IconComponent,
55 IconComponent = _props$IconComponent === void 0 ? _ArrowDropDown.default : _props$IconComponent,
56 id = props.id,
57 input = props.input,
58 inputProps = props.inputProps,
59 labelId = props.labelId,
60 _props$labelWidth = props.labelWidth,
61 labelWidth = _props$labelWidth === void 0 ? 0 : _props$labelWidth,
62 MenuProps = props.MenuProps,
63 _props$multiple = props.multiple,
64 multiple = _props$multiple === void 0 ? false : _props$multiple,
65 _props$native = props.native,
66 native = _props$native === void 0 ? false : _props$native,
67 onClose = props.onClose,
68 onOpen = props.onOpen,
69 open = props.open,
70 renderValue = props.renderValue,
71 SelectDisplayProps = props.SelectDisplayProps,
72 _props$variant = props.variant,
73 variantProps = _props$variant === void 0 ? 'standard' : _props$variant,
74 other = (0, _objectWithoutProperties2.default)(props, ["autoWidth", "children", "classes", "displayEmpty", "IconComponent", "id", "input", "inputProps", "labelId", "labelWidth", "MenuProps", "multiple", "native", "onClose", "onOpen", "open", "renderValue", "SelectDisplayProps", "variant"]);
75 var inputComponent = native ? _NativeSelectInput.default : _SelectInput.default;
76 var muiFormControl = (0, _useFormControl.default)();
77 var fcs = (0, _formControlState.default)({
78 props: props,
79 muiFormControl: muiFormControl,
80 states: ['variant']
81 });
82 var variant = fcs.variant || variantProps;
83 var InputComponent = input || {
84 standard: _ref,
85 outlined: _react.default.createElement(_OutlinedInput.default, {
86 labelWidth: labelWidth
87 }),
88 filled: _ref2
89 }[variant];
90 return _react.default.cloneElement(InputComponent, (0, _extends2.default)({
91 // Most of the logic is implemented in `SelectInput`.
92 // The `Select` component is a simple API wrapper to expose something better to play with.
93 inputComponent: inputComponent,
94 inputProps: (0, _extends2.default)({
95 children: children,
96 IconComponent: IconComponent,
97 variant: variant,
98 type: undefined,
99 // We render a select. We can ignore the type provided by the `Input`.
100 multiple: multiple
101 }, native ? {
102 id: id
103 } : {
104 autoWidth: autoWidth,
105 displayEmpty: displayEmpty,
106 labelId: labelId,
107 MenuProps: MenuProps,
108 onClose: onClose,
109 onOpen: onOpen,
110 open: open,
111 renderValue: renderValue,
112 SelectDisplayProps: (0, _extends2.default)({
113 id: id
114 }, SelectDisplayProps)
115 }, {}, inputProps, {
116 classes: inputProps ? (0, _styles.mergeClasses)({
117 baseClasses: classes,
118 newClasses: inputProps.classes,
119 Component: Select
120 }) : classes
121 }, input ? input.props.inputProps : {}),
122 ref: ref
123 }, other));
124});
125
126process.env.NODE_ENV !== "production" ? Select.propTypes = {
127 /**
128 * If `true`, the width of the popover will automatically be set according to the items inside the
129 * menu, otherwise it will be at least the width of the select input.
130 */
131 autoWidth: _propTypes.default.bool,
132
133 /**
134 * The option elements to populate the select with.
135 * Can be some `MenuItem` when `native` is false and `option` when `native` is true.
136 *
137 * ⚠️The `MenuItem` elements **must** be direct descendants when `native` is false.
138 */
139 children: _propTypes.default.node,
140
141 /**
142 * Override or extend the styles applied to the component.
143 * See [CSS API](#css) below for more details.
144 */
145 classes: _propTypes.default.object.isRequired,
146
147 /**
148 * The default element value. Use when the component is not controlled.
149 */
150 defaultValue: _propTypes.default.any,
151
152 /**
153 * If `true`, a value is displayed even if no items are selected.
154 *
155 * In order to display a meaningful value, a function should be passed to the `renderValue` prop which returns the value to be displayed when no items are selected.
156 * You can only use it when the `native` prop is `false` (default).
157 */
158 displayEmpty: _propTypes.default.bool,
159
160 /**
161 * The icon that displays the arrow.
162 */
163 IconComponent: _propTypes.default.elementType,
164
165 /**
166 * @ignore
167 */
168 id: _propTypes.default.string,
169
170 /**
171 * An `Input` element; does not have to be a material-ui specific `Input`.
172 */
173 input: _propTypes.default.element,
174
175 /**
176 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
177 * When `native` is `true`, the attributes are applied on the `select` element.
178 */
179 inputProps: _propTypes.default.object,
180
181 /**
182 * The idea of an element that acts as an additional label. The Select will
183 * be labelled by the additional label and the selected value.
184 */
185 labelId: _propTypes.default.string,
186
187 /**
188 * The label width to be used on OutlinedInput.
189 * This prop is required when the `variant` prop is `outlined`.
190 */
191 labelWidth: _propTypes.default.number,
192
193 /**
194 * Props applied to the [`Menu`](/api/menu/) element.
195 */
196 MenuProps: _propTypes.default.object,
197
198 /**
199 * If `true`, `value` must be an array and the menu will support multiple selections.
200 */
201 multiple: _propTypes.default.bool,
202
203 /**
204 * If `true`, the component will be using a native `select` element.
205 */
206 native: _propTypes.default.bool,
207
208 /**
209 * Callback function fired when a menu item is selected.
210 *
211 * @param {object} event The event source of the callback.
212 * You can pull out the new value by accessing `event.target.value` (any).
213 * @param {object} [child] The react element that was selected when `native` is `false` (default).
214 */
215 onChange: _propTypes.default.func,
216
217 /**
218 * Callback fired when the component requests to be closed.
219 * Use in controlled mode (see open).
220 *
221 * @param {object} event The event source of the callback.
222 */
223 onClose: _propTypes.default.func,
224
225 /**
226 * Callback fired when the component requests to be opened.
227 * Use in controlled mode (see open).
228 *
229 * @param {object} event The event source of the callback.
230 */
231 onOpen: _propTypes.default.func,
232
233 /**
234 * Control `select` open state.
235 * You can only use it when the `native` prop is `false` (default).
236 */
237 open: _propTypes.default.bool,
238
239 /**
240 * Render the selected value.
241 * You can only use it when the `native` prop is `false` (default).
242 *
243 * @param {any} value The `value` provided to the component.
244 * @returns {ReactNode}
245 */
246 renderValue: _propTypes.default.func,
247
248 /**
249 * Props applied to the clickable div element.
250 */
251 SelectDisplayProps: _propTypes.default.object,
252
253 /**
254 * The input value. Providing an empty string will select no options.
255 * This prop is required when the `native` prop is `false` (default).
256 * Set to an empty string `''` if you don't want any of the available options to be selected.
257 *
258 * If the value is an object it must have reference equality with the option in order to be selected.
259 * If the value is not an object, the string representation must match with the string representation of the option in order to be selected.
260 */
261 value: _propTypes.default.any,
262
263 /**
264 * The variant to use.
265 */
266 variant: _propTypes.default.oneOf(['standard', 'outlined', 'filled'])
267} : void 0;
268Select.muiName = 'Select';
269
270var _default = (0, _withStyles.default)(styles, {
271 name: 'MuiSelect'
272})(Select);
273
274exports.default = _default;
\No newline at end of file