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: not crash if getValueProps return empty #655

Merged
merged 1 commit into from
Mar 5, 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
13 changes: 8 additions & 5 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export type MetaEvent = Meta & { destroy?: boolean };

export interface InternalFieldProps<Values = any> {
children?:
| React.ReactElement
| ((control: ChildProps, meta: Meta, form: FormInstance<Values>) => React.ReactNode);
| React.ReactElement
| ((control: ChildProps, meta: Meta, form: FormInstance<Values>) => React.ReactNode);
/**
* Set up `dependencies` field.
* When dependencies field update and current field is touched,
Expand Down Expand Up @@ -581,10 +581,13 @@ class Field extends React.Component<InternalFieldProps, FieldState> 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 = {
Expand Down
15 changes: 14 additions & 1 deletion tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,23 @@ describe('Form.Basic', () => {
</div>,
);

// 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(
<div>
<Form initialValues={{ test: 'bamboo' }}>
<Field name="test" getValueProps={() => null}>
<span className="anything" />
</Field>
</Form>
</div>,
);

expect(container.querySelector('.anything')).toBeTruthy();
});

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