Skip to content

Commit 4ed7d5c

Browse files
authored
feat: search (#10)
1 parent 1cd17c3 commit 4ed7d5c

File tree

24 files changed

+302
-43
lines changed

24 files changed

+302
-43
lines changed

src/lib/core/components/Form/Controller.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {isCorrectSpec} from '../../helpers';
44
import {Spec} from '../../types';
55

66
import {useComponents, useDynamicFormsCtx, useField, useRender, useValidate} from './hooks';
7+
import {useSearch} from './hooks/useSearch';
78
import {FieldValue, ValidateError} from './types';
89

910
export interface ControllerProps<Value extends FieldValue, SpecType extends Spec> {
@@ -40,9 +41,10 @@ export const Controller = <Value extends FieldValue, SpecType extends Spec>({
4041
parentOnChange,
4142
parentOnUnmount,
4243
});
44+
const withSearch = useSearch(spec, renderProps.input.value, name);
4345

4446
if (_.isString(name) && isCorrectSpec(spec)) {
45-
return render(renderProps);
47+
return withSearch(render(renderProps));
4648
}
4749

4850
return null;

src/lib/core/components/Form/DynamicField.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@ import {isCorrectSpec} from '../../helpers';
88
import {Spec} from '../../types';
99

1010
import {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

1515
export 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
}

src/lib/core/components/Form/hooks/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ export * from './useRender';
66
export * from './useStore';
77
export * from './useValidate';
88
export * from './useMonaco';
9+
export * from './useSearchStore';
10+
export * from './useSearchContext';
11+
export * from './useSearch';
12+
export * from './useCreateSearchContext';

src/lib/core/components/Form/hooks/useComponents.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {isValidElementType} from 'react-is';
55

66
import {isCorrectSpec} from '../../../helpers';
77
import {FormValue, Spec} from '../../../types';
8-
import {isCorrectConfig} from '../helpers';
98
import {FieldValue, IndependentInputEntity, InputEntity, LayoutType, TypeConfig} from '../types';
9+
import {isCorrectConfig} from '../utils';
1010

1111
import {useDynamicFormsCtx} from './';
1212

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
3+
import _ from 'lodash';
4+
5+
import {SearchContext} from '../types';
6+
7+
const createContext = _.once(() => React.createContext({} as unknown as SearchContext));
8+
9+
export const useCreateSearchContext = () => createContext();

src/lib/core/components/Form/hooks/useField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import _ from 'lodash';
55
import {isArraySpec, isObjectSpec} from '../../../helpers';
66
import {Spec} from '../../../types';
77
import {OBJECT_ARRAY_CNT, OBJECT_ARRAY_FLAG, REMOVED_ITEM} from '../constants';
8-
import {isArrayItem, transformArrIn, transformArrOut} from '../helpers';
98
import {
109
DynamicFormsContext,
1110
FieldArrayValue,
1211
FieldRenderProps,
1312
FieldValue,
1413
ValidateError,
1514
} from '../types';
15+
import {isArrayItem, transformArrIn, transformArrOut} from '../utils';
1616

1717
export interface FieldProps<Value extends FieldValue, SpecType extends Spec> {
1818
name: string;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useSearch';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@import '../../../../../kit/styles/variables.scss';
2+
3+
.#{$ns}use-search {
4+
margin-bottom: 15px;
5+
6+
&_hidden {
7+
display: none;
8+
}
9+
10+
&:last-child {
11+
margin-bottom: 0;
12+
}
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from 'react';
2+
3+
import {block} from '../../../../../kit/utils';
4+
import {Spec} from '../../../../types';
5+
import {FieldValue} from '../../types';
6+
import {useSearchContext} from '../useSearchContext';
7+
8+
import './useSearch.scss';
9+
10+
const b = block('use-search');
11+
12+
export const useSearch = (spec: Spec, value: FieldValue, name: string) => {
13+
const {setField, removeField, isHiddenField, searchFunction} = useSearchContext();
14+
15+
const searchResult = React.useMemo(
16+
() => !searchFunction(spec, value, name),
17+
[name, searchFunction, spec, value],
18+
);
19+
20+
const hidden = React.useMemo(() => isHiddenField(name), [isHiddenField, name]);
21+
22+
const withSearch = React.useCallback(
23+
(children: JSX.Element | null) => <div className={b({hidden: hidden})}>{children}</div>,
24+
[hidden],
25+
);
26+
27+
React.useEffect(() => {
28+
setField(name, searchResult);
29+
}, [searchResult]);
30+
31+
React.useEffect(() => {
32+
return () => removeField(name);
33+
}, []);
34+
35+
return withSearch;
36+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
3+
import {useCreateSearchContext} from './index';
4+
5+
export const useSearchContext = () => {
6+
const SearchContext = useCreateSearchContext();
7+
const context = React.useContext(SearchContext);
8+
9+
return context;
10+
};

0 commit comments

Comments
 (0)