-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathNumberInput.tsx
204 lines (186 loc) · 5.63 KB
/
NumberInput.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import TextField, { TextFieldProps } from '@mui/material/TextField';
import { useInput, FieldTitle } from 'ra-core';
import { CommonInputProps } from './CommonInputProps';
import { InputHelperText } from './InputHelperText';
import { sanitizeInputRestProps } from './sanitizeInputRestProps';
/**
* An Input component for a number
*
* @example
* <NumberInput source="nb_views" />
*
* You can customize the `step` props (which defaults to "any")
* @example
* <NumberInput source="nb_views" step={1} />
*
*/
export const NumberInput = ({
className,
defaultValue = null,
format = convertNumberToString,
helperText,
label,
margin,
onChange,
onBlur,
onFocus,
parse,
resource,
source,
step = 'any',
min,
max,
validate,
variant,
inputProps: overrideInputProps,
...rest
}: NumberInputProps) => {
const {
field,
fieldState: { error, invalid, isTouched },
formState: { isSubmitted },
id,
isRequired,
} = useInput({
defaultValue,
onBlur,
resource,
source,
validate,
...rest,
});
const { onBlur: onBlurFromField } = field;
const inputProps = { ...overrideInputProps, step, min, max };
// This is a controlled input that renders directly the string typed by the user.
// This string is converted to a number on change, and stored in the form state,
// but that number is not not displayed.
// This is to allow transitory values like '1.0' that will lead to '1.02'
// text typed by the user and displayed in the input, unparsed
const [value, setValue] = React.useState(format(field.value));
const hasFocus = React.useRef(false);
// update the input text when the record changes
React.useEffect(() => {
if (!hasFocus.current) {
const stringValue = format(field.value);
setValue(value => (value !== stringValue ? stringValue : value));
}
}, [field.value, format]); // eslint-disable-line react-hooks/exhaustive-deps
// update the input text when the user types in the input
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (onChange) {
onChange(event);
}
if (
typeof event.target === 'undefined' ||
typeof event.target.value === 'undefined'
) {
return;
}
const target = event.target;
setValue(target.value);
const newValue =
target.valueAsNumber !== undefined &&
target.valueAsNumber !== null &&
!isNaN(target.valueAsNumber)
? parse
? parse(target.valueAsNumber)
: target.valueAsNumber
: parse
? parse(target.value)
: convertStringToNumber(target.value);
field.onChange(newValue);
};
const handleFocus = (event: React.FocusEvent<HTMLInputElement>) => {
if (onFocus) {
onFocus(event);
}
hasFocus.current = true;
};
const handleBlur = () => {
if (onBlurFromField) {
onBlurFromField();
}
hasFocus.current = false;
const stringValue = format(field.value);
setValue(value => (value !== stringValue ? stringValue : value));
};
const renderHelperText =
helperText !== false || ((isTouched || isSubmitted) && invalid);
return (
<TextField
id={id}
{...field}
// use the locally controlled state instead of the react-hook-form field state
value={value}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
className={clsx('ra-input', `ra-input-${source}`, className)}
type="number"
size="small"
variant={variant}
error={(isTouched || isSubmitted) && invalid}
helperText={
renderHelperText ? (
<InputHelperText
touched={isTouched || isSubmitted}
error={error?.message}
helperText={helperText}
/>
) : null
}
label={
<FieldTitle
label={label}
source={source}
resource={resource}
isRequired={isRequired}
/>
}
margin={margin}
inputProps={inputProps}
{...sanitizeInputRestProps(rest)}
/>
);
};
NumberInput.propTypes = {
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
PropTypes.element,
]),
resource: PropTypes.string,
source: PropTypes.string,
step: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
NumberInput.defaultProps = {
step: 'any',
textAlign: 'right',
};
export interface NumberInputProps
extends CommonInputProps,
Omit<
TextFieldProps,
| 'label'
| 'helperText'
| 'defaultValue'
| 'onChange'
| 'onBlur'
| 'type'
> {
step?: string | number;
min?: string | number;
max?: string | number;
}
const convertStringToNumber = value => {
if (value == null || value === '') {
return null;
}
const float = parseFloat(value);
return isNaN(float) ? 0 : float;
};
const convertNumberToString = value =>
value == null || isNaN(value) ? '' : value.toString();