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

Commit

Permalink
docs: Add Select example
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroschmitz committed Mar 16, 2021
1 parent ff0a9dc commit 786bbdf
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/README.md
Expand Up @@ -6,10 +6,11 @@ needs, hiding labels, styling error messages, etc.

| Name | Link |
| -------- | --------------------------------------------------------------------- |
| Checkbox | [JavaScript](./checkbox.js) - [TypeScript](./typescript/checkbox.tsx) |
| Input | [JavaScript](./input.js) - [TypeScript](./typescript/input.tsx) |
| Radio | [JavaScript](./radio.js) - [TypeScript](./typescript/radio.tsx) |
| Select | [JavaScript](./select.js) - [TypeScript](./typescript/select.tsx) |
| Textarea | [JavaScript](./textarea.js) - [TypeScript](./typescript/textarea.tsx) |
| Checkbox | [JavaScript](./checkbox.js) - [TypeScript](./typescript/checkbox.tsx) |

## Add an example

Expand Down
83 changes: 83 additions & 0 deletions examples/select.js
@@ -0,0 +1,83 @@
import { useRef, useEffect } from 'react'

import { useField } from '@unform/core'
import { Form } from '@unform/web'

/**
* Select component for Unform (without React Select)
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
*/
function Select({ name, label, children, ...rest }) {
const selectRef = useRef(null)

const { fieldName, defaultValue, registerField, error } = useField(name)

useEffect(() => {
registerField({
ref: selectRef,
name: fieldName,
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>

<select
id={fieldName}
ref={selectRef}
defaultValue={defaultValue}
{...rest}
>
{children}
</select>

{error && <span className="error">{error}</span>}
</div>
)
}

/**
* Usage
*/
export default function App() {
const formRef = useRef(null)

const handleSubmit = data => {
console.log(data)
}

const selectOptions = [
{ value: 'brazil', label: 'Brazil' },
{ value: 'usa', label: 'USA' },
{ value: 'argentina', label: 'Argentina' },
]

return (
<Form ref={formRef} onSubmit={handleSubmit}>
<Select
name="country"
label="Choose your country"
options={selectOptions}
>
{selectOptions.map(option => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</Select>

<button type="submit">Submit</button>
</Form>
)
}
92 changes: 92 additions & 0 deletions examples/typescript/select.tsx
@@ -0,0 +1,92 @@
import { useRef, useEffect, ReactNode, SelectHTMLAttributes } from 'react'

import { useField, SubmitHandler, FormHandles } from '@unform/core'
import { Form } from '@unform/web'

interface SelectProps {
name: string
label: string
children: ReactNode
}

type Props = SelectHTMLAttributes<HTMLSelectElement> & SelectProps

/**
* Select component for Unform (without React Select)
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
*/
function Select({ name, label, children, ...rest }: Props) {
const selectRef = useRef<HTMLSelectElement>(null)

const { fieldName, defaultValue, registerField, error } = useField(name)

useEffect(() => {
registerField({
ref: selectRef,
name: fieldName,
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>

<select
id={fieldName}
ref={selectRef}
defaultValue={defaultValue}
{...rest}
>
{children}
</select>

{error && <span className="error">{error}</span>}
</div>
)
}

/**
* Usage
*/

interface FormData {
username: string
}

export default function App() {
const formRef = useRef<FormHandles>(null)

const handleSubmit: SubmitHandler<FormData> = data => {
console.log(data)
}

const selectOptions = [
{ value: 'brazil', label: 'Brazil' },
{ value: 'usa', label: 'USA' },
{ value: 'argentina', label: 'Argentina' },
]

return (
<Form ref={formRef} onSubmit={handleSubmit}>
<Select name="country" label="Choose your country">
{selectOptions.map(option => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</Select>

<button type="submit">Submit</button>
</Form>
)
}

1 comment on commit 786bbdf

@vercel
Copy link

@vercel vercel bot commented on 786bbdf Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.