Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getValueProps should not be executed when name does not exist #666

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" />
Copy link

@jongwong jongwong Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lack of case when children is a function.

</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
Loading