Skip to content
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ cypress/screenshots
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.history
22 changes: 17 additions & 5 deletions packages/ra-ui-materialui/src/input/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const NumberInput = ({
readOnly,
...rest
}: NumberInputProps) => {
const inputRef = React.useRef<HTMLInputElement>(null);
const {
field,
fieldState: { error, invalid, isTouched },
Expand All @@ -58,7 +59,11 @@ export const NumberInput = ({
readOnly,
...rest,
});
const { onBlur: onBlurFromField } = field;
const {
ref: refFromField,
onBlur: onBlurFromField,
...otherFieldProps
} = field;

const inputProps = { ...overrideInputProps, step, min, max };

Expand Down Expand Up @@ -113,7 +118,10 @@ export const NumberInput = ({
hasFocus.current = true;
};

const handleBlur = () => {
const handleBlur = (event: React.FocusEvent<HTMLInputElement>) => {
if (!event.currentTarget.value && inputRef.current) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm afraid !event.currentTarget.value will return false when the value is 0, which will reset the value although 0 is a valid value for NumberInput.

inputRef.current.value = '';
}
if (onBlurFromField) {
onBlurFromField();
}
Expand All @@ -125,12 +133,16 @@ export const NumberInput = ({
const renderHelperText =
helperText !== false || ((isTouched || isSubmitted) && invalid);

const { ref, ...fieldWithoutRef } = field;
const setInputRefs = (instance: HTMLInputElement): void => {
refFromField(instance);
inputRef.current = instance;
};

return (
<TextField
id={id}
{...fieldWithoutRef}
inputRef={ref}
{...otherFieldProps}
inputRef={setInputRefs}
// use the locally controlled state instead of the react-hook-form field state
value={value}
onChange={handleChange}
Expand Down