1
- import { useCallback } from 'react' ;
2
- import type { Apis , GlobalProps } from 'react-form-simple/types/form ' ;
1
+ import { useCallback , useRef } from 'react' ;
2
+ import type { Apis , GlobalProps } from 'react-form-simple' ;
3
3
import { isMeaningful } from 'react-form-simple/utils/util' ;
4
- import useControllerRef from './useControllerRef' ;
5
4
6
5
export const useContextApi = ( ) => {
7
- const globalDatas = useControllerRef ( {
8
- apis : new Map ( ) ,
9
- bindIdApis : new Map ( ) ,
10
- } ) ;
11
- const { apis, bindIdApis } = globalDatas ;
6
+ const globalDatas = useRef ( {
7
+ apis : new Map < string , GlobalProps . ApiEffectOptions > ( ) ,
8
+ bindIdMapUid : new Map ( ) ,
9
+ } ) . current ;
10
+ const { apis, bindIdMapUid } = globalDatas ;
11
+
12
+ const getUid = ( bindId : any ) => bindIdMapUid . get ( bindId ) ;
13
+
14
+ const formarStringToArray = ( value : Apis . ValidateBindIds ) =>
15
+ typeof value === 'string'
16
+ ? [ value ]
17
+ : ( value as ( number | string | boolean ) [ ] ) ;
12
18
13
19
const executeMethodFromApis = useCallback (
14
- ( bindId : Apis . ValidateBindIds , methodName : string , ...args : any [ ] ) => {
20
+ (
21
+ bindId : Apis . ValidateBindIds ,
22
+ methodName : keyof GlobalProps . ApiEffectOptions ,
23
+ ...args : any [ ]
24
+ ) => {
15
25
if ( ! isMeaningful ( bindId ) ) {
16
26
Array . from ( apis . values ( ) ) . forEach ( ( api ) => void api ?. [ methodName ] ?.( ) ) ;
17
27
return ;
18
28
}
19
- const _bindIds =
20
- typeof bindId === 'string'
21
- ? [ bindId ]
22
- : ( bindId as ( number | string | boolean ) [ ] ) ;
29
+ const _bindIds = formarStringToArray ( bindId ) ;
23
30
_bindIds ?. forEach ( ( bindId ) => {
24
- const api = bindIdApis . get ( bindId ) ;
31
+ const uid = getUid ( bindId ) ;
32
+ const api = apis . get ( uid ) ;
25
33
api ?. [ methodName ] ?.( ...args ) ;
26
34
} ) ;
27
35
} ,
28
36
[ ] ,
29
37
) ;
30
38
31
- const contextProps = useControllerRef < GlobalProps . ContextProps > ( {
32
- apiEffect ( { uid, bindId, ...rests } ) {
33
- apis . set ( uid , rests ) ;
34
- bindIdApis . set ( bindId , rests ) ;
39
+ const contextProps = useRef < GlobalProps . ContextProps > ( {
40
+ apiEffect ( option ) {
41
+ const { uid, bindId } = option ;
42
+ apis . set ( uid , option ) ;
43
+ bindIdMapUid . set ( bindId , uid ) ;
44
+ // bindIdApis.set(bindId, option);
35
45
} ,
36
46
destroy ( { uid, bindId } ) {
37
47
apis . delete ( uid ) ;
38
- bindIdApis . delete ( bindId ) ;
48
+ bindIdMapUid . delete ( bindId ) ;
49
+ // bindIdApis.delete(bindId);
39
50
} ,
40
- } ) ;
51
+ } ) . current ;
52
+
53
+ const overlayApis = useRef < Apis . FormApis > ( {
54
+ validate ( names ) {
55
+ const _apiValues = Array . from ( apis . values ( ) ) ;
56
+ if ( isMeaningful ( names ) ) {
57
+ const _bindIds = formarStringToArray ( names ) ;
58
+ const _uid = _bindIds . map ( ( bindId ) => bindIdMapUid . get ( bindId ) ) ;
59
+ const _validateFuns = _apiValues . filter ( ( api ) =>
60
+ _uid . includes ( api . uid ) ,
61
+ ) ;
62
+ return Promise . all ( _validateFuns . map ( ( { validate } ) => validate ( ) ) ) ;
63
+ }
41
64
42
- const overlayApis = useControllerRef < Apis . FormApis > ( {
43
- validate ( ) {
44
- const validateFuns = Array . from ( apis . values ( ) ) . map (
45
- ( { validate } ) => validate ,
46
- ) ;
47
- return Promise . all ( validateFuns . map ( ( fn ) => fn ?.( ) ) ) ;
65
+ return Promise . all ( _apiValues . map ( ( { validate } ) => validate ( ) ) ) ;
48
66
} ,
49
67
reset ( ) {
50
68
Array . from ( apis . values ( ) ) . forEach ( ( { reset } ) => void reset ?.( ) ) ;
@@ -59,15 +77,15 @@ export const useContextApi = () => {
59
77
executeMethodFromApis ( bindId , 'reapplyValidator' ) ;
60
78
} ,
61
79
setValue ( bindId , value ) {
62
- const api = bindIdApis . get ( bindId ) ;
80
+ const api = apis . get ( getUid ( bindId ) ) ;
63
81
if ( api ) {
64
82
api . setValue ( value ) ;
65
83
}
66
84
} ,
67
85
setError ( bindId , message ) {
68
86
executeMethodFromApis ( bindId , 'setError' , message ) ;
69
87
} ,
70
- } ) ;
88
+ } ) . current ;
71
89
72
90
return { globalDatas, contextProps, overlayApis } ;
73
91
} ;
0 commit comments