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: add destroyOnUnregister property #206

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This component serves as the primary entry point for drawing dynamic forms.
| Monaco | `React.ComponentType<MonacoEditorProps>` | | [MonacoEditor](https://github.com/react-monaco-editor/react-monaco-editor) component for Monaco [Input](./config.md#inputs) |
| search | `string \| function` | | A string or function for performing a form search |
| withoutInsertFFDebounce | `boolean` | | Flag that disables the delay before inserting data into the final-form store |
| destroyOnUnregister | `boolean` | | If true, the value of a field will be destroyed when that field is unregistered. Defaults to true |
| generateRandomValue | `function` | | Function that is necessary to generate a random value |

### Controller
Expand Down
6 changes: 4 additions & 2 deletions src/lib/core/components/Form/DynamicField.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';

import get from 'lodash/get';
import isFunction from 'lodash/isFunction';
import isString from 'lodash/isString';
import get from 'lodash/get';
import {isValidElementType} from 'react-is';
import type {MonacoEditorProps} from 'react-monaco-editor/lib/types';

Expand Down Expand Up @@ -30,6 +30,7 @@ export interface DynamicFieldProps {
search?: string | ((spec: Spec, input: FieldValue, name: string) => boolean);
generateRandomValue?: (spec: StringSpec) => string;
withoutInsertFFDebounce?: boolean;
destroyOnUnregister?: boolean;
mutators?: DynamicFormMutators;
__mirror?: WonderMirror;
}
Expand All @@ -42,13 +43,14 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
generateRandomValue,
search,
withoutInsertFFDebounce,
destroyOnUnregister = true,
mutators: externalMutators,
__mirror,
}) => {
const DynamicFormsCtx = useCreateContext();
const SearchContext = useCreateSearchContext();
const {tools, store} = useStore(name);
const watcher = useIntegrationFF(store, withoutInsertFFDebounce);
const watcher = useIntegrationFF(store, withoutInsertFFDebounce, destroyOnUnregister);
const {mutatorsStore, mutateDFState} = useMutators(externalMutators);
const {store: searchStore, setField, removeField, isHiddenField} = useSearchStore();

Expand Down
8 changes: 6 additions & 2 deletions src/lib/core/components/Form/hooks/useIntegrationFF.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {Field as FinalFormField, useForm} from 'react-final-form';
import {AsyncValidateError, BaseValidateError, DynamicFieldStore, FieldValue} from '../types';
import {transformArrOut} from '../utils';

export const useIntegrationFF = (store: DynamicFieldStore, withoutDebounce?: boolean) => {
export const useIntegrationFF = (
store: DynamicFieldStore,
withoutDebounce?: boolean,
destroyOnUnregister?: boolean,
) => {
const form = useForm();

const watcher = React.useMemo(() => {
Expand Down Expand Up @@ -64,7 +68,7 @@ export const useIntegrationFF = (store: DynamicFieldStore, withoutDebounce?: boo

React.useEffect(() => {
return () => {
if (store.name) {
if (store.name && destroyOnUnregister) {
form.change(store.name, undefined);
}
};
Expand Down
Loading