Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: search #10

Merged
merged 13 commits into from
Mar 23, 2023
4 changes: 3 additions & 1 deletion src/lib/core/components/Form/Controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {isCorrectSpec} from '../../helpers';
import {Spec} from '../../types';

import {useComponents, useDynamicFormsCtx, useField, useRender, useValidate} from './hooks';
import {useSearch} from './hooks/useSearch';
import {FieldValue, ValidateError} from './types';

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

if (_.isString(name) && isCorrectSpec(spec)) {
return render(renderProps);
return withSearch(render(renderProps));
}

return null;
Expand Down
40 changes: 28 additions & 12 deletions src/lib/core/components/Form/DynamicField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ import {isCorrectSpec} from '../../helpers';
import {Spec} from '../../types';

import {Controller} from './Controller';
import {isCorrectConfig} from './helpers';
import {useCreateContext, useStore} from './hooks';
import {DynamicFormConfig} from './types';
import {useCreateContext, useCreateSearchContext, useSearchStore, useStore} from './hooks';
import {DynamicFormConfig, FieldValue} from './types';
import {getDefaultSearchFunction, isCorrectConfig} from './utils';

export interface DynamicFieldProps {
name: string;
spec: Spec;
config: DynamicFormConfig;
Monaco?: React.ComponentType<MonacoEditorProps>;
search?: string | ((spec: Spec, input: FieldValue, name: string) => boolean);
}

export const DynamicField: React.FC<DynamicFieldProps> = ({name, spec, config, Monaco}) => {
export const DynamicField: React.FC<DynamicFieldProps> = ({name, spec, config, Monaco, search}) => {
const DynamicFormsCtx = useCreateContext();
const SearchContext = useCreateSearchContext();
const {tools, watcher} = useStore(name);

const {setField, removeField, isHiddenField} = useSearchStore(name);

const context = React.useMemo(
() => ({
config,
Expand All @@ -32,6 +36,16 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({name, spec, config, M
[tools, config, Monaco],
);

const searchContext = React.useMemo(
() => ({
setField,
removeField,
isHiddenField,
searchFunction: _.isFunction(search) ? search : getDefaultSearchFunction(search),
}),
[isHiddenField, removeField, search, setField],
);

const correctParams = React.useMemo(
() => _.isString(name) && isCorrectSpec(spec) && isCorrectConfig(config),
[name, spec, config],
Expand All @@ -40,14 +54,16 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({name, spec, config, M
if (correctParams) {
return (
<DynamicFormsCtx.Provider value={context}>
<Controller
spec={spec}
name={name}
parentOnChange={null}
parentOnUnmount={null}
initialValue={_.get(tools.initialValue, name)}
/>
{watcher}
<SearchContext.Provider value={searchContext}>
<Controller
spec={spec}
name={name}
parentOnChange={null}
parentOnUnmount={null}
initialValue={_.get(tools.initialValue, name)}
/>
{watcher}
</SearchContext.Provider>
</DynamicFormsCtx.Provider>
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/core/components/Form/hooks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export * from './useRender';
export * from './useStore';
export * from './useValidate';
export * from './useMonaco';
export * from './useSearchStore';
export * from './useSearchContext';
export * from './useSearch';
export * from './useCreateSearchContext';
2 changes: 1 addition & 1 deletion src/lib/core/components/Form/hooks/useComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {isValidElementType} from 'react-is';

import {isCorrectSpec} from '../../../helpers';
import {FormValue, Spec} from '../../../types';
import {isCorrectConfig} from '../helpers';
import {FieldValue, IndependentInputEntity, InputEntity, LayoutType, TypeConfig} from '../types';
import {isCorrectConfig} from '../utils';

import {useDynamicFormsCtx} from './';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

import _ from 'lodash';

import {SearchContext} from '../types';

const createContext = _.once(() => React.createContext({} as unknown as SearchContext));

export const useCreateSearchContext = () => createContext();
2 changes: 1 addition & 1 deletion src/lib/core/components/Form/hooks/useField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import _ from 'lodash';
import {isArraySpec, isObjectSpec} from '../../../helpers';
import {Spec} from '../../../types';
import {OBJECT_ARRAY_CNT, OBJECT_ARRAY_FLAG, REMOVED_ITEM} from '../constants';
import {isArrayItem, transformArrIn, transformArrOut} from '../helpers';
import {
DynamicFormsContext,
FieldArrayValue,
FieldRenderProps,
FieldValue,
ValidateError,
} from '../types';
import {isArrayItem, transformArrIn, transformArrOut} from '../utils';

export interface FieldProps<Value extends FieldValue, SpecType extends Spec> {
name: string;
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/components/Form/hooks/useSearch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useSearch';
13 changes: 13 additions & 0 deletions src/lib/core/components/Form/hooks/useSearch/useSearch.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@import '../../../../../kit/styles/variables.scss';

.#{$ns}use-search {
margin-bottom: 15px;
bocembocem marked this conversation as resolved.
Show resolved Hide resolved

&_hidden {
display: none;
}

&:last-child {
margin-bottom: 0;
}
}
36 changes: 36 additions & 0 deletions src/lib/core/components/Form/hooks/useSearch/useSearch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';

import {block} from '../../../../../kit/utils';
import {Spec} from '../../../../types';
import {FieldValue} from '../../types';
import {useSearchContext} from '../useSearchContext';

import './useSearch.scss';

const b = block('use-search');

export const useSearch = (spec: Spec, value: FieldValue, name: string) => {
const {setField, removeField, isHiddenField, searchFunction} = useSearchContext();

const searchResult = React.useMemo(
() => !searchFunction(spec, value, name),
[name, searchFunction, spec, value],
);

const hidden = React.useMemo(() => isHiddenField(name), [isHiddenField, name]);

const withSearch = React.useCallback(
(children: JSX.Element | null) => <div className={b({hidden: hidden})}>{children}</div>,
[hidden],
);

React.useEffect(() => {
setField(name, searchResult);
}, [searchResult]);

React.useEffect(() => {
return () => removeField(name);
}, []);
NasgulNexus marked this conversation as resolved.
Show resolved Hide resolved

return withSearch;
};
10 changes: 10 additions & 0 deletions src/lib/core/components/Form/hooks/useSearchContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';

import {useCreateSearchContext} from './index';

export const useSearchContext = () => {
const SearchContext = useCreateSearchContext();
const context = React.useContext(SearchContext);

return context;
};
48 changes: 48 additions & 0 deletions src/lib/core/components/Form/hooks/useSearchStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';

import _ from 'lodash';

import {getParentName} from '../';

export const useSearchStore = (name: string) => {
const [store, setStore] = React.useState({[name]: false});

const isHiddenField = React.useCallback(
(name: string) => {
const selfFlag = store[name];

if (selfFlag === false) {
return false;
}

let parentName = getParentName(name);

while (parentName) {
if (store[parentName] === false) {
return false;
}

parentName = getParentName(parentName);
}
NasgulNexus marked this conversation as resolved.
Show resolved Hide resolved

for (const key of Object.keys(store)) {
if (key.includes(name + '.') && !store[key]) {
return false;
}
}

return true;
},
[store],
);

return {
store,
setField: (name: string, search: boolean) =>
setStore((store) => ({...store, [name]: search})),
removeField: (name: string) => {
setStore((store) => _.omit(store, name));
},
isHiddenField,
};
};
2 changes: 1 addition & 1 deletion src/lib/core/components/Form/hooks/useStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import React from 'react';
import _ from 'lodash';
import {Field as FinalFormField, useForm} from 'react-final-form';

import {transformArrIn, transformArrOut} from '../helpers';
import {
AsyncValidateError,
BaseValidateError,
FieldObjectValue,
FieldValue,
ValidateError,
} from '../types';
import {transformArrIn, transformArrOut} from '../utils';

export interface DynamicFieldStore {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/components/Form/hooks/useValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import _ from 'lodash';

import {isCorrectSpec} from '../../../helpers';
import {FormValue, Spec} from '../../../types';
import {isCorrectConfig} from '../helpers';
import {FieldValue, TypeConfig} from '../types';
import {isCorrectConfig} from '../utils';

import {useDynamicFormsCtx} from './';

Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/components/Form/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './constants';
export * from './Controller';
export * from './DynamicField';
export * from './helpers';
export * from './types';
export * from './utils';
1 change: 1 addition & 0 deletions src/lib/core/components/Form/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './object';
export * from './string';
export * from './validators';
export * from './value';
export * from './search';
10 changes: 10 additions & 0 deletions src/lib/core/components/Form/types/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Spec} from '../../../types';

import {FieldValue} from './value';

export interface SearchContext {
setField: (name: string, search: boolean) => void;
removeField: (name: string) => void;
isHiddenField: (name: string) => boolean;
searchFunction: (spec: Spec, value: FieldValue, name: string) => boolean;
}
NasgulNexus marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import _ from 'lodash';

import {SpecTypes} from '../../constants';
import {FormValue, ObjectValue} from '../../types';

import {OBJECT_ARRAY_CNT, OBJECT_ARRAY_FLAG, REMOVED_ITEM} from './constants';
import {SpecTypes} from '../../../constants';
import {FormValue, ObjectValue} from '../../../types';
import {OBJECT_ARRAY_CNT, OBJECT_ARRAY_FLAG, REMOVED_ITEM} from '../constants';

export const isCorrectConfig = (candidate: any) =>
Object.values(SpecTypes).every(
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/components/Form/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './common';
export * from './search';
21 changes: 21 additions & 0 deletions src/lib/core/components/Form/utils/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {Spec} from '../../../types';

export const getParentName = (name: string) => {
const index = name.lastIndexOf('.');

if (index !== -1) {
return name.substring(0, index);
}

return undefined;
};

export const getDefaultSearchFunction = (search?: string) => (spec: Spec) => {
if (search) {
return Boolean(
spec.viewSpec.layoutTitle?.toLowerCase().includes(search.trim().toLowerCase()),
);
}

return true;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
.#{$ns}table-array {
&__table {
margin-bottom: 10px;

.yc-table__cell {
border-bottom: 0px transparent;
}
}

&__row {
.yc-table__cell {
border-bottom: 0px transparent;
border-top: 1px solid var(--yc-color-line-generic);
}

&_hidden {
display: none;
}
}

&__cell {
Expand Down
Loading