Skip to content

Commit 9324e1c

Browse files
mj12albertchristophermorin
authored andcommitted
[FormControl][material-next] Copy v5 FormControl (mui#39039)
1 parent 5da2dec commit 9324e1c

10 files changed

+948
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import * as React from 'react';
2+
import { SxProps } from '@mui/system';
3+
import { OverridableStringUnion, OverridableComponent, OverrideProps } from '@mui/types';
4+
import { Theme } from '../styles';
5+
import { FormControlClasses } from './formControlClasses';
6+
7+
export interface FormControlPropsSizeOverrides {}
8+
export interface FormControlPropsColorOverrides {}
9+
10+
export interface FormControlOwnProps {
11+
/**
12+
* The content of the component.
13+
*/
14+
children?: React.ReactNode;
15+
/**
16+
* Override or extend the styles applied to the component.
17+
*/
18+
classes?: Partial<FormControlClasses>;
19+
/**
20+
* The color of the component.
21+
* It supports both default and custom theme colors, which can be added as shown in the
22+
* [palette customization guide](https://mui.com/material-ui/customization/palette/#adding-new-colors).
23+
* @default 'primary'
24+
*/
25+
color?: OverridableStringUnion<
26+
'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning',
27+
FormControlPropsColorOverrides
28+
>;
29+
/**
30+
* If `true`, the label, input and helper text should be displayed in a disabled state.
31+
* @default false
32+
*/
33+
disabled?: boolean;
34+
/**
35+
* If `true`, the label is displayed in an error state.
36+
* @default false
37+
*/
38+
error?: boolean;
39+
/**
40+
* If `true`, the component will take up the full width of its container.
41+
* @default false
42+
*/
43+
fullWidth?: boolean;
44+
/**
45+
* If `true`, the component is displayed in focused state.
46+
*/
47+
focused?: boolean;
48+
/**
49+
* If `true`, the label is hidden.
50+
* This is used to increase density for a `FilledInput`.
51+
* Be sure to add `aria-label` to the `input` element.
52+
* @default false
53+
*/
54+
hiddenLabel?: boolean;
55+
/**
56+
* If `dense` or `normal`, will adjust vertical spacing of this and contained components.
57+
* @default 'none'
58+
*/
59+
margin?: 'dense' | 'normal' | 'none';
60+
/**
61+
* If `true`, the label will indicate that the `input` is required.
62+
* @default false
63+
*/
64+
required?: boolean;
65+
/**
66+
* The size of the component.
67+
* @default 'medium'
68+
*/
69+
size?: OverridableStringUnion<'small' | 'medium', FormControlPropsSizeOverrides>;
70+
/**
71+
* The system prop that allows defining system overrides as well as additional CSS styles.
72+
*/
73+
sx?: SxProps<Theme>;
74+
/**
75+
* The variant to use.
76+
* @default 'outlined'
77+
*/
78+
variant?: 'standard' | 'outlined' | 'filled';
79+
}
80+
81+
export interface FormControlTypeMap<
82+
AdditionalProps = {},
83+
RootComponent extends React.ElementType = 'div',
84+
> {
85+
props: AdditionalProps & FormControlOwnProps;
86+
defaultComponent: RootComponent;
87+
}
88+
89+
/**
90+
* Provides context such as filled/focused/error/required for form inputs.
91+
* Relying on the context provides high flexibility and ensures that the state always stays
92+
* consistent across the children of the `FormControl`.
93+
* This context is used by the following components:
94+
*
95+
* * FormLabel
96+
* * FormHelperText
97+
* * Input
98+
* * InputLabel
99+
*
100+
* You can find one composition example below and more going to [the demos](https://mui.com/material-ui/react-text-field/#components).
101+
*
102+
* ```jsx
103+
* <FormControl>
104+
* <InputLabel htmlFor="my-input">Email address</InputLabel>
105+
* <Input id="my-input" aria-describedby="my-helper-text" />
106+
* <FormHelperText id="my-helper-text">We'll never share your email.</FormHelperText>
107+
* </FormControl>
108+
* ```
109+
*
110+
* ⚠️ Only one `InputBase` can be used within a FormControl because it creates visual inconsistencies.
111+
* For instance, only one input can be focused at the same time, the state shouldn't be shared.
112+
*
113+
* Demos:
114+
*
115+
* - [Checkbox](https://mui.com/material-ui/react-checkbox/)
116+
* - [Radio Group](https://mui.com/material-ui/react-radio-button/)
117+
* - [Switch](https://mui.com/material-ui/react-switch/)
118+
* - [Text Field](https://mui.com/material-ui/react-text-field/)
119+
*
120+
* API:
121+
*
122+
* - [FormControl API](https://mui.com/material-ui/api/form-control/)
123+
*/
124+
declare const FormControl: OverridableComponent<FormControlTypeMap>;
125+
126+
export type FormControlProps<
127+
RootComponent extends React.ElementType = FormControlTypeMap['defaultComponent'],
128+
AdditionalProps = {},
129+
> = OverrideProps<FormControlTypeMap<AdditionalProps, RootComponent>, RootComponent> & {
130+
component?: React.ElementType;
131+
};
132+
133+
export default FormControl;

0 commit comments

Comments
 (0)