Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 786bbdf

Browse files
committed
docs: Add Select example
1 parent ff0a9dc commit 786bbdf

3 files changed

Lines changed: 177 additions & 1 deletion

File tree

examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ needs, hiding labels, styling error messages, etc.
66

77
| Name | Link |
88
| -------- | --------------------------------------------------------------------- |
9+
| Checkbox | [JavaScript](./checkbox.js) - [TypeScript](./typescript/checkbox.tsx) |
910
| Input | [JavaScript](./input.js) - [TypeScript](./typescript/input.tsx) |
1011
| Radio | [JavaScript](./radio.js) - [TypeScript](./typescript/radio.tsx) |
12+
| Select | [JavaScript](./select.js) - [TypeScript](./typescript/select.tsx) |
1113
| Textarea | [JavaScript](./textarea.js) - [TypeScript](./typescript/textarea.tsx) |
12-
| Checkbox | [JavaScript](./checkbox.js) - [TypeScript](./typescript/checkbox.tsx) |
1314

1415
## Add an example
1516

examples/select.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { useRef, useEffect } from 'react'
2+
3+
import { useField } from '@unform/core'
4+
import { Form } from '@unform/web'
5+
6+
/**
7+
* Select component for Unform (without React Select)
8+
*
9+
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
10+
*/
11+
function Select({ name, label, children, ...rest }) {
12+
const selectRef = useRef(null)
13+
14+
const { fieldName, defaultValue, registerField, error } = useField(name)
15+
16+
useEffect(() => {
17+
registerField({
18+
ref: selectRef,
19+
name: fieldName,
20+
getValue: ref => {
21+
return ref.current?.value
22+
},
23+
setValue: (ref, newValue) => {
24+
ref.current.value = newValue
25+
},
26+
clearValue: ref => {
27+
ref.current.value = ''
28+
},
29+
})
30+
}, [fieldName, registerField])
31+
32+
return (
33+
<div>
34+
<label htmlFor={fieldName}>{label}</label>
35+
36+
<select
37+
id={fieldName}
38+
ref={selectRef}
39+
defaultValue={defaultValue}
40+
{...rest}
41+
>
42+
{children}
43+
</select>
44+
45+
{error && <span className="error">{error}</span>}
46+
</div>
47+
)
48+
}
49+
50+
/**
51+
* Usage
52+
*/
53+
export default function App() {
54+
const formRef = useRef(null)
55+
56+
const handleSubmit = data => {
57+
console.log(data)
58+
}
59+
60+
const selectOptions = [
61+
{ value: 'brazil', label: 'Brazil' },
62+
{ value: 'usa', label: 'USA' },
63+
{ value: 'argentina', label: 'Argentina' },
64+
]
65+
66+
return (
67+
<Form ref={formRef} onSubmit={handleSubmit}>
68+
<Select
69+
name="country"
70+
label="Choose your country"
71+
options={selectOptions}
72+
>
73+
{selectOptions.map(option => (
74+
<option key={option.value} value={option.value}>
75+
{option.label}
76+
</option>
77+
))}
78+
</Select>
79+
80+
<button type="submit">Submit</button>
81+
</Form>
82+
)
83+
}

examples/typescript/select.tsx

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { useRef, useEffect, ReactNode, SelectHTMLAttributes } from 'react'
2+
3+
import { useField, SubmitHandler, FormHandles } from '@unform/core'
4+
import { Form } from '@unform/web'
5+
6+
interface SelectProps {
7+
name: string
8+
label: string
9+
children: ReactNode
10+
}
11+
12+
type Props = SelectHTMLAttributes<HTMLSelectElement> & SelectProps
13+
14+
/**
15+
* Select component for Unform (without React Select)
16+
*
17+
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
18+
*/
19+
function Select({ name, label, children, ...rest }: Props) {
20+
const selectRef = useRef<HTMLSelectElement>(null)
21+
22+
const { fieldName, defaultValue, registerField, error } = useField(name)
23+
24+
useEffect(() => {
25+
registerField({
26+
ref: selectRef,
27+
name: fieldName,
28+
getValue: ref => {
29+
return ref.current?.value
30+
},
31+
setValue: (ref, newValue) => {
32+
ref.current.value = newValue
33+
},
34+
clearValue: ref => {
35+
ref.current.value = ''
36+
},
37+
})
38+
}, [fieldName, registerField])
39+
40+
return (
41+
<div>
42+
<label htmlFor={fieldName}>{label}</label>
43+
44+
<select
45+
id={fieldName}
46+
ref={selectRef}
47+
defaultValue={defaultValue}
48+
{...rest}
49+
>
50+
{children}
51+
</select>
52+
53+
{error && <span className="error">{error}</span>}
54+
</div>
55+
)
56+
}
57+
58+
/**
59+
* Usage
60+
*/
61+
62+
interface FormData {
63+
username: string
64+
}
65+
66+
export default function App() {
67+
const formRef = useRef<FormHandles>(null)
68+
69+
const handleSubmit: SubmitHandler<FormData> = data => {
70+
console.log(data)
71+
}
72+
73+
const selectOptions = [
74+
{ value: 'brazil', label: 'Brazil' },
75+
{ value: 'usa', label: 'USA' },
76+
{ value: 'argentina', label: 'Argentina' },
77+
]
78+
79+
return (
80+
<Form ref={formRef} onSubmit={handleSubmit}>
81+
<Select name="country" label="Choose your country">
82+
{selectOptions.map(option => (
83+
<option key={option.value} value={option.value}>
84+
{option.label}
85+
</option>
86+
))}
87+
</Select>
88+
89+
<button type="submit">Submit</button>
90+
</Form>
91+
)
92+
}

0 commit comments

Comments
 (0)