44 CloseCircleFilledIcon as TdCloseCircleFilledIcon ,
55 ErrorCircleFilledIcon as TdErrorCircleFilledIcon ,
66} from 'tdesign-icons-react' ;
7- import { get , isEqual , isFunction , isObject , isString , set , unset } from 'lodash-es' ;
7+ import { get , isEqual , isFunction , isObject , isString , set } from 'lodash-es' ;
88
99import useConfig from '../hooks/useConfig' ;
1010import useDefaultProps from '../hooks/useDefaultProps' ;
@@ -81,7 +81,14 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
8181 onFormItemValueChange,
8282 } = useFormContext ( ) ;
8383
84- const { name : formListName , rules : formListRules , formListMapRef, form : formOfFormList } = useFormListContext ( ) ;
84+ const {
85+ name : formListName ,
86+ fullPath : parentFullPath ,
87+ rules : formListRules ,
88+ formListMapRef,
89+ form : formOfFormList ,
90+ } = useFormListContext ( ) ;
91+
8592 const props = useDefaultProps < FormItemProps > ( originalProps , formItemDefaultProps ) ;
8693
8794 const {
@@ -104,10 +111,15 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
104111 requiredMark = requiredMarkFromContext ,
105112 } = props ;
106113
107- const { fullPath : parentFullPath } = useFormListContext ( ) ;
108- const fullPath = concatName ( parentFullPath , name ) ;
114+ /* 用于处理嵌套 Form 的情况 (例如 FormList 内有一个 Dialog + Form) */
115+ const isSameForm = useMemo ( ( ) => isEqual ( form , formOfFormList ) , [ form , formOfFormList ] ) ;
116+
117+ const fullPath = useMemo ( ( ) => {
118+ const validParentFullPath = formListName && isSameForm ? parentFullPath : undefined ;
119+ return concatName ( validParentFullPath , name ) ;
120+ } , [ formListName , parentFullPath , name , isSameForm ] ) ;
109121
110- const { getDefaultInitialData } = useFormItemInitialData ( name , fullPath ) ;
122+ const { defaultInitialData } = useFormItemInitialData ( name , fullPath , initialData , children ) ;
111123
112124 const [ , forceUpdate ] = useState ( { } ) ; // custom render state
113125 const [ freeShowErrorMessage , setFreeShowErrorMessage ] = useState ( undefined ) ;
@@ -116,12 +128,7 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
116128 const [ verifyStatus , setVerifyStatus ] = useState ( 'validating' ) ;
117129 const [ resetValidating , setResetValidating ] = useState ( false ) ;
118130 const [ needResetField , setNeedResetField ] = useState ( false ) ;
119- const [ formValue , setFormValue ] = useState ( ( ) =>
120- getDefaultInitialData ( {
121- children,
122- initialData,
123- } ) ,
124- ) ;
131+ const [ formValue , setFormValue ] = useState ( defaultInitialData ) ;
125132
126133 const formItemRef = useRef < FormItemInstance > ( null ) ; // 当前 formItem 实例
127134 const innerFormItemsRef = useRef ( [ ] ) ;
@@ -130,7 +137,6 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
130137 const valueRef = useRef ( formValue ) ; // 当前最新值
131138 const errorListMapRef = useRef ( new Map ( ) ) ;
132139
133- const isSameForm = useMemo ( ( ) => isEqual ( form , formOfFormList ) , [ form , formOfFormList ] ) ; // 用于处理 Form 嵌套的情况
134140 const snakeName = [ ]
135141 . concat ( isSameForm ? formListName : undefined , name )
136142 . filter ( ( item ) => item !== undefined )
@@ -325,10 +331,7 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
325331
326332 function getResetValue ( resetType : TdFormProps [ 'resetType' ] ) : ValueType {
327333 if ( resetType === 'initial' ) {
328- return getDefaultInitialData ( {
329- children,
330- initialData,
331- } ) ;
334+ return defaultInitialData ;
332335 }
333336
334337 let emptyValue : ValueType ;
@@ -413,26 +416,21 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
413416 } , [ shouldUpdate , form ] ) ;
414417
415418 useEffect ( ( ) => {
416- // 记录填写 name 属性 formItem
417419 if ( typeof name === 'undefined' ) return ;
418420
419- // FormList 下特殊处理
420- if ( formListName && isSameForm ) {
421- formListMapRef . current . set ( fullPath , formItemRef ) ;
422- set ( form ?. store , fullPath , formValue ) ;
423- return ( ) => {
424- // eslint-disable-next-line react-hooks/exhaustive-deps
425- formListMapRef . current . delete ( fullPath ) ;
426- unset ( form ?. store , fullPath ) ;
427- } ;
428- }
429- if ( ! formMapRef ) return ;
430- formMapRef . current . set ( fullPath , formItemRef ) ;
431- set ( form ?. store , fullPath , formValue ) ;
421+ const isFormList = formListName && isSameForm ;
422+ const mapRef = isFormList ? formListMapRef : formMapRef ;
423+ if ( ! mapRef . current ) return ;
424+
425+ // 注册实例
426+ mapRef . current . set ( fullPath , formItemRef ) ;
427+
428+ // 初始化
429+ set ( form ?. store , fullPath , defaultInitialData ) ;
430+ setFormValue ( defaultInitialData ) ;
431+
432432 return ( ) => {
433- // eslint-disable-next-line react-hooks/exhaustive-deps
434- formMapRef . current . delete ( fullPath ) ;
435- unset ( form ?. store , fullPath ) ;
433+ mapRef . current . delete ( fullPath ) ;
436434 } ;
437435 // eslint-disable-next-line react-hooks/exhaustive-deps
438436 } , [ snakeName , formListName ] ) ;
0 commit comments