@@ -8,21 +8,25 @@ import {isCorrectSpec} from '../../helpers';
88import { Spec } from '../../types' ;
99
1010import { Controller } from './Controller' ;
11- import { isCorrectConfig } from './helpers ' ;
12- import { useCreateContext , useStore } from './hooks ' ;
13- import { DynamicFormConfig } from './types ' ;
11+ import { useCreateContext , useCreateSearchContext , useSearchStore , useStore } from './hooks ' ;
12+ import { DynamicFormConfig , FieldValue } from './types ' ;
13+ import { getDefaultSearchFunction , isCorrectConfig } from './utils ' ;
1414
1515export interface DynamicFieldProps {
1616 name : string ;
1717 spec : Spec ;
1818 config : DynamicFormConfig ;
1919 Monaco ?: React . ComponentType < MonacoEditorProps > ;
20+ search ?: string | ( ( spec : Spec , input : FieldValue , name : string ) => boolean ) ;
2021}
2122
22- export const DynamicField : React . FC < DynamicFieldProps > = ( { name, spec, config, Monaco} ) => {
23+ export const DynamicField : React . FC < DynamicFieldProps > = ( { name, spec, config, Monaco, search } ) => {
2324 const DynamicFormsCtx = useCreateContext ( ) ;
25+ const SearchContext = useCreateSearchContext ( ) ;
2426 const { tools, watcher} = useStore ( name ) ;
2527
28+ const { setField, removeField, isHiddenField} = useSearchStore ( name ) ;
29+
2630 const context = React . useMemo (
2731 ( ) => ( {
2832 config,
@@ -32,6 +36,16 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({name, spec, config, M
3236 [ tools , config , Monaco ] ,
3337 ) ;
3438
39+ const searchContext = React . useMemo (
40+ ( ) => ( {
41+ setField,
42+ removeField,
43+ isHiddenField,
44+ searchFunction : _ . isFunction ( search ) ? search : getDefaultSearchFunction ( search ) ,
45+ } ) ,
46+ [ isHiddenField , removeField , search , setField ] ,
47+ ) ;
48+
3549 const correctParams = React . useMemo (
3650 ( ) => _ . isString ( name ) && isCorrectSpec ( spec ) && isCorrectConfig ( config ) ,
3751 [ name , spec , config ] ,
@@ -40,14 +54,16 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({name, spec, config, M
4054 if ( correctParams ) {
4155 return (
4256 < DynamicFormsCtx . Provider value = { context } >
43- < Controller
44- spec = { spec }
45- name = { name }
46- parentOnChange = { null }
47- parentOnUnmount = { null }
48- initialValue = { _ . get ( tools . initialValue , name ) }
49- />
50- { watcher }
57+ < SearchContext . Provider value = { searchContext } >
58+ < Controller
59+ spec = { spec }
60+ name = { name }
61+ parentOnChange = { null }
62+ parentOnUnmount = { null }
63+ initialValue = { _ . get ( tools . initialValue , name ) }
64+ />
65+ { watcher }
66+ </ SearchContext . Provider >
5167 </ DynamicFormsCtx . Provider >
5268 ) ;
5369 }
0 commit comments