Skip to content
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
3 changes: 3 additions & 0 deletions examples/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const Demo = () => {
console.log('values:', values);
}}
style={{ border: '1px solid red', padding: 15 }}
preserve={false}
>
<Form.Field shouldUpdate>{() => JSON.stringify(form.getFieldsValue(), null, 2)}</Form.Field>

<List name="users">
{(fields, { add, remove }) => {
console.log('Demo Fields:', fields);
Expand Down
24 changes: 16 additions & 8 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ export interface InternalFieldProps {
initialValue?: any;
onReset?: () => void;
preserve?: boolean;

/** @private Passed by Form.List props. Do not use since it will break by path check. */
isListField?: boolean;
}

export interface FieldProps extends Omit<InternalFieldProps, 'name'> {
name?: NamePath;

/** @private Passed by Form.List props. */
isListField?: boolean;
}

export interface FieldState {
Expand All @@ -102,7 +102,7 @@ class Field extends React.Component<InternalFieldProps, FieldState, InternalForm
resetCount: 0,
};

private cancelRegisterFunc: (preserve?: boolean) => void | null = null;
private cancelRegisterFunc: (isListField?: boolean, preserve?: boolean) => void | null = null;

private destroy = false;

Expand Down Expand Up @@ -140,10 +140,10 @@ class Field extends React.Component<InternalFieldProps, FieldState, InternalForm
}

public cancelRegister = () => {
const { preserve } = this.props;
const { preserve, isListField } = this.props;

if (this.cancelRegisterFunc) {
this.cancelRegisterFunc(preserve);
this.cancelRegisterFunc(isListField, preserve);
}
this.cancelRegisterFunc = null;
};
Expand Down Expand Up @@ -498,13 +498,21 @@ class Field extends React.Component<InternalFieldProps, FieldState, InternalForm
}
}

const WrapperField: React.FC<FieldProps> = ({ name, isListField, ...restProps }) => {
const WrapperField: React.FC<FieldProps> = ({ name, ...restProps }) => {
const namePath = name !== undefined ? getNamePath(name) : undefined;

let key: string = 'keep';
if (!isListField) {
if (!restProps.isListField) {
key = `_${(namePath || []).join('_')}`;
}

if (process.env.NODE_ENV !== 'production') {
warning(
restProps.preserve !== false || !restProps.isListField,
'`preserve` should not apply on Form.List fields.',
);
}

return <Field key={key} name={namePath} {...restProps} />;
};

Expand Down
4 changes: 2 additions & 2 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,12 @@ export class FormStore {
}

// un-register field callback
return (preserve?: boolean) => {
return (isListField?: boolean, preserve?: boolean) => {
this.fieldEntities = this.fieldEntities.filter(item => item !== entity);

// Clean up store value if preserve
const mergedPreserve = preserve !== undefined ? preserve : this.preserve;
if (mergedPreserve === false) {
if (mergedPreserve === false && !isListField) {
const namePath = entity.getNamePath();
if (this.getFieldValue(namePath) !== undefined) {
this.store = setValue(this.store, namePath, undefined);
Expand Down
67 changes: 65 additions & 2 deletions tests/preserve.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-template-curly-in-string */
/* eslint-disable no-template-curly-in-string, arrow-body-style */
import React from 'react';
import { mount } from 'enzyme';
import Form from '../src';
import Form, { FormInstance } from '../src';
import InfoField from './common/InfoField';
import timeout from './common/timeout';

Expand Down Expand Up @@ -74,5 +74,68 @@ describe('Form.Preserve', () => {
await matchTest(true, { keep: 233 });
await matchTest(false, { keep: 233, remove: 666 });
});

it('form perishable should not crash Form.List', async () => {
let form: FormInstance;

const wrapper = mount(
<Form
initialValues={{ list: ['light', 'bamboo', 'little'] }}
preserve={false}
ref={instance => {
form = instance;
}}
>
<Form.List name="list">
{(fields, { remove }) => {
return (
<div>
{fields.map(field => (
<Form.Field {...field}>
<input />
</Form.Field>
))}
<button
type="button"
onClick={() => {
remove(0);
}}
/>
</div>
);
}}
</Form.List>
</Form>,
);

wrapper.find('button').simulate('click');
wrapper.update();

expect(form.getFieldsValue()).toEqual({ list: ['bamboo', 'little'] });
});

it('warning when Form.List use preserve', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

mount(
<Form initialValues={{ list: ['bamboo'] }}>
<Form.List name="list">
{fields =>
fields.map(field => (
<Form.Field {...field} preserve={false}>
<input />
</Form.Field>
))
}
</Form.List>
</Form>,
);

expect(errorSpy).toHaveBeenCalledWith(
'Warning: `preserve` should not apply on Form.List fields.',
);

errorSpy.mockRestore();
});
});
/* eslint-enable no-template-curly-in-string */