-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathFormControl.js
351 lines (326 loc) · 9.33 KB
/
FormControl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import { styled } from '../zero-styled';
import { useDefaultProps } from '../DefaultPropsProvider';
import { isFilled, isAdornedStart } from '../InputBase/utils';
import capitalize from '../utils/capitalize';
import isMuiElement from '../utils/isMuiElement';
import FormControlContext from './FormControlContext';
import { getFormControlUtilityClasses } from './formControlClasses';
const useUtilityClasses = (ownerState) => {
const { classes, margin, fullWidth } = ownerState;
const slots = {
root: ['root', margin !== 'none' && `margin${capitalize(margin)}`, fullWidth && 'fullWidth'],
};
return composeClasses(slots, getFormControlUtilityClasses, classes);
};
const FormControlRoot = styled('div', {
name: 'MuiFormControl',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.root,
styles[`margin${capitalize(ownerState.margin)}`],
ownerState.fullWidth && styles.fullWidth,
];
},
})({
display: 'inline-flex',
flexDirection: 'column',
position: 'relative',
// Reset fieldset default style.
minWidth: 0,
padding: 0,
margin: 0,
border: 0,
verticalAlign: 'top', // Fix alignment issue on Safari.
variants: [
{
props: { margin: 'normal' },
style: {
marginTop: 16,
marginBottom: 8,
},
},
{
props: { margin: 'dense' },
style: {
marginTop: 8,
marginBottom: 4,
},
},
{
props: { fullWidth: true },
style: {
width: '100%',
},
},
],
});
/**
* Provides context such as filled/focused/error/required for form inputs.
* Relying on the context provides high flexibility and ensures that the state always stays
* consistent across the children of the `FormControl`.
* This context is used by the following components:
*
* - FormLabel
* - FormHelperText
* - Input
* - InputLabel
*
* You can find one composition example below and more going to [the demos](/material-ui/react-text-field/#components).
*
* ```jsx
* <FormControl>
* <InputLabel htmlFor="my-input">Email address</InputLabel>
* <Input id="my-input" aria-describedby="my-helper-text" />
* <FormHelperText id="my-helper-text">We'll never share your email.</FormHelperText>
* </FormControl>
* ```
*
* ⚠️ Only one `InputBase` can be used within a FormControl because it creates visual inconsistencies.
* For instance, only one input can be focused at the same time, the state shouldn't be shared.
*/
const FormControl = React.forwardRef(function FormControl(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiFormControl' });
const {
children,
className,
color = 'primary',
component = 'div',
disabled = false,
error = false,
focused: visuallyFocused,
fullWidth = false,
hiddenLabel = false,
margin = 'none',
required = false,
size = 'medium',
variant = 'outlined',
...other
} = props;
const ownerState = {
...props,
color,
component,
disabled,
error,
fullWidth,
hiddenLabel,
margin,
required,
size,
variant,
};
const classes = useUtilityClasses(ownerState);
const [adornedStart, setAdornedStart] = React.useState(() => {
// We need to iterate through the children and find the Input in order
// to fully support server-side rendering.
let initialAdornedStart = false;
if (children) {
React.Children.forEach(children, (child) => {
if (!isMuiElement(child, ['Input', 'Select'])) {
return;
}
const input = isMuiElement(child, ['Select']) ? child.props.input : child;
if (input && isAdornedStart(input.props)) {
initialAdornedStart = true;
}
});
}
return initialAdornedStart;
});
const [filled, setFilled] = React.useState(() => {
// We need to iterate through the children and find the Input in order
// to fully support server-side rendering.
let initialFilled = false;
if (children) {
React.Children.forEach(children, (child) => {
if (!isMuiElement(child, ['Input', 'Select'])) {
return;
}
if (isFilled(child.props, true) || isFilled(child.props.inputProps, true)) {
initialFilled = true;
}
});
}
return initialFilled;
});
const [focusedState, setFocused] = React.useState(false);
if (disabled && focusedState) {
setFocused(false);
}
const focused = visuallyFocused !== undefined && !disabled ? visuallyFocused : focusedState;
let registerEffect;
const registeredInput = React.useRef(false);
if (process.env.NODE_ENV !== 'production') {
registerEffect = () => {
if (registeredInput.current) {
console.error(
[
'MUI: There are multiple `InputBase` components inside a FormControl.',
'This creates visual inconsistencies, only use one `InputBase`.',
].join('\n'),
);
}
registeredInput.current = true;
return () => {
registeredInput.current = false;
};
};
}
const onFilled = React.useCallback(() => {
setFilled(true);
}, []);
const onEmpty = React.useCallback(() => {
setFilled(false);
}, []);
const childContext = React.useMemo(() => {
return {
adornedStart,
setAdornedStart,
color,
disabled,
error,
filled,
focused,
fullWidth,
hiddenLabel,
size,
onBlur: () => {
setFocused(false);
},
onFocus: () => {
setFocused(true);
},
onEmpty,
onFilled,
registerEffect,
required,
variant,
};
}, [
adornedStart,
color,
disabled,
error,
filled,
focused,
fullWidth,
hiddenLabel,
registerEffect,
onEmpty,
onFilled,
required,
size,
variant,
]);
return (
<FormControlContext.Provider value={childContext}>
<FormControlRoot
as={component}
ownerState={ownerState}
className={clsx(classes.root, className)}
ref={ref}
{...other}
>
{children}
</FormControlRoot>
</FormControlContext.Provider>
);
});
FormControl.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the component.
* It supports both default and custom theme colors, which can be added as shown in the
* [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
* @default 'primary'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['primary', 'secondary', 'error', 'info', 'success', 'warning']),
PropTypes.string,
]),
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* If `true`, the label, input and helper text should be displayed in a disabled state.
* @default false
*/
disabled: PropTypes.bool,
/**
* If `true`, the label is displayed in an error state.
* @default false
*/
error: PropTypes.bool,
/**
* If `true`, the component is displayed in focused state.
*/
focused: PropTypes.bool,
/**
* If `true`, the component will take up the full width of its container.
* @default false
*/
fullWidth: PropTypes.bool,
/**
* If `true`, the label is hidden.
* This is used to increase density for a `FilledInput`.
* Be sure to add `aria-label` to the `input` element.
* @default false
*/
hiddenLabel: PropTypes.bool,
/**
* If `dense` or `normal`, will adjust vertical spacing of this and contained components.
* @default 'none'
*/
margin: PropTypes.oneOf(['dense', 'none', 'normal']),
/**
* If `true`, the label will indicate that the `input` is required.
* @default false
*/
required: PropTypes.bool,
/**
* The size of the component.
* @default 'medium'
*/
size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['medium', 'small']),
PropTypes.string,
]),
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* The variant to use.
* @default 'outlined'
*/
variant: PropTypes.oneOf(['filled', 'outlined', 'standard']),
};
export default FormControl;