-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidatedInput.tsx
140 lines (131 loc) · 3.05 KB
/
ValidatedInput.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
import {
ForwardedRef,
forwardRef,
HTMLInputTypeAttribute,
InputHTMLAttributes,
} from 'react'
import { useControlField, useField, useFormContext } from 'remix-validated-form'
import { cn } from '~/utilities/cn'
export const ValidatedHiddenInput = forwardRef<
HTMLInputElement,
Omit<ValidatedInput, 'type' | 'label' | 'placeholder'>
>((props, ref) =>
ValidatedInput({
...props,
label: '',
type: 'hidden',
ref: ref,
}),
)
export const ValidatedTextInput = forwardRef<
HTMLInputElement,
Omit<ValidatedInput, 'type'>
>((props, ref) =>
ValidatedInput({
...props,
type: 'text',
ref: ref,
}),
)
export const ValidatedCheckboxInput = forwardRef<
HTMLInputElement,
Omit<ValidatedInput, 'type'>
>((props, ref) =>
ValidatedInput({
...props,
type: 'checkbox',
ref: ref,
}),
)
type ValidatedInput = {
ref: ForwardedRef<HTMLInputElement>
name: string
label: string
value?: string
placeholder?: string
className?: string
type: HTMLInputTypeAttribute
submitOnChange?: boolean
} & Pick<InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'disabled'>
function ValidatedInput({
name,
value: defaultValue,
label,
placeholder,
className,
type,
onChange,
submitOnChange,
...props
}: ValidatedInput) {
const { error, getInputProps } = useField(name)
const { submit } = useFormContext()
const [value, setValue] = useControlField<string | undefined>(name)
const isCheckboxOrRadio = type === 'checkbox' || type === 'radio'
const isTextLike = type !== 'button' && !isCheckboxOrRadio
if (type === 'hidden') {
return (
<input
{...props}
{...getInputProps({
type: 'hidden',
value: value ?? defaultValue ?? '',
onChange: e => {
setValue(e.target.value)
onChange?.(e)
submitOnChange && submit()
},
})}
/>
)
}
if (isTextLike) {
return (
<div>
<input
className={cn('w-full', 'rounded-md', 'text-black', className)}
placeholder={placeholder}
type={type}
{...props}
{...getInputProps({
type,
value: value ?? defaultValue ?? '',
onChange: e => {
setValue(e.target.value)
onChange?.(e)
submitOnChange && submit()
},
})}
aria-label={label}
/>
{error && (
<div
className={cn('px-3 pt-3 pb-2 -mt-1', 'bg-error text-sm rounded-b')}
>
{error}
</div>
)}
</div>
)
}
if (isCheckboxOrRadio) {
return (
<input
className={cn(className, error && 'border-raspberry')}
type={type}
{...props}
{...getInputProps({
type,
value: value ?? defaultValue ?? 'on',
onChange: e => {
setValue(e.target.value)
onChange?.(e)
submitOnChange && submit()
},
})}
aria-label={label}
/>
)
}
return <>Unsupported input type</>
}