-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathFormControl.js
235 lines (215 loc) · 5.1 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
// @flow
import React from 'react';
import type { ElementType, Node } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import withStyles from '../styles/withStyles';
import { isDirty, isAdornedStart } from '../Input/Input';
import { isMuiElement } from '../utils/reactHelpers';
export const styles = (theme: Object) => ({
root: {
display: 'inline-flex',
flexDirection: 'column',
position: 'relative',
// Reset fieldset default style
minWidth: 0,
padding: 0,
margin: 0,
border: 0,
},
marginNormal: {
marginTop: theme.spacing.unit * 2,
marginBottom: theme.spacing.unit,
},
marginDense: {
marginTop: theme.spacing.unit,
marginBottom: theme.spacing.unit / 2,
},
fullWidth: {
width: '100%',
},
});
export type Margin = 'none' | 'dense' | 'normal';
type ProvidedProps = {
disabled: boolean,
classes: Object,
component: ElementType,
error: boolean,
fullWidth: boolean,
margin: Margin,
required: boolean,
};
export type Props = {
/**
* The contents of the form control.
*/
children?: Node,
/**
* Useful to extend the style applied to components.
*/
classes?: Object,
/**
* @ignore
*/
className?: string,
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
component?: ElementType,
/**
* If `true`, the label, input and helper text should be displayed in a disabled state.
*/
disabled?: boolean,
/**
* If `true`, the label should be displayed in an error state.
*/
error?: boolean,
/**
* If `true`, the component, as well as its children,
* will take up the full width of its container.
*/
fullWidth?: boolean,
/**
* @ignore
*/
onBlur?: Function,
/**
* @ignore
*/
onFocus?: Function,
/**
* If `true`, the label will indicate that the input is required.
*/
required?: boolean,
/**
* If `dense` or `normal`, will adjust vertical spacing of this and contained components.
*/
margin?: Margin,
};
type State = {
adornedStart: boolean,
dirty: boolean,
focused: boolean,
};
/**
* Provides context such as dirty/focused/error/required for form inputs.
* Relying on the context provides high flexibilty and ensures that the state always stay
* consitent across the children of the `FormControl`.
* This context is used by the following components:
* - FormLabel
* - FormHelperText
* - Input
* - InputLabel
*/
class FormControl extends React.Component<ProvidedProps & Props, State> {
static defaultProps = {
component: 'div',
disabled: false,
error: false,
fullWidth: false,
margin: 'none',
required: false,
};
static childContextTypes = {
muiFormControl: PropTypes.object.isRequired,
};
constructor(props, context) {
super(props, context);
// We need to iterate through the children and find the Input in order
// to fully support server side rendering.
const { children } = this.props;
if (children) {
React.Children.forEach(children, child => {
if (isMuiElement(child, ['Input', 'Select']) && isDirty(child.props, true)) {
this.state.dirty = true;
}
if (isMuiElement(child, ['Input']) && isAdornedStart(child.props)) {
this.state.adornedStart = true;
}
});
}
}
state = {
adornedStart: false,
dirty: false,
focused: false,
};
getChildContext() {
const { disabled, error, required, margin } = this.props;
const { adornedStart, dirty, focused } = this.state;
return {
muiFormControl: {
adornedStart,
dirty,
disabled,
error,
focused,
margin,
required,
onDirty: this.handleDirty,
onClean: this.handleClean,
onFocus: this.handleFocus,
onBlur: this.handleBlur,
},
};
}
handleFocus = event => {
if (this.props.onFocus) {
this.props.onFocus(event);
}
if (!this.state.focused) {
this.setState({ focused: true });
}
};
handleBlur = event => {
if (this.props.onBlur) {
this.props.onBlur(event);
}
if (this.state.focused) {
this.setState({ focused: false });
}
};
handleDirty = () => {
if (!this.state.dirty) {
this.setState({ dirty: true });
}
};
handleClean = () => {
if (this.state.dirty) {
this.setState({ dirty: false });
}
};
render() {
const {
children,
classes,
className,
component: ComponentProp,
disabled,
error,
fullWidth,
margin,
...other
} = this.props;
return (
<ComponentProp
className={classNames(
classes.root,
{
[classes.marginNormal]: margin === 'normal',
[classes.marginDense]: margin === 'dense',
[classes.fullWidth]: fullWidth,
},
className,
)}
{...other}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
>
{children}
</ComponentProp>
);
}
}
export default withStyles(styles, { name: 'MuiFormControl' })(FormControl);