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

Change Number input type to text instead of number #9757

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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