UNPKG

6.53 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import NativeSelectInput from './NativeSelectInput';
6import withStyles from '../styles/withStyles';
7import formControlState from '../FormControl/formControlState';
8import useFormControl from '../FormControl/useFormControl';
9import ArrowDropDownIcon from '../internal/svg-icons/ArrowDropDown';
10import Input from '../Input';
11export var styles = function styles(theme) {
12 return {
13 /* Styles applied to the select component `root` class. */
14 root: {},
15
16 /* Styles applied to the select component `select` class. */
17 select: {
18 '-moz-appearance': 'none',
19 // Reset
20 '-webkit-appearance': 'none',
21 // Reset
22 // When interacting quickly, the text can end up selected.
23 // Native select can't be selected either.
24 userSelect: 'none',
25 borderRadius: 0,
26 // Reset
27 minWidth: 16,
28 // So it doesn't collapse.
29 cursor: 'pointer',
30 '&:focus': {
31 // Show that it's not an text input
32 backgroundColor: theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.05)' : 'rgba(255, 255, 255, 0.05)',
33 borderRadius: 0 // Reset Chrome style
34
35 },
36 // Remove IE 11 arrow
37 '&::-ms-expand': {
38 display: 'none'
39 },
40 '&$disabled': {
41 cursor: 'default'
42 },
43 '&[multiple]': {
44 height: 'auto'
45 },
46 '&:not([multiple]) option, &:not([multiple]) optgroup': {
47 backgroundColor: theme.palette.background.paper
48 },
49 '&&': {
50 paddingRight: 24
51 }
52 },
53
54 /* Styles applied to the select component if `variant="filled"`. */
55 filled: {
56 '&&': {
57 paddingRight: 32
58 }
59 },
60
61 /* Styles applied to the select component if `variant="outlined"`. */
62 outlined: {
63 borderRadius: theme.shape.borderRadius,
64 '&&': {
65 paddingRight: 32
66 }
67 },
68
69 /* Styles applied to the select component `selectMenu` class. */
70 selectMenu: {
71 height: 'auto',
72 // Resets for multpile select with chips
73 minHeight: '1.1876em',
74 // Required for select\text-field height consistency
75 textOverflow: 'ellipsis',
76 whiteSpace: 'nowrap',
77 overflow: 'hidden'
78 },
79
80 /* Pseudo-class applied to the select component `disabled` class. */
81 disabled: {},
82
83 /* Styles applied to the icon component. */
84 icon: {
85 // We use a position absolute over a flexbox in order to forward the pointer events
86 // to the input and to support wrapping tags..
87 position: 'absolute',
88 right: 0,
89 top: 'calc(50% - 12px)',
90 // Center vertically
91 pointerEvents: 'none',
92 // Don't block pointer events on the select under the icon.
93 color: theme.palette.action.active,
94 '&$disabled': {
95 color: theme.palette.action.disabled
96 }
97 },
98
99 /* Styles applied to the icon component if the popup is open. */
100 iconOpen: {
101 transform: 'rotate(180deg)'
102 },
103
104 /* Styles applied to the icon component if `variant="filled"`. */
105 iconFilled: {
106 right: 7
107 },
108
109 /* Styles applied to the icon component if `variant="outlined"`. */
110 iconOutlined: {
111 right: 7
112 },
113
114 /* Styles applied to the underlying native input component. */
115 nativeInput: {
116 bottom: 0,
117 left: 0,
118 position: 'absolute',
119 opacity: 0,
120 pointerEvents: 'none',
121 width: '100%'
122 }
123 };
124};
125var defaultInput = /*#__PURE__*/React.createElement(Input, null);
126/**
127 * An alternative to `<Select native />` with a much smaller bundle size footprint.
128 */
129
130var NativeSelect = /*#__PURE__*/React.forwardRef(function NativeSelect(props, ref) {
131 var children = props.children,
132 classes = props.classes,
133 _props$IconComponent = props.IconComponent,
134 IconComponent = _props$IconComponent === void 0 ? ArrowDropDownIcon : _props$IconComponent,
135 _props$input = props.input,
136 input = _props$input === void 0 ? defaultInput : _props$input,
137 inputProps = props.inputProps,
138 variant = props.variant,
139 other = _objectWithoutProperties(props, ["children", "classes", "IconComponent", "input", "inputProps", "variant"]);
140
141 var muiFormControl = useFormControl();
142 var fcs = formControlState({
143 props: props,
144 muiFormControl: muiFormControl,
145 states: ['variant']
146 });
147 return /*#__PURE__*/React.cloneElement(input, _extends({
148 // Most of the logic is implemented in `NativeSelectInput`.
149 // The `Select` component is a simple API wrapper to expose something better to play with.
150 inputComponent: NativeSelectInput,
151 inputProps: _extends({
152 children: children,
153 classes: classes,
154 IconComponent: IconComponent,
155 variant: fcs.variant,
156 type: undefined
157 }, inputProps, input ? input.props.inputProps : {}),
158 ref: ref
159 }, other));
160});
161process.env.NODE_ENV !== "production" ? NativeSelect.propTypes = {
162 // ----------------------------- Warning --------------------------------
163 // | These PropTypes are generated from the TypeScript type definitions |
164 // | To update them edit the d.ts file and run "yarn proptypes" |
165 // ----------------------------------------------------------------------
166
167 /**
168 * The option elements to populate the select with.
169 * Can be some `<option>` elements.
170 */
171 children: PropTypes.node,
172
173 /**
174 * Override or extend the styles applied to the component.
175 * See [CSS API](#css) below for more details.
176 */
177 classes: PropTypes.object,
178
179 /**
180 * The icon that displays the arrow.
181 */
182 IconComponent: PropTypes.elementType,
183
184 /**
185 * An `Input` element; does not have to be a material-ui specific `Input`.
186 */
187 input: PropTypes.element,
188
189 /**
190 * Attributes applied to the `select` element.
191 */
192 inputProps: PropTypes.object,
193
194 /**
195 * Callback function fired when a menu item is selected.
196 *
197 * @param {object} event The event source of the callback.
198 * You can pull out the new value by accessing `event.target.value` (string).
199 */
200 onChange: PropTypes.func,
201
202 /**
203 * The input value. The DOM API casts this to a string.
204 */
205 value: PropTypes.any,
206
207 /**
208 * The variant to use.
209 */
210 variant: PropTypes.oneOf(['filled', 'outlined', 'standard'])
211} : void 0;
212NativeSelect.muiName = 'Select';
213export default withStyles(styles, {
214 name: 'MuiNativeSelect'
215})(NativeSelect);
\No newline at end of file