Skip to content

Commit d2b7ce6

Browse files
authored
feat: add destroyOnUnregister property (#206)
1 parent 2addc2c commit d2b7ce6

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

docs/lib.md

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

2324
### Controller

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22

3+
import get from 'lodash/get';
34
import isFunction from 'lodash/isFunction';
45
import isString from 'lodash/isString';
5-
import get from 'lodash/get';
66
import {isValidElementType} from 'react-is';
77
import type {MonacoEditorProps} from 'react-monaco-editor/lib/types';
88

@@ -30,6 +30,7 @@ export interface DynamicFieldProps {
3030
search?: string | ((spec: Spec, input: FieldValue, name: string) => boolean);
3131
generateRandomValue?: (spec: StringSpec) => string;
3232
withoutInsertFFDebounce?: boolean;
33+
destroyOnUnregister?: boolean;
3334
mutators?: DynamicFormMutators;
3435
__mirror?: WonderMirror;
3536
}
@@ -42,13 +43,14 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
4243
generateRandomValue,
4344
search,
4445
withoutInsertFFDebounce,
46+
destroyOnUnregister = true,
4547
mutators: externalMutators,
4648
__mirror,
4749
}) => {
4850
const DynamicFormsCtx = useCreateContext();
4951
const SearchContext = useCreateSearchContext();
5052
const {tools, store} = useStore(name);
51-
const watcher = useIntegrationFF(store, withoutInsertFFDebounce);
53+
const watcher = useIntegrationFF(store, withoutInsertFFDebounce, destroyOnUnregister);
5254
const {mutatorsStore, mutateDFState} = useMutators(externalMutators);
5355
const {store: searchStore, setField, removeField, isHiddenField} = useSearchStore();
5456

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {Field as FinalFormField, useForm} from 'react-final-form';
1010
import {AsyncValidateError, BaseValidateError, DynamicFieldStore, FieldValue} from '../types';
1111
import {transformArrOut} from '../utils';
1212

13-
export const useIntegrationFF = (store: DynamicFieldStore, withoutDebounce?: boolean) => {
13+
export const useIntegrationFF = (
14+
store: DynamicFieldStore,
15+
withoutDebounce?: boolean,
16+
destroyOnUnregister?: boolean,
17+
) => {
1418
const form = useForm();
1519

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

6569
React.useEffect(() => {
6670
return () => {
67-
if (store.name) {
71+
if (store.name && destroyOnUnregister) {
6872
form.change(store.name, undefined);
6973
}
7074
};

0 commit comments

Comments
 (0)