Skip to content

Commit

Permalink
fix: error reported when the name of Form.List is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuiai01 committed Jun 27, 2023
1 parent cb1abcf commit f7f1853
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 2 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,7 @@ const FormList: React.FC<FormListProps> = ({
children,
...props
}) => {
warning(!!props.name, 'Form.List', 'Miss `name` prop.');
warning(typeof props.name === 'number' || !!props.name.length, 'Form.List', 'Miss `name` prop.');

const { getPrefixCls } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('form', customizePrefixCls);
Expand Down
44 changes: 44 additions & 0 deletions components/form/__tests__/list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,48 @@ 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}>
{(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();
});
});

0 comments on commit f7f1853

Please sign in to comment.