UNPKG

9.43 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 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 variantProps = _props$variant === void 0 ? 'standard' : _props$variant,
72 _props$labelWidth = props.labelWidth,
73 labelWidth = _props$labelWidth === void 0 ? 0 : _props$labelWidth,
74 other = (0, _objectWithoutProperties2.default)(props, ["autoWidth", "children", "classes", "displayEmpty", "IconComponent", "id", "input", "inputProps", "labelId", "MenuProps", "multiple", "native", "onClose", "onOpen", "open", "renderValue", "SelectDisplayProps", "variant", "labelWidth"]);
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 select: true,
95 inputProps: (0, _extends2.default)({
96 children: children,
97 IconComponent: IconComponent,
98 variant: variant,
99 type: undefined,
100 // We render a select. We can ignore the type provided by the `Input`.
101 multiple: multiple
102 }, native ? {
103 id: id
104 } : {
105 autoWidth: autoWidth,
106 displayEmpty: displayEmpty,
107 labelId: labelId,
108 MenuProps: MenuProps,
109 onClose: onClose,
110 onOpen: onOpen,
111 open: open,
112 renderValue: renderValue,
113 SelectDisplayProps: (0, _extends2.default)({
114 id: id
115 }, SelectDisplayProps)
116 }, {}, inputProps, {
117 classes: inputProps ? (0, _styles.mergeClasses)({
118 baseClasses: classes,
119 newClasses: inputProps.classes,
120 Component: Select
121 }) : classes
122 }, input ? input.props.inputProps : {}),
123 ref: ref
124 }, other));
125});
126
127process.env.NODE_ENV !== "production" ? Select.propTypes = {
128 /**
129 * If true, the width of the popover will automatically be set according to the items inside the
130 * menu, otherwise it will be at least the width of the select input.
131 */
132 autoWidth: _propTypes.default.bool,
133
134 /**
135 * The option elements to populate the select with.
136 * Can be some `MenuItem` when `native` is false and `option` when `native` is true.
137 *
138 * ⚠️The `MenuItem` elements **must** be direct descendants when `native` is false.
139 */
140 children: _propTypes.default.node,
141
142 /**
143 * Override or extend the styles applied to the component.
144 * See [CSS API](#css) below for more details.
145 */
146 classes: _propTypes.default.object.isRequired,
147
148 /**
149 * The default element value. Use when the component is not controlled.
150 */
151 defaultValue: _propTypes.default.any,
152
153 /**
154 * If `true`, a value is displayed even if no items are selected.
155 *
156 * 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.
157 * You can only use it when the `native` prop is `false` (default).
158 */
159 displayEmpty: _propTypes.default.bool,
160
161 /**
162 * The icon that displays the arrow.
163 */
164 IconComponent: _propTypes.default.elementType,
165
166 /**
167 * @ignore
168 */
169 id: _propTypes.default.string,
170
171 /**
172 * An `Input` element; does not have to be a material-ui specific `Input`.
173 */
174 input: _propTypes.default.element,
175
176 /**
177 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
178 * When `native` is `true`, the attributes are applied on the `select` element.
179 */
180 inputProps: _propTypes.default.object,
181
182 /**
183 * The idea of an element that acts as an additional label. The Select will
184 * be labelled by the additional label and the selected value.
185 */
186 labelId: _propTypes.default.string,
187
188 /**
189 * The label width to be used on OutlinedInput.
190 * This prop is required when the `variant` prop is `outlined`.
191 */
192 labelWidth: _propTypes.default.number,
193
194 /**
195 * Props applied to the [`Menu`](/api/menu/) element.
196 */
197 MenuProps: _propTypes.default.object,
198
199 /**
200 * If true, `value` must be an array and the menu will support multiple selections.
201 */
202 multiple: _propTypes.default.bool,
203
204 /**
205 * If `true`, the component will be using a native `select` element.
206 */
207 native: _propTypes.default.bool,
208
209 /**
210 * Callback function fired when a menu item is selected.
211 *
212 * @param {object} event The event source of the callback.
213 * You can pull out the new value by accessing `event.target.value` (any).
214 * @param {object} [child] The react element that was selected when `native` is `false` (default).
215 */
216 onChange: _propTypes.default.func,
217
218 /**
219 * Callback fired when the component requests to be closed.
220 * Use in controlled mode (see open).
221 *
222 * @param {object} event The event source of the callback.
223 */
224 onClose: _propTypes.default.func,
225
226 /**
227 * Callback fired when the component requests to be opened.
228 * Use in controlled mode (see open).
229 *
230 * @param {object} event The event source of the callback.
231 */
232 onOpen: _propTypes.default.func,
233
234 /**
235 * Control `select` open state.
236 * You can only use it when the `native` prop is `false` (default).
237 */
238 open: _propTypes.default.bool,
239
240 /**
241 * Render the selected value.
242 * You can only use it when the `native` prop is `false` (default).
243 *
244 * @param {any} value The `value` provided to the component.
245 * @returns {ReactNode}
246 */
247 renderValue: _propTypes.default.func,
248
249 /**
250 * Props applied to the clickable div element.
251 */
252 SelectDisplayProps: _propTypes.default.object,
253
254 /**
255 * The input value. Providing an empty string will select no options.
256 * This prop is required when the `native` prop is `false` (default).
257 * Set to an empty string `''` if you don't want any of the available options to be selected.
258 *
259 * If the value is an object it must have reference equality with the option in order to be selected.
260 * If the value is not an object, the string representation must match with the string representation of the option in order to be selected.
261 */
262 value: _propTypes.default.any,
263
264 /**
265 * The variant to use.
266 */
267 variant: _propTypes.default.oneOf(['standard', 'outlined', 'filled'])
268} : void 0;
269Select.muiName = 'Select';
270
271var _default = (0, _withStyles.default)(styles, {
272 name: 'MuiSelect'
273})(Select);
274
275exports.default = _default;
\No newline at end of file