diff --git a/.gitignore b/.gitignore index 30396811043..2adc54e5c3c 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ cypress/screenshots !.yarn/releases !.yarn/sdks !.yarn/versions +.history \ No newline at end of file diff --git a/packages/ra-ui-materialui/src/input/NumberInput.tsx b/packages/ra-ui-materialui/src/input/NumberInput.tsx index e149714b989..21325c0974e 100644 --- a/packages/ra-ui-materialui/src/input/NumberInput.tsx +++ b/packages/ra-ui-materialui/src/input/NumberInput.tsx @@ -42,6 +42,7 @@ export const NumberInput = ({ readOnly, ...rest }: NumberInputProps) => { + const inputRef = React.useRef(null); const { field, fieldState: { error, invalid, isTouched }, @@ -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 }; @@ -113,7 +118,10 @@ export const NumberInput = ({ hasFocus.current = true; }; - const handleBlur = () => { + const handleBlur = (event: React.FocusEvent) => { + if (!event.currentTarget.value && inputRef.current) { + inputRef.current.value = ''; + } if (onBlurFromField) { onBlurFromField(); } @@ -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 (