-
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 FormikSelect (#10)
* feat(formiks): add FormikEffect * feat(formiks): add FormikSelect
- Loading branch information
1 parent
c78de7c
commit cf3938d
Showing
16 changed files
with
208 additions
and
14 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,17 @@ | ||
import { useFormikContext } from 'formik' | ||
import { useEffect } from 'react' | ||
|
||
import type { Promisable } from 'type-fest' | ||
|
||
export type FormikEffectProps = { | ||
onChange: (nextValues: Record<string, any>) => Promisable<void> | ||
} | ||
export function FormikEffect({ onChange }: FormikEffectProps) { | ||
const { values } = useFormikContext<Record<string, any>>() | ||
|
||
useEffect(() => { | ||
onChange(values) | ||
}, [onChange, values]) | ||
|
||
return <></> | ||
} |
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 { Select } from '../fields/Select' | ||
|
||
import type { SelectProps } from '../fields/Select' | ||
|
||
export type FormikSelectProps = Omit<SelectProps, 'name' | 'onChange'> & { | ||
name: string | ||
} | ||
export function FormikSelect({ name, ...originalProps }: FormikSelectProps) { | ||
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((valueOrValues: string | string[] | undefined) => { | ||
setValue(valueOrValues) | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEffect(() => () => setValue(undefined), []) | ||
|
||
return <Select 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 @@ | ||
export function noop() {} |
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 { Field, Formik } from 'formik' | ||
import { useState } from 'react' | ||
|
||
import { FormikEffect } from '../../src' | ||
import { Output } from '../_components/Output' | ||
import { noop } from '../_utils/noop' | ||
|
||
export default { | ||
title: 'Formiks/FormikEffect', | ||
component: FormikEffect, | ||
|
||
argTypes: {}, | ||
|
||
args: {} | ||
} | ||
|
||
export const _FormikEffect = () => { | ||
const [outputValue, setOutputValue] = useState< | ||
| { | ||
aFormikField?: string | ||
} | ||
| '∅' | ||
>('∅') | ||
|
||
return ( | ||
<> | ||
<Formik initialValues={{}} onSubmit={noop}> | ||
<> | ||
<FormikEffect onChange={setOutputValue} /> | ||
|
||
<p> | ||
<code>{`<FormikEffect />`}</code> doesn’t show anything. It’s an inner <code>{`<Formik />`}</code> listener | ||
component allowing us to get form values outside of Formik context. | ||
</p> | ||
<p> | ||
Here is an example with a simple <code>{`<Formik.Field />`}</code> input: | ||
</p> | ||
<Field name="aFormikField" placeholder="Fill me!" type="text" /> | ||
</> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Formik } from 'formik' | ||
import { useState } from 'react' | ||
|
||
import { FormikEffect, FormikSelect } from '../../src' | ||
import { Output } from '../_components/Output' | ||
import { noop } from '../_utils/noop' | ||
|
||
import type { FormikSelectProps } from '../../src' | ||
|
||
export default { | ||
title: 'Formiks/FormikSelect', | ||
component: FormikSelect, | ||
|
||
argTypes: {}, | ||
|
||
args: { | ||
isMulti: false, | ||
name: 'mySelect', | ||
options: [ | ||
{ label: 'First Option', value: 'FIRST_OPTION' }, | ||
{ label: 'Second Option', value: 'SECOND_OPTION' }, | ||
{ label: 'Third Option', value: 'THIRD_OPTION' } | ||
] | ||
} | ||
} | ||
|
||
export const _FormikSelect = (props: FormikSelectProps) => { | ||
const [outputValue, setOutputValue] = useState< | ||
| { | ||
mySelect?: string | string[] | ||
} | ||
| '∅' | ||
>('∅') | ||
|
||
return ( | ||
<> | ||
<Formik initialValues={{}} onSubmit={noop}> | ||
<> | ||
<FormikEffect onChange={setOutputValue} /> | ||
|
||
<FormikSelect {...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