This repository was archived by the owner on Dec 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPositiveIntegerOnlyInput.tsx
More file actions
63 lines (60 loc) · 1.68 KB
/
PositiveIntegerOnlyInput.tsx
File metadata and controls
63 lines (60 loc) · 1.68 KB
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
import {TextField, TextFieldProps} from '@mui/material';
import {Controller, FieldValues, Path} from 'react-hook-form';
import React from 'react';
import {useTranslation} from 'next-i18next';
import {Control} from 'react-hook-form/dist/types/form';
interface PositiveIntegerOnlyInputProps<T extends FieldValues>
extends Omit<TextFieldProps,
| 'inputProps' | 'variant' | 'error' | 'helperText' | 'label'
| 'onChange' | 'onBlur' | 'value' | 'name' | 'ref'>
{
name: Path<T>;
control: Control<T>;
showError: boolean;
helperText: string;
min?: number;
inputLabel?: string;
required?: boolean;
}
const PositiveIntegerOnlyInput = function<T extends FieldValues>({
name, control, showError, helperText,
min = 1, inputLabel, required = true,
...others
}: PositiveIntegerOnlyInputProps<T>) {
const {t} = useTranslation('home');
return <Controller
name={name}
control={control}
rules={{
required: {
value: required,
message: t('addPieceDialog.required'),
},
pattern: {
value: /^\d+$/,
message: t('addPieceDialog.mustBeAInteger'),
},
min: {
value: min,
message: t('addPieceDialog.minimumIs', {min: 1}),
},
max: {
value: 999,
message: t('addPieceDialog.maximumIs', {max: 999}),
},
}}
render={({field: {ref: fieldRef, ...field}}) => (
<TextField
{...others}
{...field}
inputRef={fieldRef}
inputProps={{pattern: '\\d*'}}
variant="outlined"
error={showError}
helperText={helperText}
label={inputLabel ?? t('addPieceDialog.quantity')}
/>
)}
/>;
};
export default PositiveIntegerOnlyInput;