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

fix: error reported when the name of Form.List is 0 #43199

Merged
merged 2 commits into from
Jun 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions components/form/FormList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { List } from 'rc-field-form';
import type { StoreValue, ValidatorRule } from 'rc-field-form/lib/interface';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import warning from '../_util/warning';
import { ConfigContext } from '../config-provider';
import { FormItemPrefixContext } from './context';

export interface FormListFieldData {
Expand Down Expand Up @@ -35,7 +35,12 @@ const FormList: React.FC<FormListProps> = ({
children,
...props
}) => {
warning(!!props.name, 'Form.List', 'Miss `name` prop.');
warning(
typeof props.name === 'number' ||
(Array.isArray(props.name) ? !!props.name.length : !!props.name),
'Form.List',
'Miss `name` prop.',
);

const { getPrefixCls } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('form', customizePrefixCls);
Expand Down
66 changes: 66 additions & 0 deletions components/form/__tests__/list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,70 @@ describe('Form.List', () => {

errorSpy.mockRestore();
});

it('no warning when name is 0', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

render(
<Form>
<Form.List name={0}>
zombieJ marked this conversation as resolved.
Show resolved Hide resolved
{(fields) =>
fields.map((field) => (
<Form.Item {...field} key={field.key}>
<Input />
</Form.Item>
))
}
</Form.List>
</Form>,
);

expect(errorSpy).not.toHaveBeenCalled();

errorSpy.mockRestore();
});

it('warning when name is empty array', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

render(
<Form>
<Form.List name={[]}>
{(fields) =>
fields.map((field) => (
<Form.Item {...field} key={field.key}>
<Input />
</Form.Item>
))
}
</Form.List>
</Form>,
);

expect(errorSpy).toHaveBeenCalled();

errorSpy.mockRestore();
});

it('warning when name is null', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

render(
<Form>
<Form.List name={null!!}>
{(fields) =>
fields.map((field) => (
<Form.Item {...field} key={field.key}>
<Input />
</Form.Item>
))
}
</Form.List>
</Form>,
);

expect(errorSpy).toHaveBeenCalled();

errorSpy.mockRestore();
});
});