-
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 FormikTextInput (#29)
- Loading branch information
1 parent
cdd610f
commit 8feddd3
Showing
6 changed files
with
74 additions
and
9 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
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,25 @@ | ||
import { useField } from 'formik' | ||
import { useCallback, useEffect } from 'react' | ||
|
||
import { TextInput } from '../fields/TextInput' | ||
|
||
import type { TextInputProps } from '../fields/TextInput' | ||
|
||
export type FormikTextInputProps = Omit<TextInputProps, 'defaultValue' | 'onChange'> | ||
export function FormikTextInput({ name, ...originalProps }: FormikTextInputProps) { | ||
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 <TextInput 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,44 @@ | ||
import { Formik } from 'formik' | ||
import { useState } from 'react' | ||
|
||
import { FormikEffect, FormikTextInput } from '../../src' | ||
import { Output } from '../_components/Output' | ||
import { noop } from '../_utils/noop' | ||
|
||
import type { FormikTextInputProps } from '../../src' | ||
|
||
const args: FormikTextInputProps = { | ||
name: 'myTextInput' | ||
} | ||
|
||
export default { | ||
title: 'Formiks/FormikTextInput', | ||
component: FormikTextInput, | ||
|
||
argTypes: {}, | ||
|
||
args | ||
} | ||
|
||
export const _FormikTextInput = (props: FormikTextInputProps) => { | ||
const [outputValue, setOutputValue] = useState< | ||
| { | ||
myTextInput?: string | ||
} | ||
| '∅' | ||
>('∅') | ||
|
||
return ( | ||
<> | ||
<Formik initialValues={{}} onSubmit={noop}> | ||
<> | ||
<FormikEffect onChange={setOutputValue} /> | ||
|
||
<FormikTextInput {...props} /> | ||
</> | ||
</Formik> | ||
|
||
{outputValue !== '∅' && <Output value={outputValue} />} | ||
</> | ||
) | ||
} |