forked from mui/material-ui
/
FormControlUnstyled.tsx
222 lines (204 loc) · 5.96 KB
/
FormControlUnstyled.tsx
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
import * as React from 'react';
import PropTypes from 'prop-types';
import { OverridableComponent } from '@mui/types';
import { unstable_useControlled as useControlled } from '@mui/utils';
import FormControlUnstyledContext from './FormControlUnstyledContext';
import { getFormControlUnstyledUtilityClass } from './formControlUnstyledClasses';
import {
FormControlUnstyledProps,
NativeFormControlElement,
FormControlUnstyledTypeMap,
FormControlUnstyledOwnerState,
FormControlUnstyledState,
FormControlUnstyledRootSlotProps,
} from './FormControlUnstyled.types';
import { useSlotProps, WithOptionalOwnerState } from '../utils';
import composeClasses from '../composeClasses';
function hasValue(value: unknown) {
return value != null && !(Array.isArray(value) && value.length === 0) && value !== '';
}
function useUtilityClasses(ownerState: FormControlUnstyledOwnerState) {
const { disabled, error, filled, focused, required } = ownerState;
const slots = {
root: [
'root',
disabled && 'disabled',
focused && 'focused',
error && 'error',
filled && 'filled',
required && 'required',
],
};
return composeClasses(slots, getFormControlUnstyledUtilityClass, {});
}
/**
* 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](https://mui.com/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 `Input` can be used within a FormControl because it create visual inconsistencies.
* For instance, only one input can be focused at the same time, the state shouldn't be shared.
*
* Demos:
*
* - [Form control](https://mui.com/base/react-form-control/)
*
* API:
*
* - [FormControlUnstyled API](https://mui.com/base/api/form-control-unstyled/)
*/
const FormControlUnstyled = React.forwardRef(function FormControlUnstyled<
D extends React.ElementType = FormControlUnstyledTypeMap['defaultComponent'],
>(props: FormControlUnstyledProps<D>, ref: React.ForwardedRef<any>) {
const {
defaultValue,
children,
component,
components = {},
componentsProps = {},
disabled = false,
error = false,
onChange,
required = false,
value: incomingValue,
...other
} = props;
const [value, setValue] = useControlled({
controlled: incomingValue,
default: defaultValue,
name: 'FormControl',
state: 'value',
});
const filled = hasValue(value);
const [focused, setFocused] = React.useState(false);
if (disabled && focused) {
setFocused(false);
}
const ownerState: FormControlUnstyledOwnerState = {
...props,
disabled,
error,
filled,
focused,
required,
};
const handleChange = (event: React.ChangeEvent<NativeFormControlElement>) => {
setValue(event.target.value);
onChange?.(event);
};
const childContext: FormControlUnstyledState = {
disabled,
error,
filled,
focused,
onBlur: () => {
setFocused(false);
},
onChange: handleChange,
onFocus: () => {
setFocused(true);
},
required,
value: value ?? '',
};
const classes = useUtilityClasses(ownerState);
const renderChildren = () => {
if (typeof children === 'function') {
return children(childContext);
}
return children;
};
const Root = component ?? components.Root ?? 'div';
const rootProps: WithOptionalOwnerState<FormControlUnstyledRootSlotProps> = useSlotProps({
elementType: Root,
externalSlotProps: componentsProps.root,
externalForwardedProps: other,
additionalProps: {
ref,
children: renderChildren(),
},
ownerState,
className: classes.root,
});
return (
<FormControlUnstyledContext.Provider value={childContext}>
<Root {...rootProps} />
</FormControlUnstyledContext.Provider>
);
}) as OverridableComponent<FormControlUnstyledTypeMap>;
FormControlUnstyled.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content of the component.
*/
children: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.node,
PropTypes.func,
]),
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* The components used for each slot inside the FormControl.
* Either a string to use a HTML element or a component.
* @default {}
*/
components: PropTypes.shape({
Root: PropTypes.elementType,
}),
/**
* @ignore
*/
componentsProps: PropTypes.shape({
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
}),
/**
* @ignore
*/
defaultValue: PropTypes.any,
/**
* 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,
/**
* @ignore
*/
onChange: PropTypes.func,
/**
* If `true`, the label will indicate that the `input` is required.
* @default false
*/
required: PropTypes.bool,
/**
* @ignore
*/
value: PropTypes.any,
} as any;
export default FormControlUnstyled;