11import { useRef } from 'react'
22import Schema from 'async-validator'
3+ import { merge } from '@/utils/merge'
34import {
4- Store ,
55 Callbacks ,
6- FormInstance ,
76 FormFieldEntity ,
7+ FormInstance ,
88 NamePath ,
9+ Store ,
910} from './types'
1011
1112export const SECRET = 'NUT_FORM_INTERNAL'
@@ -74,28 +75,31 @@ class FormStore {
7475 return fieldsValue
7576 }
7677
78+ updateStore ( nextStore : Store ) {
79+ this . store = nextStore
80+ }
81+
7782 /**
7883 * 设置 form 的初始值,之后在 reset 的时候使用
7984 * @param values
8085 * @param init
8186 */
8287
83- setInitialValues = ( values : Store , init : boolean ) => {
88+ setInitialValues = ( initialValues : Store , init : boolean ) => {
89+ this . initialValues = initialValues || { }
8490 if ( init ) {
85- this . initialValues = values
86- this . store = values
91+ const nextStore = merge ( initialValues , this . store )
92+ this . updateStore ( nextStore )
8793 }
8894 }
8995
9096 /**
9197 * 存储组件数据
9298 * @param newStore { [name]: newValue }
9399 */
94- setFieldsValue = ( newStore : any , needValidate = true ) => {
95- this . store = {
96- ...this . store ,
97- ...newStore ,
98- }
100+ setFieldsValue = ( newStore : any ) => {
101+ const nextStore = merge ( this . store , newStore )
102+ this . updateStore ( nextStore )
99103 this . fieldEntities . forEach ( ( entity : FormFieldEntity ) => {
100104 const { name } = entity . props
101105 Object . keys ( newStore ) . forEach ( ( key ) => {
@@ -113,7 +117,6 @@ class FormStore {
113117 item . entity . onStoreChange ( 'update' )
114118 }
115119 } )
116- needValidate && this . validateFields ( )
117120 }
118121
119122 setCallback = ( callback : Callbacks ) => {
@@ -123,48 +126,55 @@ class FormStore {
123126 }
124127 }
125128
129+ validateEntities = async ( entity : FormFieldEntity , errs : any [ ] ) => {
130+ const { name, rules = [ ] } = entity . props
131+ const descriptor : any = { }
132+ if ( rules . length ) {
133+ // 多条校验规则
134+ if ( rules . length > 1 ) {
135+ descriptor [ name ] = [ ]
136+ rules . forEach ( ( v : any ) => {
137+ descriptor [ name ] . push ( v )
138+ } )
139+ } else {
140+ descriptor [ name ] = rules [ 0 ]
141+ }
142+ }
143+ const validator = new Schema ( descriptor )
144+ // 此处合并无值message 没有意义?
145+ // validator.messages()
146+ try {
147+ await validator . validate ( { [ name ] : this . store ?. [ name ] } )
148+ } catch ( { errors } ) {
149+ if ( errors ) {
150+ errs . push ( ...( errors as any [ ] ) )
151+ this . errors [ name ] = errors
152+ }
153+ } finally {
154+ if ( ! errs || errs . length === 0 ) {
155+ this . errors [ name ] = [ ]
156+ }
157+ }
158+
159+ entity . onStoreChange ( 'validate' )
160+ }
161+
126162 validateFields = async ( nameList ?: NamePath [ ] ) => {
127- let filterEntitys = [ ]
128- const errs = [ ]
163+ let filterEntities = [ ]
129164 this . errors . length = 0
130165 if ( ! nameList || nameList . length === 0 ) {
131- filterEntitys = this . fieldEntities
166+ filterEntities = this . fieldEntities
132167 } else {
133- filterEntitys = this . fieldEntities . filter ( ( { props : { name } } ) =>
168+ filterEntities = this . fieldEntities . filter ( ( { props : { name } } ) =>
134169 nameList . includes ( name )
135170 )
136171 }
137- for ( const entity of filterEntitys ) {
138- const { name, rules = [ ] } = entity . props
139- const descriptor : any = { }
140- if ( rules . length ) {
141- // 多条校验规则
142- if ( rules . length > 1 ) {
143- descriptor [ name ] = [ ]
144- rules . forEach ( ( v : any ) => {
145- descriptor [ name ] . push ( v )
146- } )
147- } else {
148- descriptor [ name ] = rules [ 0 ]
149- }
150- }
151- const validator = new Schema ( descriptor )
152- // 此处合并无值message 没有意义?
153- // validator.messages()
154- try {
155- await validator . validate ( { [ name ] : this . store ?. [ name ] } )
156- } catch ( { errors } : any ) {
157- if ( errors ) {
158- errs . push ( ...( errors as any [ ] ) )
159- this . errors [ name ] = errors
160- }
161- } finally {
162- if ( ! errs || errs . length === 0 ) {
163- this . errors [ name ] = [ ]
164- }
165- }
166- entity . onStoreChange ( 'validate' )
167- }
172+ const errs : any [ ] = [ ]
173+ await Promise . all (
174+ filterEntities . map ( async ( entity ) => {
175+ await this . validateEntities ( entity , errs )
176+ } )
177+ )
168178 return errs
169179 }
170180
@@ -179,7 +189,8 @@ class FormStore {
179189
180190 resetFields = ( ) => {
181191 this . errors . length = 0
182- this . store = this . initialValues
192+ const nextStore = merge ( { } , this . initialValues )
193+ this . updateStore ( nextStore )
183194 this . fieldEntities . forEach ( ( entity : FormFieldEntity ) => {
184195 entity . onStoreChange ( 'reset' )
185196 } )
@@ -237,6 +248,6 @@ export const useForm = (form?: FormInstance): [FormInstance] => {
237248 const formStore = new FormStore ( )
238249 formRef . current = formStore . getForm ( ) as FormInstance
239250 }
251+ return [ formRef . current as FormInstance ]
240252 }
241- return [ formRef . current ]
242253}
0 commit comments