-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
111 lines (97 loc) · 2.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
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
import React, {Fragment} from 'react'
import {render} from 'react-dom'
import {Formik} from 'formik'
import epitath from '../../src'
class Query extends React.Component {
state = {loading: true, data: null}
componentDidMount() {
setTimeout(() => {
this.setState({
loading: false,
data: {
user: {id: '000', name: 'Nikolas Tesla', email: '[email protected]'},
},
})
}, 2000)
}
render() {
return this.props.children(this.state)
}
}
class Time extends React.Component {
state = {time: new Date()}
componentDidMount() {
setInterval(() => {
this.setState({
time: new Date(),
})
}, 1000)
}
render() {
return this.props.children(this.state)
}
}
function WrapFormik({ children, ...props}){ return <Formik {...props} render={children} /> }
const App = epitath(function*() {
console.log('Rendering again!');
const {loading, data} = yield <Query />
const {time} = yield <Time/>
if (loading) return <h1>Loading</h1>
const {
values,
touched,
errors,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
} = yield (
<WrapFormik
initialValues={{
// Use data from other HOCs!
email: data.user.email,
password: '',
}}
validate={values => {
// same as above, but feel free to move this into a class method now.
let errors = {}
if (!values.email) {
errors.email = 'Required'
} else if (
!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)
) {
errors.email = 'Invalid email address'
}
return errors
}}
/>
)
return (
<div className="App">
<h1>{`Hello, ${data.user.name}`}</h1>
<h2>The time is {time.toLocaleString()}!</h2>
<form onSubmit={handleSubmit}>
<input
type="email"
name="email"
onChange={handleChange}
onBlur={handleBlur}
value={values.email}
/>
{touched.email && errors.email && <div>{errors.email}</div>}
<input
type="password"
name="password"
onChange={handleChange}
onBlur={handleBlur}
value={values.password}
/>
{touched.password && errors.password && <div>{errors.password}</div>}
<button type="submit" disabled={isSubmitting}>
Submit
</button>
</form>
</div>
)
})
render(<App />, document.querySelector('#demo'))