@@ -5,7 +5,6 @@ import { UseFormOptions } from "./types";
55import { Validator } from './validator' ;
66import { findAutoFields } from "./utils/findAutoFields" ;
77import React from "react" ;
8- import { isEmpty } from "../utils/isEmpty" ;
98import { fromStateToField } from "./utils/fromStateToField" ;
109import { fromFieldToState } from "./utils" ;
1110import { isFalse } from "./utils/isFalse" ;
@@ -51,16 +50,13 @@ export function createAutoFormComponent<State extends Dict>(store: ReactAutoStor
5150 const options :UseFormOptions < State > = formCtx . current ! . options
5251 return ( props :AutoFormProps < State > ) => {
5352 const initial = useRef < boolean > ( false ) ;
53+
5454 // 仅在初始化时执行一次
5555 const initForm = useCallback ( ( ) => {
5656 const form = options . ref ! . current ;
5757 if ( ! form ) return ;
5858 let initValid :boolean = true
59- ctx . fields = findAutoFields ( form , options . findFields ) ;
60- if ( isEmpty ( ctx . fields ) ) {
61- store . log ( 'No fields found in the autoform' , 'warn' )
62- return
63- }
59+ ctx . fields = findAutoFields ( form , options . findFields ) ;
6460 ctx . validator . attach ( )
6561 // 初始化表单控件的值: 从state读取数据更新到表单控件
6662 initFormFields ( store , ctx . fields , options )
@@ -76,15 +72,16 @@ export function createAutoFormComponent<State extends Dict>(store: ReactAutoStor
7672 useEffect ( ( ) => {
7773 const form = options . ref ! . current ;
7874 if ( ! form ) return ;
79- const { entry = [ ] } = options ;
75+ const { entry = [ ] } = options ;
76+ const entryPath = Array . isArray ( entry ) ? entry : entry . split ( PATH_DELIMITER )
8077 // 1. 初始化表单控件
8178 if ( ! initial . current && form ) {
8279 initForm ( )
8380 }
8481 // 2. 侦听来自变更
8582 const watcher = store . watch ( ( { path, value } ) => {
8683 // 2.1 如果变更的路径不是表单的路径,则忽略
87- if ( ! pathStartsWith ( entry , path ) ) return ;
84+ if ( ! pathStartsWith ( entryPath , path ) ) return ;
8885 // 2.2 更新到表单的输入控件
8986 const spath = path . join ( PATH_DELIMITER ) ;
9087 if ( spath in ctx . fields ) {
@@ -133,8 +130,6 @@ export function createAutoFormComponent<State extends Dict>(store: ReactAutoStor
133130 } ;
134131
135132 } , [ ] ) ;
136-
137-
138133
139134 return < form { ...props }
140135 ref = { options . ref }
0 commit comments