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
/
index.js
55 lines (44 loc) · 1.48 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
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) => {
// Access to field and form state
console.log('Field State', fieldState)
console.log('Form State State', formState)
return (
<div className="field-wrap">
<label htmlFor={`field-${name}`}>{label}</label>
<div className="field">
<Component name={name} originalValue={value} fieldState={fieldState} formState={formState} events={events}>
{children}
</Component>
</div>
<div className="error">
{fieldState.error}
</div>
</div>
)
}} />
)
}
class LoginForm extends React.Component {
validate(values) {
const errors = {}
if (!/^[\w\d\.]+@[\w\d]+\.[\w]{2,9}$/.test(values.email)) errors.email = 'Invalid Email'
return errors
}
render() {
// The password field was left out of this example on purpose to limit
// the amount of console logs for state changes.
return (
<Form validate={this.validate}>
<FieldWrap label="Email" name="email" component={InputField} />
<button type="submit">Submit</button>
</Form>
)
}
}
ReactDOM.render(<LoginForm />, document.getElementById('root'))