-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMaterial.tsx
232 lines (197 loc) · 6.8 KB
/
Material.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
223
224
225
226
227
228
229
230
231
232
import * as React from 'react';
import {
createStyles,
createTheme,
FormControl as _FormControl,
FormControlProps,
TextField as _TextField,
TextFieldProps,
Theme,
WithStyles,
withStyles
} from '@material-ui/core';
import { ThemeProvider } from '@material-ui/core/styles';
import { instanceOf } from 'prop-types';
import {
assert,
deepForEach,
Field,
FieldFeedback as _FieldFeedback,
FieldFeedbackBaseProps,
FormWithConstraints as _FormWithConstraints,
FormWithConstraintsChildContext
} from 'react-form-with-constraints';
interface FormControlState {
field: Field | undefined;
}
type FormControlContext = FormWithConstraintsChildContext;
export class FormControl extends React.Component<FormControlProps, FormControlState> {
static contextTypes: React.ValidationMap<FormControlContext> = {
form: instanceOf(_FormWithConstraints).isRequired
};
context!: FormControlContext;
state: FormControlState = {
field: undefined
};
/* eslint-disable react/destructuring-assignment */
componentDidMount() {
this.context.form.addFieldWillValidateEventListener(this.fieldWillValidate);
this.context.form.addFieldDidValidateEventListener(this.fieldDidValidate);
this.context.form.addFieldDidResetEventListener(this.fieldDidReset);
}
componentWillUnmount() {
this.context.form.removeFieldWillValidateEventListener(this.fieldWillValidate);
this.context.form.removeFieldDidValidateEventListener(this.fieldDidValidate);
this.context.form.removeFieldDidResetEventListener(this.fieldDidReset);
}
getAssociatedFieldName() {
const fieldNames = new Set<string>();
deepForEach(this.props.children, child => {
if (child.props !== undefined) {
const fieldName = child.props.name;
if (fieldName !== undefined && fieldName.length > 0) {
fieldNames.add(fieldName);
}
}
});
assert(
fieldNames.size === 1,
// [...Set] vs Array.from(Set): the latter doesn't need downlevelIteration with IE
// eslint-disable-next-line unicorn/prefer-spread
`0 or multiple [name="*"] instead of 1: '${Array.from(fieldNames)}'`
);
// Return the first and only entry
return fieldNames.values().next().value;
}
/* eslint-enable react/destructuring-assignment */
fieldWillValidate = (fieldName: string) => {
// Ignore the event if it's not for us
if (fieldName === this.getAssociatedFieldName()) {
this.setState({ field: undefined });
}
};
fieldDidValidate = (field: Field) => {
// Ignore the event if it's not for us
if (field.name === this.getAssociatedFieldName()) {
this.setState({ field });
}
};
fieldDidReset = (field: Field) => {
// Ignore the event if it's not for us
if (field.name === this.getAssociatedFieldName()) {
this.setState({ field: undefined });
}
};
render() {
const { field } = this.state;
const error = field !== undefined && field.hasErrors();
return <_FormControl error={error} {...this.props} />;
}
}
interface TextFieldState {
field: Field | undefined;
}
export class TextField extends React.Component<TextFieldProps, TextFieldState> {
static contextTypes: React.ValidationMap<FormControlContext> = {
form: instanceOf(_FormWithConstraints).isRequired
};
context!: FormControlContext;
state: TextFieldState = {
field: undefined
};
/* eslint-disable react/destructuring-assignment */
componentDidMount() {
this.context.form.addFieldWillValidateEventListener(this.fieldWillValidate);
this.context.form.addFieldDidValidateEventListener(this.fieldDidValidate);
this.context.form.addFieldDidResetEventListener(this.fieldDidReset);
}
componentWillUnmount() {
this.context.form.removeFieldWillValidateEventListener(this.fieldWillValidate);
this.context.form.removeFieldDidValidateEventListener(this.fieldDidValidate);
this.context.form.removeFieldDidResetEventListener(this.fieldDidReset);
}
fieldWillValidate = (fieldName: string) => {
// Ignore the event if it's not for us
if (fieldName === this.props.name) {
this.setState({ field: undefined });
}
};
fieldDidValidate = (field: Field) => {
// Ignore the event if it's not for us
if (field.name === this.props.name) {
this.setState({ field });
}
};
fieldDidReset = (field: Field) => {
// Ignore the event if it's not for us
if (field.name === this.props.name) {
this.setState({ field: undefined });
}
};
/* eslint-enable react/destructuring-assignment */
render() {
const { field } = this.state;
const error = field !== undefined && field.hasErrors();
return <_TextField error={error} {...this.props} />;
}
}
const defaultTheme = createTheme();
// https://v3-5-0.material-ui.com/customization/themes/#nesting-the-theme
function formWithConstraintsTheme(outerTheme: Theme | null) {
return {
...(outerTheme ?? defaultTheme),
overrides: {
MuiFormHelperText: {
root: {
// Make FormHelperText invisible when there is no content
// https://github.com/mui-org/material-ui/blob/v3.5.1/packages/material-ui/src/FormHelperText/FormHelperText.js#L14-L16
'&:empty': {
marginTop: 0,
minHeight: 0
}
}
}
}
};
}
export class FormWithConstraints extends _FormWithConstraints {
render() {
return <ThemeProvider theme={formWithConstraintsTheme}>{super.render()}</ThemeProvider>;
}
}
function fieldFeedbackStyles(theme: Theme) {
return createStyles({
root: {
// Simulates FormHelperText margin
// https://github.com/mui-org/material-ui/blob/v1.0.0-beta.44/packages/material-ui/src/Form/FormHelperText.js#L12
'&:not(:last-child)': {
marginBottom: theme.spacing()
}
}
});
}
type FieldFeedbackPropsWithStyles = FieldFeedbackBaseProps &
React.HTMLAttributes<HTMLSpanElement> &
WithStyles<typeof fieldFeedbackStyles>;
export const FieldFeedback = withStyles(fieldFeedbackStyles)(
// Without a class name (class extends React.Component) React Developer Tools displays:
// <WithStyles(Component) ...>
// <Component>
// <FieldFeedbackWS className="Component-root-125" ...></FieldFeedback>
// </Component>
// </WithStyles(Component)>
//
// With a class name React Developer Tools displays:
// <WithStyles(FieldFeedbackWS) ...>
// <FieldFeedbackWS>
// <FieldFeedbackWS className="FieldFeedbackWS-root-125" ...></FieldFeedback>
// </FieldFeedbackWS>
// </WithStyles(FieldFeedbackWS)>
class FieldFeedbackWS extends React.PureComponent<FieldFeedbackPropsWithStyles> {
render() {
const { classes, className, ...otherProps } = this.props;
const classNames = className !== undefined ? `${className} ${classes.root}` : classes.root;
return <_FieldFeedback className={classNames} {...otherProps} />;
}
}
);