This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff0a9dc
commit 786bbdf
Showing
3 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
) | ||
} |
786bbdf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: