-
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 FormikMultiSelect (#27)
- Loading branch information
1 parent
e1123b1
commit 414c4da
Showing
6 changed files
with
82 additions
and
5 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
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,27 @@ | ||
import { useField } from 'formik' | ||
import { useCallback, useEffect } from 'react' | ||
|
||
import { MultiSelect } from '../fields/MultiSelect' | ||
|
||
import type { MultiSelectProps } from '../fields/MultiSelect' | ||
|
||
export type FormikMultiSelectProps = Omit<MultiSelectProps, 'defaultValue' | 'onChange'> & { | ||
name: string | ||
} | ||
export function FormikMultiSelect({ name, ...originalProps }: FormikMultiSelectProps) { | ||
const [, , helpers] = useField(name) | ||
// We don't include `setValues` in `useCallback()` and `useEffect()` dependencies | ||
// both because it is useless and it will trigger infinite hook calls | ||
const { setValue } = helpers | ||
|
||
const handleChange = useCallback((nextValue: string[] | undefined) => { | ||
setValue(nextValue) | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEffect(() => () => setValue(undefined), []) | ||
|
||
return <MultiSelect 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,51 @@ | ||
import { Formik } from 'formik' | ||
import { useState } from 'react' | ||
|
||
import { FormikEffect, FormikMultiSelect } from '../../src' | ||
import { Output } from '../_components/Output' | ||
import { noop } from '../_utils/noop' | ||
|
||
import type { FormikMultiSelectProps } from '../../src' | ||
|
||
const args: FormikMultiSelectProps = { | ||
placeholder: 'Pick some options', | ||
name: 'myMultiSelect', | ||
options: [ | ||
{ label: 'First Option', value: 'FIRST_OPTION' }, | ||
{ label: 'Second Option', value: 'SECOND_OPTION' }, | ||
{ label: 'Third Option', value: 'THIRD_OPTION' }, | ||
{ label: 'A Very Very Long Option', value: 'A_VERY_VERY_LONG_OPTION' } | ||
] | ||
} | ||
|
||
export default { | ||
title: 'Formiks/FormikMultiSelect', | ||
component: FormikMultiSelect, | ||
|
||
argTypes: {}, | ||
|
||
args | ||
} | ||
|
||
export const _FormikMultiSelect = (props: FormikMultiSelectProps) => { | ||
const [outputValue, setOutputValue] = useState< | ||
| { | ||
mySelect?: string[] | ||
} | ||
| '∅' | ||
>('∅') | ||
|
||
return ( | ||
<> | ||
<Formik initialValues={{}} onSubmit={noop}> | ||
<> | ||
<FormikEffect onChange={setOutputValue} /> | ||
|
||
<FormikMultiSelect {...props} /> | ||
</> | ||
</Formik> | ||
|
||
{outputValue !== '∅' && <Output value={outputValue} />} | ||
</> | ||
) | ||
} |
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