-
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 FormikDateRangePicker
- Loading branch information
1 parent
3df48fc
commit dd37249
Showing
3 changed files
with
69 additions
and
0 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,21 @@ | ||
import { useField } from 'formik' | ||
import { useEffect } from 'react' | ||
|
||
import { DateRangePicker } from '../fields/DateRangePicker' | ||
|
||
import type { DateRangePickerProps } from '../fields/DateRangePicker' | ||
|
||
export type FormikDateRangePickerProps = Omit<DateRangePickerProps, 'onChange'> & { | ||
name: string | ||
} | ||
export function FormikDateRangePicker({ name, ...originalProps }: FormikDateRangePickerProps) { | ||
const [, , helpers] = useField(name) | ||
const { setValue } = helpers | ||
|
||
// We don't include `setValues` in `useEffect()` dependencies | ||
// both because it is useless and it will trigger infinite hook calls | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEffect(() => () => setValue(undefined), []) | ||
|
||
return <DateRangePicker onChange={setValue} {...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,46 @@ | ||
import { Formik } from 'formik' | ||
import { useState } from 'react' | ||
|
||
import { FormikEffect, FormikDateRangePicker } from '../../src' | ||
import { Output } from '../_components/Output' | ||
import { noop } from '../_utils/noop' | ||
|
||
import type { FormikDateRangePickerProps } from '../../src' | ||
import type { DateRange } from '../../src/types' | ||
|
||
export default { | ||
title: 'Formiks/FormikDateRangePicker', | ||
component: FormikDateRangePicker, | ||
|
||
argTypes: {}, | ||
|
||
args: { | ||
isHistorical: false, | ||
isLabelHidden: false, | ||
label: 'FormikDateRangePicker Label', | ||
name: 'myDateRange' | ||
} | ||
} | ||
|
||
export const _FormikDateRangePicker = (props: FormikDateRangePickerProps) => { | ||
const [outputValue, setOutputValue] = useState< | ||
| { | ||
myDateRange?: DateRange | ||
} | ||
| '∅' | ||
>('∅') | ||
|
||
return ( | ||
<> | ||
<Formik initialValues={{}} onSubmit={noop}> | ||
<> | ||
<FormikEffect onChange={setOutputValue} /> | ||
|
||
<FormikDateRangePicker {...props} /> | ||
</> | ||
</Formik> | ||
|
||
{outputValue !== '∅' && <Output value={outputValue} />} | ||
</> | ||
) | ||
} |