This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
77 lines (67 loc) · 2.5 KB
/
index.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
import React from 'react'
import ReactDOM from 'react-dom'
import { Form, Field, InputField } from 'src'
const FieldWrap = props => {
const { label, component: Component, children, name, value, ...rest } = props
return (
<Field {...rest} name={name} value={value} render={(events, fieldState, formState) => (
<div className="field-wrap">
<label htmlFor={`field-${name}`}>{label}</label>
<div className="field">
<Component {...rest} originalValue={value} name={name} fieldState={fieldState} formState={formState} events={events}>
{children}
</Component>
</div>
<div className="error">
{fieldState.error}
</div>
</div>
)} />
)
}
// High-level Field Abstractions
const FieldFirstName = props => <FieldWrap label="First Name" name="firstName" component={InputField} {...props} />
const FieldLastName = props => <FieldWrap label="Last Name" name="lastName" component={InputField} {...props} />
const FieldEmail = props => <FieldWrap label="Email" name="email" component={InputField} type="email" {...props} />
const FieldPassword = props => <FieldWrap label="Password" name="password" component={InputField} type="password" {...props} />
class UserForm extends React.Component {
constructor() {
super()
this.state = {}
}
componentWillMount() {
// Simulate network latency for asynchronous `initialValues`
setTimeout(() => {
this.setState({
firstName: 'Sally',
lastName: 'Jane',
email: '[email protected]',
password: 'abc123'
})
}, 500)
}
validate(values) {
const errors = {}
if (!/^[\w\d\.]+@[\w\d]+\.[\w]{2,9}$/.test(values.email)) errors.email = 'Invalid Email'
if (!/^[\w\d]{6,20}$/.test(values.password)) errors.password = 'Invalid Password'
return errors
}
onSubmit(values, formState) {
console.log('Submit Form', values)
return Promise.resolve()
}
render() {
return (
<Form validate={this.validate} onSubmit={this.onSubmit} render={(formProps, formState) => (
<form {...formProps} noValidate>
<FieldFirstName value={this.state.firstName} />
<FieldLastName value={this.state.lastName} />
<FieldEmail value={this.state.email} />
<FieldPassword value={this.state.password} />
<button type="submit" disabled={!formState.validForm}>Submit</button>
</form>
)} />
)
}
}
ReactDOM.render(<UserForm />, document.getElementById('root'))