This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Examples): add react-hook-form example
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { FC } from 'react' | ||
import { useForm, Controller } from 'react-hook-form' | ||
import ColumnSelect from '../src' | ||
import type { OptionType } from '../src/types' | ||
import { chessOptions } from './data/options' | ||
|
||
type FormData = { | ||
username: string | ||
favorites: string[] | ||
} | ||
|
||
const ReactHookForm: FC = () => { | ||
const { register, control, setValue, handleSubmit } = useForm() | ||
|
||
const onSubmit = (data: FormData) => { | ||
console.info('Form Submitted:', data) | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<input {...register('username')} /> | ||
<Controller | ||
name='favorites' | ||
control={control} | ||
defaultValue={[]} | ||
render={() => ( | ||
<ColumnSelect | ||
options={chessOptions} | ||
onChange={(e) => | ||
setValue( | ||
'favorites', | ||
e.map((i: OptionType) => i.value) | ||
) | ||
} | ||
/> | ||
)} | ||
/> | ||
<input type='submit' /> | ||
</form> | ||
) | ||
} | ||
|
||
export default ReactHookForm |
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,8 @@ | ||
export const chessOptions = [ | ||
{ value: 'pawn', label: 'Pawn' }, | ||
{ value: 'bishop', label: 'Bishop' }, | ||
{ value: 'knight', label: 'Knight' }, | ||
{ value: 'rook', label: 'Rook' }, | ||
{ value: 'queen', label: 'Queen' }, | ||
{ value: 'king', label: 'King' }, | ||
] |