-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formiks): add FormikCheckbox (#22)
- Loading branch information
1 parent
37a1746
commit f018046
Showing
3 changed files
with
73 additions
and
2 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,26 @@ | ||
import { useField } from 'formik' | ||
import { useCallback, useMemo } from 'react' | ||
|
||
import { Checkbox } from '../fields/Checkbox' | ||
|
||
import type { CheckboxProps } from '../fields/Checkbox' | ||
|
||
export type FormikCheckboxProps = Omit<CheckboxProps, 'checked' | 'defaultChecked' | 'onChange'> & { | ||
name: string | ||
} | ||
export function FormikCheckbox({ name, ...originalProps }: FormikCheckboxProps) { | ||
const [field, , helpers] = useField(name) | ||
|
||
const value = useMemo(() => field.value, [field.value]) | ||
const setValue = useMemo(() => helpers.setValue, [helpers.setValue]) | ||
|
||
// We don't include `setValues` in `useCallback()` dependencok calls | ||
// both because it is useless and it will trigger infinite ho | ||
const handleChange = useCallback((isChecked: boolean) => { | ||
setValue(isChecked) | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
return <Checkbox defaultChecked={value} name={name} onChange={handleChange} {...originalProps} /> | ||
} |
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,45 @@ | ||
import { Formik } from 'formik' | ||
import { useState } from 'react' | ||
|
||
import { FormikEffect, FormikCheckbox } from '../../src' | ||
import { Output } from '../_components/Output' | ||
import { noop } from '../_utils/noop' | ||
|
||
import type { FormikCheckboxProps } from '../../src' | ||
|
||
const args: FormikCheckboxProps = { | ||
label: 'Check me', | ||
name: 'myCheckbox' | ||
} | ||
|
||
export default { | ||
title: 'Formiks/FormikCheckbox', | ||
component: FormikCheckbox, | ||
|
||
argTypes: {}, | ||
|
||
args | ||
} | ||
|
||
export const _FormikCheckbox = (props: FormikCheckboxProps) => { | ||
const [outputValue, setOutputValue] = useState< | ||
| { | ||
myCheckbox?: boolean | ||
} | ||
| '∅' | ||
>('∅') | ||
|
||
return ( | ||
<> | ||
<Formik initialValues={{}} onSubmit={noop}> | ||
<> | ||
<FormikEffect onChange={setOutputValue} /> | ||
|
||
<FormikCheckbox {...props} /> | ||
</> | ||
</Formik> | ||
|
||
{outputValue !== '∅' && <Output value={outputValue} />} | ||
</> | ||
) | ||
} |