/
index.jsx
254 lines (212 loc) · 8.12 KB
/
index.jsx
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
import React from "react";
import { render } from "react-dom";
import debounce from "./debounce";
import { Form, InputGroup } from "Form";
const XHR_TIMEOUT = 1000,
MONTHS = [ "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December" ];
async function onSubmit( form ) {
try {
let rsp = await fetch( `server-response.json` ).then( rsp => rsp.json() );
if ( !rsp.ok ) {
form.setError( rsp.exception.message );
}
} catch( e ) {
form.setError( "Server error" );
}
};
// Debounce for 50ms
const onInput = debounce(( e, inputGroup, form ) => {
// Update state only of input group in focus
inputGroup.checkValidityAndUpdate();
// Update "valid" property of the form
form.checkValidityAndUpdate();
}, 50 );
const onShowErrors = ( e, form ) => {
e.preventDefault();
form.checkValidityAndUpdateInputGroups();
};
const validateDateTime = ( input ) => {
if ( input.current.value === "Choose..." ) {
input.setCustomValidity( `Please select ${input.current.title}` );
return false;
}
return true;
};
const MyForm = props => (
<Form onSubmit={onSubmit} id="myform">
{({ error, valid, pristine, submitting, submitted, form }) => (
<React.Fragment>
<h2>Demo Form</h2>
<p>Form validity:
{ valid
? <span className="text-success">valid</span>
: <span className="text-danger">at least one input in invalid state</span> }
<button className="btn btn-info btn-sm" onClick={( e ) => onShowErrors( e, form ) }>update input groups</button>
</p>
{ error && (<div className="alert alert-danger" role="alert">
<strong>Oh snap!</strong> {error}
</div>)
}
{ submitted && !error && (<div className="alert alert-success" role="alert">
Thanks for submitting!
</div>)
}
<div className="form-row">
<InputGroup
tag="div"
className="form-group col-md-6"
validate={[ "email" ]}
translate={{
email: {
valueMissing: "C'mon! We need some value"
}
}}>
{({ error, valid }) => (
<div>
<label id="emailLabel" htmlFor="emailInput">Email address</label>
<input
type="email"
required
name="email"
aria-labelledby="emailLabel"
className={`form-control ${valid ? "" : "is-invalid"}`}
id="emailInput"
aria-describedby="emailHelp"
placeholder="Enter email" />
{ error && (<div className="invalid-feedback">{error}</div>) }
<small className="form-text text-muted">
Group validates on submit event
</small>
</div>
)}
</InputGroup>
<InputGroup
tag="div"
className="form-group col-md-6"
validate={[ "firstName" ]}
translate={{
firstName: {
patternMismatch: "Please enter a valid first name."
}
}}>
{({ error, valid, inputGroup }) => (
<div>
<label id="firstNameLabel" htmlFor="firstNameInput">First Name</label>
<input
pattern="^.{5,30}$"
required
className={`form-control ${valid ? "" : "is-invalid"}`}
id="firstNameInput"
aria-labelledby="firstNameLabel"
name="firstName"
onInput={( e ) => onInput( e, inputGroup, form ) }
placeholder="Enter first name"/>
{ error && (<div className="invalid-feedback">{error}</div>) }
<small className="form-text text-muted">
Group validates as you are typing
</small>
</div>
)}
</InputGroup>
</div>
<InputGroup validate={{
"vatId": ( input ) => {
if ( !input.current.value.startsWith( "DE" ) ) {
input.setCustomValidity( "Code must start with DE." );
return false;
}
return true;
}
}}>
{({ error, valid }) => (
<div className="form-group">
<label id="vatIdLabel" htmlFor="vatIdInput">VAT Number (optional)</label>
<input
className={`form-control ${valid ? "" : "is-invalid"}`}
id="vatIdInput"
aria-labelledby="vatIdLabel"
name="vatId"
placeholder="Enter VAT Number"/>
{ error && (<div className="invalid-feedback">{error}</div>) }
<small className="form-text text-muted">
Group validates on submit event
</small>
</div>
)}
</InputGroup>
<InputGroup validate={{ "day": validateDateTime, "month": validateDateTime }}>
{({ errors, valid }) => (
<div className="form-group">
<div className="form-row">
<div className="form-group col-md-6">
<label htmlFor="selectDay">Day</label>
<select name="day" id="selectDay" title="Day" className={`form-control ${valid ? "" : "is-invalid"}`}>
<option>Choose...</option>
{ [ ...Array( 31 ).keys() ].map( inx => ( <option key={ inx }>{ inx + 1 }</option> )) }
</select>
</div>
<div className="form-group col-md-6">
<label htmlFor="selectMonth">Month</label>
<select name="month" id="selectMonth" title="Month" className={`form-control ${valid ? "" : "is-invalid"}`}>
<option>Choose...</option>
{ MONTHS.map( ( m, inx ) => ( <option key={ inx }>{ m }</option> )) }
</select>
</div>
</div>
{ errors.map( ( error, key ) => ( <div key={key} className="alert alert-danger">{error}</div> )) }
<small className="form-text text-muted">
Group validates on submit event
</small>
</div>
)}
</InputGroup>
<div className="form-row">
<InputGroup
validate={[ "exampleCheck1", "exampleRadio1" ]}
className="form-group col-md-6">
{({ error, valid }) => (
<div>
<div className="form-check">
<input name="exampleCheck1" type="checkbox"
className={`form-check-input ${valid ? "" : "is-invalid"}`} id="exampleCheck1" />
<label className="form-check-label" htmlFor="exampleCheck1">Check me out</label>
{ error && (<div className="invalid-feedback">{error}</div>) }
</div>
<div className="form-check">
<input name="exampleRadio1" type="radio"
className={`form-check-input ${valid ? "" : "is-invalid"}`} id="exampleRadio1" />
<label className="form-check-label" htmlFor="exampleRadio1">Pick me</label>
{ error && (<div className="invalid-feedback">{error}</div>) }
</div>
</div>
)}
</InputGroup>
<InputGroup
validate={[ "fileInput" ]}
className="form-group col-md-6">
{({ error, valid }) => (
<div>
<label id="fileInputLabel" htmlFor="fileInput">File</label>
<input
className={`form-control-file ${valid ? "" : "is-invalid"}`}
id="fileInput"
type="file"
required
aria-labelledby="fileInputLabel"
name="fileInput" />
{ error && (<div className="invalid-feedback">{error}</div>) }
</div>
)}
</InputGroup>
</div>
<button
disabled={ ( pristine || submitting ) }
className="btn btn-primary"
type="submit"
>Submit</button>
</React.Fragment>
)}
</Form>
);
render( <MyForm />, document.getElementById( "app" ) );