Skip to content

Commit cde8248

Browse files
authored
fix: getValueProps should not be executed when name does not exist (#666)
* fix: `getValueProps` should not be executed when `name` does not exist * test: update * feat: improve * feat: improve
1 parent c48cb7a commit cde8248

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Field.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
561561

562562
public getControlled = (childProps: ChildProps = {}) => {
563563
const {
564+
name,
564565
trigger,
565566
validateTrigger,
566567
getValueFromEvent,
@@ -582,7 +583,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
582583
// eslint-disable-next-line @typescript-eslint/no-explicit-any
583584
const originTriggerFunc: any = childProps[trigger];
584585

585-
const valueProps = mergedGetValueProps(value);
586+
const valueProps = name !== undefined ? mergedGetValueProps(value) : {};
586587

587588
// warning when prop value is function
588589
if (process.env.NODE_ENV !== 'production' && valueProps) {

tests/index.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,27 @@ describe('Form.Basic', () => {
412412
expect(container.querySelector('.anything')).toBeTruthy();
413413
});
414414

415+
it('getValueProps should not be executed when name does not exist', async () => {
416+
const getValueProps1 = jest.fn();
417+
const getValueProps2 = jest.fn();
418+
419+
render(
420+
<div>
421+
<Form initialValues={{ test: 'bamboo' }}>
422+
<Field getValueProps={getValueProps1}>
423+
<span className="anything" />
424+
</Field>
425+
<Field getValueProps={getValueProps2}>
426+
{() => <span className="anything" />}
427+
</Field>
428+
</Form>
429+
</div>,
430+
);
431+
432+
expect(getValueProps1).not.toHaveBeenCalled();
433+
expect(getValueProps2).not.toHaveBeenCalled();
434+
});
435+
415436
describe('shouldUpdate', () => {
416437
it('work', async () => {
417438
let isAllTouched: boolean;

0 commit comments

Comments
 (0)