From c4aa8902cd846d974189794120fbba5ecd37dfb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Tue, 5 Mar 2024 14:16:30 +0800 Subject: [PATCH] fix: not crash if getValueProps return empty (#655) --- src/Field.tsx | 13 ++++++++----- tests/index.test.tsx | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Field.tsx b/src/Field.tsx index 1c983d4f..7f72d55c 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -59,8 +59,8 @@ export type MetaEvent = Meta & { destroy?: boolean }; export interface InternalFieldProps { children?: - | React.ReactElement - | ((control: ChildProps, meta: Meta, form: FormInstance) => React.ReactNode); + | React.ReactElement + | ((control: ChildProps, meta: Meta, form: FormInstance) => React.ReactNode); /** * Set up `dependencies` field. * When dependencies field update and current field is touched, @@ -581,10 +581,13 @@ class Field extends React.Component implements F const valueProps = mergedGetValueProps(value); // warning when prop value is function - if (process.env.NODE_ENV !== 'production') { + if (process.env.NODE_ENV !== 'production' && valueProps) { Object.keys(valueProps).forEach(key => { - warning(typeof valueProps[key] !== 'function', `It's not recommended to generate dynamic function prop by \`getValueProps\`. Please pass it to child component directly (prop: ${key})`) - }) + warning( + typeof valueProps[key] !== 'function', + `It's not recommended to generate dynamic function prop by \`getValueProps\`. Please pass it to child component directly (prop: ${key})`, + ); + }); } const control = { diff --git a/tests/index.test.tsx b/tests/index.test.tsx index b1ca3833..fa499b04 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -395,10 +395,23 @@ describe('Form.Basic', () => { , ); - // expect((container.querySelector('.anything').props() as any).light).toEqual('bamboo'); expect(container.querySelector('.anything')).toHaveAttribute('data-light', 'bamboo'); }); + it('getValueProps should not throw if return empty', async () => { + const { container } = render( +
+
+ null}> + + +
+
, + ); + + expect(container.querySelector('.anything')).toBeTruthy(); + }); + describe('shouldUpdate', () => { it('work', async () => { let isAllTouched: boolean;