File tree Expand file tree Collapse file tree 4 files changed +37
-4
lines changed
primitives/filed-form /src Expand file tree Collapse file tree 4 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -1337,6 +1337,7 @@ class FormStore {
13371337 getArrayFields : this . getArrayFields ,
13381338 getInitialValue : this . getInitialValue ,
13391339 registerComputed : this . registerComputed ,
1340+ registerEffect : this . registerEffect ,
13401341 registerField : this . registerField ,
13411342 setCallbacks : this . setCallbacks ,
13421343 setFieldRules : this . setFieldRules ,
Original file line number Diff line number Diff line change @@ -130,6 +130,10 @@ export interface InternalFieldHooks<Values = any> {
130130 deps : AllPathsKeys < Values > [ ] ,
131131 compute : ( get : ( n : AllPathsKeys < Values > ) => any , all : Values ) => PathToDeepType < Values , T >
132132 ) => ( ) => void ;
133+ registerEffect : (
134+ deps : AllPathsKeys < Values > [ ] ,
135+ effect : ( get : ( n : AllPathsKeys < Values > ) => any , all : Values ) => void
136+ ) => ( ) => void ;
133137 registerField : ( entity : FieldEntity ) => ( ) => void ;
134138 setFieldRules : ( name : AllPathsKeys < Values > , rules ?: Rule [ ] ) => void ;
135139 setRules : ( name : AllPathsKeys < Values > , rules ?: Rule [ ] ) => void ;
Original file line number Diff line number Diff line change @@ -11,15 +11,15 @@ export type ArrayFieldItem = {
1111} ;
1212
1313export function useArrayField < Values = any > ( name : ArrayKeys < Values > , form ?: FormInstance < Values > ) {
14- const context = useFieldContext ( ) ;
14+ const contextForm = useFieldContext ( ) ;
1515
16- const fieldContext = form ?? context ;
16+ const formInstance = form ?? contextForm ;
1717
18- if ( ! fieldContext ) {
18+ if ( ! formInstance ) {
1919 throw new Error ( 'Can not find FormContext. Please make sure you wrap Field under Form or provide a form instance.' ) ;
2020 }
2121
22- const { arrayOp } = fieldContext as unknown as InternalFormInstance < Values > ;
22+ const { arrayOp } = formInstance as unknown as InternalFormInstance < Values > ;
2323
2424 return arrayOp ( name ) ;
2525}
Original file line number Diff line number Diff line change 1+ import { useEffect } from 'react' ;
2+ import type { AllPathsKeys } from 'skyroc-type-utils' ;
3+
4+ import type { FormInstance , InternalFormInstance } from './FieldContext' ;
5+ import { useFieldContext } from './FieldContext' ;
6+
7+ export function useEffectField < Values = any > (
8+ deps : AllPathsKeys < Values > [ ] ,
9+ effect : ( get : ( n : AllPathsKeys < Values > ) => any , all : Values ) => void ,
10+ form ?: FormInstance < Values >
11+ ) {
12+ const contextForm = useFieldContext < Values > ( ) ;
13+
14+ const formInstance = form ?? contextForm ;
15+
16+ if ( ! formInstance ) {
17+ throw new Error ( 'Can not find FormContext. Please make sure you wrap Field under Form or provide a form instance.' ) ;
18+ }
19+
20+ const { getInternalHooks } = formInstance as unknown as InternalFormInstance < Values > ;
21+
22+ const { registerEffect } = getInternalHooks ( ) ;
23+
24+ useEffect ( ( ) => {
25+ const unregister = registerEffect ( deps , effect ) ;
26+ return unregister ;
27+ } , [ deps ] ) ;
28+ }
You can’t perform that action at this time.
0 commit comments