Skip to content

Commit

Permalink
fix: getValueProps should not be executed when name does not exist (
Browse files Browse the repository at this point in the history
#666)

* fix: `getValueProps` should not be executed when `name` does not exist

* test: update

* feat: improve

* feat: improve
  • Loading branch information
madocto authored Mar 28, 2024
1 parent c48cb7a commit cde8248
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F

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

const valueProps = mergedGetValueProps(value);
const valueProps = name !== undefined ? mergedGetValueProps(value) : {};

// warning when prop value is function
if (process.env.NODE_ENV !== 'production' && valueProps) {
Expand Down
21 changes: 21 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,27 @@ describe('Form.Basic', () => {
expect(container.querySelector('.anything')).toBeTruthy();
});

it('getValueProps should not be executed when name does not exist', async () => {
const getValueProps1 = jest.fn();
const getValueProps2 = jest.fn();

render(
<div>
<Form initialValues={{ test: 'bamboo' }}>
<Field getValueProps={getValueProps1}>
<span className="anything" />
</Field>
<Field getValueProps={getValueProps2}>
{() => <span className="anything" />}
</Field>
</Form>
</div>,
);

expect(getValueProps1).not.toHaveBeenCalled();
expect(getValueProps2).not.toHaveBeenCalled();
});

describe('shouldUpdate', () => {
it('work', async () => {
let isAllTouched: boolean;
Expand Down

0 comments on commit cde8248

Please sign in to comment.