Skip to content

Commit c4aa890

Browse files
authored
fix: not crash if getValueProps return empty (#655)
1 parent 5bb7102 commit c4aa890

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/Field.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export type MetaEvent = Meta & { destroy?: boolean };
5959

6060
export interface InternalFieldProps<Values = any> {
6161
children?:
62-
| React.ReactElement
63-
| ((control: ChildProps, meta: Meta, form: FormInstance<Values>) => React.ReactNode);
62+
| React.ReactElement
63+
| ((control: ChildProps, meta: Meta, form: FormInstance<Values>) => React.ReactNode);
6464
/**
6565
* Set up `dependencies` field.
6666
* When dependencies field update and current field is touched,
@@ -581,10 +581,13 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
581581
const valueProps = mergedGetValueProps(value);
582582

583583
// warning when prop value is function
584-
if (process.env.NODE_ENV !== 'production') {
584+
if (process.env.NODE_ENV !== 'production' && valueProps) {
585585
Object.keys(valueProps).forEach(key => {
586-
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})`)
587-
})
586+
warning(
587+
typeof valueProps[key] !== 'function',
588+
`It's not recommended to generate dynamic function prop by \`getValueProps\`. Please pass it to child component directly (prop: ${key})`,
589+
);
590+
});
588591
}
589592

590593
const control = {

tests/index.test.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,23 @@ describe('Form.Basic', () => {
395395
</div>,
396396
);
397397

398-
// expect((container.querySelector('.anything').props() as any).light).toEqual('bamboo');
399398
expect(container.querySelector('.anything')).toHaveAttribute('data-light', 'bamboo');
400399
});
401400

401+
it('getValueProps should not throw if return empty', async () => {
402+
const { container } = render(
403+
<div>
404+
<Form initialValues={{ test: 'bamboo' }}>
405+
<Field name="test" getValueProps={() => null}>
406+
<span className="anything" />
407+
</Field>
408+
</Form>
409+
</div>,
410+
);
411+
412+
expect(container.querySelector('.anything')).toBeTruthy();
413+
});
414+
402415
describe('shouldUpdate', () => {
403416
it('work', async () => {
404417
let isAllTouched: boolean;

0 commit comments

Comments
 (0)