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 useInput callbacks are mutable #8824

Merged
merged 1 commit into from
Apr 13, 2023
Merged
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
43 changes: 23 additions & 20 deletions packages/ra-core/src/form/useInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useFormGroupContext } from './useFormGroupContext';
import { useGetValidationErrorMessage } from './useGetValidationErrorMessage';
import { useFormGroups } from './useFormGroups';
import { useApplyInputDefaultValues } from './useApplyInputDefaultValues';
import { useEvent } from '../util';

// replace null or undefined values by empty string to avoid controlled/uncontrolled input warning
const defaultFormat = (value: any) => (value == null ? '' : value);
Expand All @@ -30,8 +31,8 @@ export const useInput = (props: InputProps): UseInputValue => {
id,
isRequired: isRequiredOption,
name,
onBlur,
onChange,
onBlur: initialOnBlur,
onChange: initialOnChange,
parse = defaultParse,
source,
validate,
Expand Down Expand Up @@ -89,27 +90,29 @@ export const useInput = (props: InputProps): UseInputValue => {
// no value for the input.
useApplyInputDefaultValues(props);

const onBlur = useEvent((...event: any[]) => {
if (initialOnBlur) {
initialOnBlur(...event);
}
controllerField.onBlur();
});

const onChange = useEvent((...event: any[]) => {
if (initialOnChange) {
initialOnChange(...event);
}
const eventOrValue = (props.type === 'checkbox' &&
event[0]?.target?.value === 'on'
? event[0].target.checked
: event[0]?.target?.value ?? event[0]) as any;
controllerField.onChange(parse ? parse(eventOrValue) : eventOrValue);
});

const field = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't we memo this ? Or even the end result of the function?

Copy link
Member Author

Choose a reason for hiding this comment

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

We haven't received any bug/performance report regarding this

...controllerField,
value: format ? format(controllerField.value) : controllerField.value,
onBlur: (...event: any[]) => {
if (onBlur) {
onBlur(...event);
}
controllerField.onBlur();
},
onChange: (...event: any[]) => {
if (onChange) {
onChange(...event);
}
const eventOrValue = (props.type === 'checkbox' &&
event[0]?.target?.value === 'on'
? event[0].target.checked
: event[0]?.target?.value ?? event[0]) as any;
controllerField.onChange(
parse ? parse(eventOrValue) : eventOrValue
);
},
onBlur,
onChange,
};

return {
Expand Down