UNPKG

9.74 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
16var React = _interopRequireWildcard(require("react"));
17
18var _propTypes = _interopRequireDefault(require("prop-types"));
19
20var _clsx = _interopRequireDefault(require("clsx"));
21
22var _utils = require("../InputBase/utils");
23
24var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
25
26var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
27
28var _isMuiElement = _interopRequireDefault(require("../utils/isMuiElement"));
29
30var _FormControlContext = _interopRequireDefault(require("./FormControlContext"));
31
32var styles = {
33 /* Styles applied to the root element. */
34 root: {
35 display: 'inline-flex',
36 flexDirection: 'column',
37 position: 'relative',
38 // Reset fieldset default style.
39 minWidth: 0,
40 padding: 0,
41 margin: 0,
42 border: 0,
43 verticalAlign: 'top' // Fix alignment issue on Safari.
44
45 },
46
47 /* Styles applied to the root element if `margin="normal"`. */
48 marginNormal: {
49 marginTop: 16,
50 marginBottom: 8
51 },
52
53 /* Styles applied to the root element if `margin="dense"`. */
54 marginDense: {
55 marginTop: 8,
56 marginBottom: 4
57 },
58
59 /* Styles applied to the root element if `fullWidth={true}`. */
60 fullWidth: {
61 width: '100%'
62 }
63};
64/**
65 * Provides context such as filled/focused/error/required for form inputs.
66 * Relying on the context provides high flexibility and ensures that the state always stays
67 * consistent across the children of the `FormControl`.
68 * This context is used by the following components:
69 *
70 * - FormLabel
71 * - FormHelperText
72 * - Input
73 * - InputLabel
74 *
75 * You can find one composition example below and more going to [the demos](/components/text-fields/#components).
76 *
77 * ```jsx
78 * <FormControl>
79 * <InputLabel htmlFor="my-input">Email address</InputLabel>
80 * <Input id="my-input" aria-describedby="my-helper-text" />
81 * <FormHelperText id="my-helper-text">We'll never share your email.</FormHelperText>
82 * </FormControl>
83 * ```
84 *
85 * ⚠️Only one input can be used within a FormControl.
86 */
87
88exports.styles = styles;
89var FormControl = /*#__PURE__*/React.forwardRef(function FormControl(props, ref) {
90 var children = props.children,
91 classes = props.classes,
92 className = props.className,
93 _props$color = props.color,
94 color = _props$color === void 0 ? 'primary' : _props$color,
95 _props$component = props.component,
96 Component = _props$component === void 0 ? 'div' : _props$component,
97 _props$disabled = props.disabled,
98 disabled = _props$disabled === void 0 ? false : _props$disabled,
99 _props$error = props.error,
100 error = _props$error === void 0 ? false : _props$error,
101 _props$fullWidth = props.fullWidth,
102 fullWidth = _props$fullWidth === void 0 ? false : _props$fullWidth,
103 visuallyFocused = props.focused,
104 _props$hiddenLabel = props.hiddenLabel,
105 hiddenLabel = _props$hiddenLabel === void 0 ? false : _props$hiddenLabel,
106 _props$margin = props.margin,
107 margin = _props$margin === void 0 ? 'none' : _props$margin,
108 _props$required = props.required,
109 required = _props$required === void 0 ? false : _props$required,
110 size = props.size,
111 _props$variant = props.variant,
112 variant = _props$variant === void 0 ? 'standard' : _props$variant,
113 other = (0, _objectWithoutProperties2.default)(props, ["children", "classes", "className", "color", "component", "disabled", "error", "fullWidth", "focused", "hiddenLabel", "margin", "required", "size", "variant"]);
114
115 var _React$useState = React.useState(function () {
116 // We need to iterate through the children and find the Input in order
117 // to fully support server-side rendering.
118 var initialAdornedStart = false;
119
120 if (children) {
121 React.Children.forEach(children, function (child) {
122 if (!(0, _isMuiElement.default)(child, ['Input', 'Select'])) {
123 return;
124 }
125
126 var input = (0, _isMuiElement.default)(child, ['Select']) ? child.props.input : child;
127
128 if (input && (0, _utils.isAdornedStart)(input.props)) {
129 initialAdornedStart = true;
130 }
131 });
132 }
133
134 return initialAdornedStart;
135 }),
136 adornedStart = _React$useState[0],
137 setAdornedStart = _React$useState[1];
138
139 var _React$useState2 = React.useState(function () {
140 // We need to iterate through the children and find the Input in order
141 // to fully support server-side rendering.
142 var initialFilled = false;
143
144 if (children) {
145 React.Children.forEach(children, function (child) {
146 if (!(0, _isMuiElement.default)(child, ['Input', 'Select'])) {
147 return;
148 }
149
150 if ((0, _utils.isFilled)(child.props, true)) {
151 initialFilled = true;
152 }
153 });
154 }
155
156 return initialFilled;
157 }),
158 filled = _React$useState2[0],
159 setFilled = _React$useState2[1];
160
161 var _React$useState3 = React.useState(false),
162 _focused = _React$useState3[0],
163 setFocused = _React$useState3[1];
164
165 var focused = visuallyFocused !== undefined ? visuallyFocused : _focused;
166
167 if (disabled && focused) {
168 setFocused(false);
169 }
170
171 var registerEffect;
172
173 if (process.env.NODE_ENV !== 'production') {
174 // eslint-disable-next-line react-hooks/rules-of-hooks
175 var registeredInput = React.useRef(false);
176
177 registerEffect = function registerEffect() {
178 if (registeredInput.current) {
179 console.error(['Material-UI: There are multiple InputBase components inside a FormControl.', 'This is not supported. It might cause infinite rendering loops.', 'Only use one InputBase.'].join('\n'));
180 }
181
182 registeredInput.current = true;
183 return function () {
184 registeredInput.current = false;
185 };
186 };
187 }
188
189 var onFilled = React.useCallback(function () {
190 setFilled(true);
191 }, []);
192 var onEmpty = React.useCallback(function () {
193 setFilled(false);
194 }, []);
195 var childContext = {
196 adornedStart: adornedStart,
197 setAdornedStart: setAdornedStart,
198 color: color,
199 disabled: disabled,
200 error: error,
201 filled: filled,
202 focused: focused,
203 fullWidth: fullWidth,
204 hiddenLabel: hiddenLabel,
205 margin: (size === 'small' ? 'dense' : undefined) || margin,
206 onBlur: function onBlur() {
207 setFocused(false);
208 },
209 onEmpty: onEmpty,
210 onFilled: onFilled,
211 onFocus: function onFocus() {
212 setFocused(true);
213 },
214 registerEffect: registerEffect,
215 required: required,
216 variant: variant
217 };
218 return /*#__PURE__*/React.createElement(_FormControlContext.default.Provider, {
219 value: childContext
220 }, /*#__PURE__*/React.createElement(Component, (0, _extends2.default)({
221 className: (0, _clsx.default)(classes.root, className, margin !== 'none' && classes["margin".concat((0, _capitalize.default)(margin))], fullWidth && classes.fullWidth),
222 ref: ref
223 }, other), children));
224});
225process.env.NODE_ENV !== "production" ? FormControl.propTypes = {
226 // ----------------------------- Warning --------------------------------
227 // | These PropTypes are generated from the TypeScript type definitions |
228 // | To update them edit the d.ts file and run "yarn proptypes" |
229 // ----------------------------------------------------------------------
230
231 /**
232 * The contents of the form control.
233 */
234 children: _propTypes.default.node,
235
236 /**
237 * Override or extend the styles applied to the component.
238 * See [CSS API](#css) below for more details.
239 */
240 classes: _propTypes.default.object,
241
242 /**
243 * @ignore
244 */
245 className: _propTypes.default.string,
246
247 /**
248 * The color of the component. It supports those theme colors that make sense for this component.
249 */
250 color: _propTypes.default.oneOf(['primary', 'secondary']),
251
252 /**
253 * The component used for the root node.
254 * Either a string to use a HTML element or a component.
255 */
256 component: _propTypes.default
257 /* @typescript-to-proptypes-ignore */
258 .elementType,
259
260 /**
261 * If `true`, the label, input and helper text should be displayed in a disabled state.
262 */
263 disabled: _propTypes.default.bool,
264
265 /**
266 * If `true`, the label should be displayed in an error state.
267 */
268 error: _propTypes.default.bool,
269
270 /**
271 * If `true`, the component will be displayed in focused state.
272 */
273 focused: _propTypes.default.bool,
274
275 /**
276 * If `true`, the component will take up the full width of its container.
277 */
278 fullWidth: _propTypes.default.bool,
279
280 /**
281 * If `true`, the label will be hidden.
282 * This is used to increase density for a `FilledInput`.
283 * Be sure to add `aria-label` to the `input` element.
284 */
285 hiddenLabel: _propTypes.default.bool,
286
287 /**
288 * If `dense` or `normal`, will adjust vertical spacing of this and contained components.
289 */
290 margin: _propTypes.default.oneOf(['dense', 'none', 'normal']),
291
292 /**
293 * If `true`, the label will indicate that the input is required.
294 */
295 required: _propTypes.default.bool,
296
297 /**
298 * The size of the text field.
299 */
300 size: _propTypes.default.oneOf(['medium', 'small']),
301
302 /**
303 * The variant to use.
304 */
305 variant: _propTypes.default.oneOf(['filled', 'outlined', 'standard'])
306} : void 0;
307
308var _default = (0, _withStyles.default)(styles, {
309 name: 'MuiFormControl'
310})(FormControl);
311
312exports.default = _default;
\No newline at end of file