33/* eslint-disable react/hook-use-state */
44import { useEffect , useState } from 'react' ;
55import { flushSync } from 'react-dom' ;
6- import type { AllPathsKeys , PathToDeepType } from 'skyroc-type-utils' ;
6+ import type { AllPathsKeys , KeysToNestedObject , PathToDeepType } from 'skyroc-type-utils' ;
77import { isArray , isNil , isObject } from 'skyroc-utils' ;
88
99import type { SubscribeMaskOptions } from '../../form-core/event' ;
1010import { toMask } from '../../form-core/event' ;
1111import type { Meta } from '../../types/shared-types' ;
12+ import { get } from '../../utils/get' ;
1213
1314import type { FormInstance , InternalFormInstance } from './FieldContext' ;
1415import { useFieldContext } from './FieldContext' ;
@@ -19,16 +20,12 @@ type UseFormFieldsStateOpts<Values> = {
1920 mask ?: SubscribeMaskOptions ;
2021} ;
2122
22- export type MetaShape <
23+ export type MetaShapeNested <
2324 Values ,
2425 Names extends readonly AllPathsKeys < Values > [ ] | undefined = undefined
2526> = Names extends readonly AllPathsKeys < Values > [ ]
26- ? {
27- [ K in Names [ number ] ] : Meta < K , PathToDeepType < Values , K > > ;
28- }
29- : {
30- [ K in AllPathsKeys < Values > ] : Meta < K , PathToDeepType < Values , K > > ;
31- } ;
27+ ? KeysToNestedObject < Names , Meta < any , any > > // 多字段/全字段 → 嵌套对象
28+ : KeysToNestedObject < AllPathsKeys < Values > [ ] , Meta < any , any > > ; // 默认全量
3229
3330function useFieldState < Values = any , T extends AllPathsKeys < Values > = AllPathsKeys < Values > > (
3431 names : T ,
@@ -38,11 +35,13 @@ function useFieldState<Values = any, T extends AllPathsKeys<Values> = AllPathsKe
3835function useFieldState < Values = any , T extends AllPathsKeys < Values > = AllPathsKeys < Values > > (
3936 names : T [ ] ,
4037 opts ?: UseFormFieldsStateOpts < Values >
41- ) : MetaShape < Values , T [ ] > ;
38+ ) : MetaShapeNested < Values , T [ ] > ;
4239
43- function useFieldState < Values = any > ( ) : MetaShape < Values , AllPathsKeys < Values > [ ] > ;
40+ // 无参数:全量嵌套对象
41+ function useFieldState < Values = any > ( ) : MetaShapeNested < Values , AllPathsKeys < Values > [ ] > ;
4442
45- function useFieldState < Values = any > ( form : FormInstance < Values > ) : MetaShape < Values , AllPathsKeys < Values > [ ] > ;
43+ // form 参数:全量嵌套对象
44+ function useFieldState < Values = any > ( form : FormInstance < Values > ) : MetaShapeNested < Values , AllPathsKeys < Values > [ ] > ;
4645
4746function useFieldState < Values = any > (
4847 names ?: AllPathsKeys < Values > | AllPathsKeys < Values > [ ] | FormInstance < Values > ,
@@ -88,6 +87,8 @@ function useFieldState<Values = any>(
8887 warnings : true
8988 } ;
9089
90+ const includeChildren = opts ?. includeChildren ?? isFormInstance ;
91+
9192 const [ _ , forceUpdate ] = useState ( { } ) ;
9293
9394 useEffect ( ( ) => {
@@ -97,7 +98,7 @@ function useFieldState<Values = any>(
9798 flushSync ( ( ) => forceUpdate ( { } ) ) ;
9899 } ,
99100 {
100- includeChildren : opts ?. includeChildren ,
101+ includeChildren,
101102 mask : toMask ( mask )
102103 }
103104 ) ;
@@ -106,14 +107,14 @@ function useFieldState<Values = any>(
106107
107108 if ( ! subscribeNames ) {
108109 // names 为空 → 返回 Map 形式
109- return Object . fromEntries ( state . map ( item => [ item . name , item ] ) ) ;
110+ return state ;
110111 }
111112 if ( subscribeNames . length === 1 ) {
112113 // 单字段 → 直接返回该字段的 meta
113- return state [ 0 ] ;
114+ return get ( state , subscribeNames [ 0 ] ) ;
114115 }
115116 // 多字段 → 返回对象
116- return Object . fromEntries ( state . map ( item => [ item . name , item ] ) ) ;
117+ return state ;
117118}
118119
119120export { useFieldState } ;
0 commit comments