Skip to content

Commit 3b5c954

Browse files
committed
refactor: 💡 use callback to memo validator
1 parent db3de2e commit 3b5c954

File tree

3 files changed

+122
-24
lines changed

3 files changed

+122
-24
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import * as React from 'react'
2+
3+
import { storiesOf } from '@storybook/react'
4+
import { action } from '@storybook/addon-actions'
5+
import useForm from '../index';
6+
7+
interface IForm {
8+
name: string
9+
password: string,
10+
mobile: string,
11+
need: boolean,
12+
}
13+
14+
const Demo = () => {
15+
const [form, field] = useForm<IForm>({
16+
name: '',
17+
mobile: '',
18+
password: ''
19+
})
20+
21+
const handleSubmit = (event) => {
22+
event.preventDefault()
23+
form.validate((errors) => {
24+
if(!errors) {
25+
action('submit')(form.value)
26+
} else {
27+
action('errors')(errors)
28+
}
29+
})
30+
}
31+
32+
const handleReset = () => {
33+
form.reset()
34+
action('reset')()
35+
}
36+
37+
const need = field("need", {
38+
type: 'boolean'
39+
})
40+
41+
return (
42+
<div className="section container">
43+
<h3 className="title is-3">
44+
dynamic
45+
</h3>
46+
<form onSubmit={handleSubmit} onReset={handleReset}>
47+
<div className="field">
48+
<label className="label">username</label>
49+
<input className="input" type="text" {...field("name", {
50+
rules: [
51+
{type: "string", required: true}
52+
]
53+
})}
54+
/>
55+
{
56+
form.errors.name && <p className="help is-danger">{form.errors.name[0].message}</p>
57+
}
58+
</div>
59+
<div className="field">
60+
<label className="label">password</label>
61+
<input className="input" type="password" {...field("password", {
62+
rules: [
63+
{type: "string", required: true}
64+
]
65+
})}
66+
/>
67+
{
68+
form.errors.password && <p className="help is-danger">{form.errors.password[0].message}</p>
69+
}
70+
</div>
71+
{
72+
need.value && (
73+
<div className="field">
74+
<label className="label">mobile</label>
75+
<input className="input" type="text" {...field("mobile", {
76+
rules: [
77+
{type: "string", required: true}
78+
]
79+
})}
80+
/>
81+
{
82+
form.errors.mobile && <p className="help is-danger">{form.errors.mobile[0].message}</p>
83+
}
84+
</div>
85+
)
86+
}
87+
<div className="field">
88+
<label className="label">checked</label>
89+
<input type="checkbox"
90+
onChange={() => need.onChange(!need.value)}
91+
onBlur={need.onBlur}
92+
checked={!!need.value}
93+
/>
94+
</div>
95+
<button type='submit' className='button is-link'>submit</button>
96+
<button type='reset' className="button">reset</button>
97+
</form>
98+
<div className="column">
99+
<div className="notification" style={{wordBreak: 'break-all'}}>
100+
{ JSON.stringify(form) }
101+
</div>
102+
</div>
103+
</div>
104+
)
105+
}
106+
107+
storiesOf('rc-use-form', module)
108+
.add('dynamic', () => (
109+
<Demo />
110+
))

‎src/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ export function getResetValue(type) {
5656
}
5757
}
5858

59-
const getValidator = (descriptor) => {
60-
if (Object.keys(descriptor).length) {
61-
return new AsyncValidator(descriptor)
62-
} else {
63-
return null
64-
}
65-
}
66-
6759
const useForm: UseForm = <T>(intial: Partial<T>) => {
6860
const initialData = intial || {}
6961

@@ -80,6 +72,8 @@ const useForm: UseForm = <T>(intial: Partial<T>) => {
8072
options: {},
8173
})
8274

75+
// const fieldOptions = fieldOptionsRef.current
76+
8377
let state = hooksState
8478

8579
const dispatch = function(type, payload) {
@@ -110,6 +104,15 @@ const useForm: UseForm = <T>(intial: Partial<T>) => {
110104
}, {})
111105
}, [fieldOptions])
112106

107+
const getValidator = useCallback((descriptor) => {
108+
if (Object.keys(descriptor).length) {
109+
return new AsyncValidator(descriptor)
110+
} else {
111+
return null
112+
}
113+
}
114+
, [fieldOptions])
115+
113116
const getFormValueFromState = (formState = state) => mapValues(formState.fields, field => field.value)
114117

115118
const updateField = function(name, data = {}) {
@@ -131,7 +134,6 @@ const useForm: UseForm = <T>(intial: Partial<T>) => {
131134
keys,
132135
fieldsError
133136
})
134-
console.log(errors, keys, fieldsError);
135137
return errors
136138
}
137139

@@ -174,6 +176,7 @@ const useForm: UseForm = <T>(intial: Partial<T>) => {
174176
value,
175177
touched: true
176178
})
179+
//#TODO: remove other key error, only add current error
177180
innerValidate(noop, undefined, 'change', newState)
178181
},
179182
onBlur() {

‎src/utils/memoize.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)