Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
feat(Examples): add react-hook-form example
Browse files Browse the repository at this point in the history
  • Loading branch information
chr-ge committed May 13, 2021
1 parent 87cd535 commit 2487ac2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/ReactHookForm.tsx
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
8 changes: 8 additions & 0 deletions examples/data/options.ts
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' },
]

0 comments on commit 2487ac2

Please sign in to comment.