This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathinput.tsx
More file actions
167 lines (151 loc) Β· 3.79 KB
/
input.tsx
File metadata and controls
167 lines (151 loc) Β· 3.79 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
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
import { useRef, useEffect, InputHTMLAttributes } from 'react'
import { useField, SubmitHandler, FormHandles } from '@unform/core'
import { Form } from '@unform/web'
/**
* This input component supports many <input> types, including:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
*
* Don't use it with the type `submit` or `reset`; otherwise, bugs will occur.
*/
interface Props {
name: string
type?:
| 'text'
| 'number'
| 'color'
| 'date'
| 'datetime-local'
| 'email'
| 'hidden'
| 'month'
| 'password'
| 'time'
| 'range'
| 'search'
| 'tel'
| 'url'
| 'week'
label?: string
value?: string
}
type InputProps = InputHTMLAttributes<HTMLInputElement> & Props
function Input({ name, type, label, value, ...rest }: InputProps) {
const inputRef = useRef(null)
const { fieldName, defaultValue, registerField, error } = useField(name)
/**
* If you add a value to the input, it will be considered the default
* This is useful when you have a `<input type="hidden" />`
*
* You can also remove it and use the `initialData` or set dynamically.
*/
const defaultInputValue = value || defaultValue
useEffect(() => {
registerField({
name: fieldName,
ref: inputRef,
getValue: ref => {
return ref.current.value
},
setValue: (ref, newValue) => {
ref.current.value = newValue
},
clearValue: ref => {
ref.current.value = ''
},
})
}, [fieldName, registerField])
return (
<div>
<label htmlFor={fieldName}>{label}</label>
<input
type={type || 'text'}
id={fieldName}
ref={inputRef}
defaultValue={defaultInputValue}
{...rest}
/>
{error && <span>{error}</span>}
</div>
)
}
/**
* Usage
*/
interface FormData {
name: string
color: string
number: string
secret: string
email: string
month: string
telephone: string
password: string
time: string
website: string
week: string
date: string
meeting: string
search: string
range: string
}
export default function App() {
const formRef = useRef<FormHandles>(null)
const handleSubmit: SubmitHandler<FormData> = data => {
console.log(data)
}
return (
<Form ref={formRef} onSubmit={handleSubmit}>
<Input label="Name" name="name" />
<Input label="Choose a color" name="color" type="color" />
<Input label="Choose a number" name="number" type="number" />
<Input name="secret" type="hidden" value="teste" />
<Input label="email" name="email" type="email" />
<Input label="Month" name="month" type="month" min="2020-09" />
<Input
label="Telephone"
name="telephone"
type="tel"
placeholder="Ex: XX-XXXXX-XXXX"
pattern="[0-9]{2}-[0-9]{5}-[0-9]{4}"
/>
<Input label="password" name="password" type="password" />
<Input label="time" name="time" type="time" min="09:00" max="18:00" />
<Input
label="website"
placeholder="https://example.com"
pattern="https://.*"
name="website"
type="url"
/>
<Input
label="week"
min="2021-W01"
max="2021-W52"
name="week"
type="week"
/>
<Input
label="date"
min="2021-01-01"
max="2021-12-31"
name="date"
type="date"
/>
<Input
label="meeting-time"
min="2020-06-07T00:00"
max="2020-06-14T00:00"
name="meeting"
type="datetime-local"
/>
<Input
label="search"
aria-label="Search through site content"
name="search"
type="search"
/>
<Input type="range" name="volume" label="Volume" min="0" max="10" />
<button type="submit">Submit</button>
</Form>
)
}