From c3225c072024a4cca519dd87efce6336f9c140fa Mon Sep 17 00:00:00 2001 From: JiaQi <112228030+Yuiai01@users.noreply.github.com> Date: Wed, 28 Jun 2023 09:46:20 +0800 Subject: [PATCH] fix: error reported when the name of Form.List is 0 (#43199) * fix: error reported when the name of Form.List is 0 * chore: add test case --- components/form/FormList.tsx | 9 +++- components/form/__tests__/list.test.tsx | 66 +++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/components/form/FormList.tsx b/components/form/FormList.tsx index eaff6f5520..55160ca9ea 100644 --- a/components/form/FormList.tsx +++ b/components/form/FormList.tsx @@ -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 { @@ -35,7 +35,12 @@ const FormList: React.FC = ({ 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); diff --git a/components/form/__tests__/list.test.tsx b/components/form/__tests__/list.test.tsx index 84ad7b5d67..1a2ca95660 100644 --- a/components/form/__tests__/list.test.tsx +++ b/components/form/__tests__/list.test.tsx @@ -262,4 +262,70 @@ describe('Form.List', () => { errorSpy.mockRestore(); }); + + it('no warning when name is 0', () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + render( +
+ + {(fields) => + fields.map((field) => ( + + + + )) + } + +
, + ); + + expect(errorSpy).not.toHaveBeenCalled(); + + errorSpy.mockRestore(); + }); + + it('warning when name is empty array', () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + render( +
+ + {(fields) => + fields.map((field) => ( + + + + )) + } + +
, + ); + + expect(errorSpy).toHaveBeenCalled(); + + errorSpy.mockRestore(); + }); + + it('warning when name is null', () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + render( +
+ + {(fields) => + fields.map((field) => ( + + + + )) + } + +
, + ); + + expect(errorSpy).toHaveBeenCalled(); + + errorSpy.mockRestore(); + }); });