Skip to content

Commit

Permalink
feat(formiks): add FormikTextarea (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Nov 29, 2022
1 parent 0cd9b13 commit 59aa260
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 12 deletions.
25 changes: 25 additions & 0 deletions src/formiks/FormikTextarea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useField } from 'formik'
import { useCallback, useEffect } from 'react'

import { Textarea } from '../fields/Textarea'

import type { TextareaProps } from '../fields/Textarea'

export type FormikTextareaProps = Omit<TextareaProps, 'defaultValue' | 'onChange'>
export function FormikTextarea({ name, ...originalProps }: FormikTextareaProps) {
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 <Textarea name={name} onChange={handleChange} {...originalProps} />
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { FormikDateRangePicker } from './formiks/FormikDateRangePicker'
export { FormikEffect } from './formiks/FormikEffect'
export { FormikMultiSelect } from './formiks/FormikMultiSelect'
export { FormikSelect } from './formiks/FormikSelect'
export { FormikTextarea } from './formiks/FormikTextarea'
export { FormikTextInput } from './formiks/FormikTextInput'

export { ThemeProvider } from './ThemeProvider'
Expand All @@ -35,4 +36,5 @@ export type { FormikDateRangePickerProps } from './formiks/FormikDateRangePicker
export type { FormikEffectProps } from './formiks/FormikEffect'
export type { FormikMultiSelectProps } from './formiks/FormikMultiSelect'
export type { FormikSelectProps } from './formiks/FormikSelect'
export type { FormikTextareaProps } from './formiks/FormikTextarea'
export type { FormikTextInputProps } from './formiks/FormikTextInput'
6 changes: 4 additions & 2 deletions stories/formiks/FormikCheckbox.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Formik } from 'formik'
import { useState } from 'react'
import { useMemo, useState } from 'react'

import { FormikEffect, FormikCheckbox } from '../../src'
import { Output } from '../_components/Output'
Expand Down Expand Up @@ -29,9 +29,11 @@ export const _FormikCheckbox = (props: FormikCheckboxProps) => {
| '∅'
>('∅')

const key = useMemo(() => props.name, [props.name])

return (
<>
<Formik initialValues={{}} onSubmit={noop}>
<Formik key={key} initialValues={{}} onSubmit={noop}>
<>
<FormikEffect onChange={setOutputValue} />

Expand Down
6 changes: 4 additions & 2 deletions stories/formiks/FormikDatePicker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Formik } from 'formik'
import { useState } from 'react'
import { useMemo, useState } from 'react'

import { FormikEffect, FormikDatePicker } from '../../src'
import { Output } from '../_components/Output'
Expand Down Expand Up @@ -32,9 +32,11 @@ export const _FormikDatePicker = (props: FormikDatePickerProps) => {
| '∅'
>('∅')

const key = useMemo(() => props.name, [props.name])

return (
<>
<Formik initialValues={{}} onSubmit={noop}>
<Formik key={key} initialValues={{}} onSubmit={noop}>
<>
<FormikEffect onChange={setOutputValue} />

Expand Down
6 changes: 4 additions & 2 deletions stories/formiks/FormikDateRangePicker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Formik } from 'formik'
import { useState } from 'react'
import { useMemo, useState } from 'react'

import { FormikEffect, FormikDateRangePicker } from '../../src'
import { Output } from '../_components/Output'
Expand Down Expand Up @@ -33,9 +33,11 @@ export const _FormikDateRangePicker = (props: FormikDateRangePickerProps) => {
| '∅'
>('∅')

const key = useMemo(() => props.name, [props.name])

return (
<>
<Formik initialValues={{}} onSubmit={noop}>
<Formik key={key} initialValues={{}} onSubmit={noop}>
<>
<FormikEffect onChange={setOutputValue} />

Expand Down
6 changes: 4 additions & 2 deletions stories/formiks/FormikMultiSelect.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Formik } from 'formik'
import { useState } from 'react'
import { useMemo, useState } from 'react'

import { FormikEffect, FormikMultiSelect } from '../../src'
import { Output } from '../_components/Output'
Expand Down Expand Up @@ -35,9 +35,11 @@ export const _FormikMultiSelect = (props: FormikMultiSelectProps) => {
| '∅'
>('∅')

const key = useMemo(() => props.name, [props.name])

return (
<>
<Formik initialValues={{}} onSubmit={noop}>
<Formik key={key} initialValues={{}} onSubmit={noop}>
<>
<FormikEffect onChange={setOutputValue} />

Expand Down
6 changes: 4 additions & 2 deletions stories/formiks/FormikSelect.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Formik } from 'formik'
import { useState } from 'react'
import { useMemo, useState } from 'react'

import { FormikEffect, FormikSelect } from '../../src'
import { Output } from '../_components/Output'
Expand Down Expand Up @@ -35,9 +35,11 @@ export const _FormikSelect = (props: FormikSelectProps) => {
| '∅'
>('∅')

const key = useMemo(() => props.name, [props.name])

return (
<>
<Formik initialValues={{}} onSubmit={noop}>
<Formik key={key} initialValues={{}} onSubmit={noop}>
<>
<FormikEffect onChange={setOutputValue} />

Expand Down
6 changes: 4 additions & 2 deletions stories/formiks/FormikTextInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Formik } from 'formik'
import { useState } from 'react'
import { useMemo, useState } from 'react'

import { FormikEffect, FormikTextInput } from '../../src'
import { Output } from '../_components/Output'
Expand Down Expand Up @@ -28,9 +28,11 @@ export const _FormikTextInput = (props: FormikTextInputProps) => {
| '∅'
>('∅')

const key = useMemo(() => props.name, [props.name])

return (
<>
<Formik initialValues={{}} onSubmit={noop}>
<Formik key={key} initialValues={{}} onSubmit={noop}>
<>
<FormikEffect onChange={setOutputValue} />

Expand Down
46 changes: 46 additions & 0 deletions stories/formiks/FormikTextarea.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Formik } from 'formik'
import { useMemo, useState } from 'react'

import { FormikEffect, FormikTextarea } from '../../src'
import { Output } from '../_components/Output'
import { noop } from '../_utils/noop'

import type { FormikTextareaProps } from '../../src'

const args: FormikTextareaProps = {
name: 'myTextarea'
}

export default {
title: 'Formiks/FormikTextarea',
component: FormikTextarea,

argTypes: {},

args
}

export const _FormikTextarea = (props: FormikTextareaProps) => {
const [outputValue, setOutputValue] = useState<
| {
myTextarea?: string
}
| '∅'
>('∅')

const key = useMemo(() => props.name, [props.name])

return (
<>
<Formik key={key} initialValues={{}} onSubmit={noop}>
<>
<FormikEffect onChange={setOutputValue} />

<FormikTextarea {...props} />
</>
</Formik>

{outputValue !== '∅' && <Output value={outputValue} />}
</>
)
}

0 comments on commit 59aa260

Please sign in to comment.