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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
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
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) {
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