From b2d25ef23b827ec2427bf47b343e6dbd66326ed3 Mon Sep 17 00:00:00 2001 From: Maxim Usachev Date: Mon, 8 Apr 2024 20:11:30 +0300 Subject: [PATCH] Calendar calendar state context set value can not set null value (#6030) * Fixed the inability to set the null for setValue from CalendarStateContext * added example in docs of calendar * added example in storybook * added test for setting "null" for method setValue * fixed eslint bug * fixed eslint bug * fixed ts error * review comments --------- Co-authored-by: Daniel Lu Co-authored-by: Robert Snow --- packages/@react-stately/calendar/src/types.ts | 4 +- .../calendar/src/useCalendarState.ts | 6 +- .../react-aria-components/docs/Calendar.mdx | 49 +++++++++++++++ .../stories/Calendar.stories.tsx | 37 ++++++++++- .../test/Calendar.test.js | 62 ++++++++++++++++++- 5 files changed, 151 insertions(+), 7 deletions(-) diff --git a/packages/@react-stately/calendar/src/types.ts b/packages/@react-stately/calendar/src/types.ts index d115bbfde34..1202ff47b71 100644 --- a/packages/@react-stately/calendar/src/types.ts +++ b/packages/@react-stately/calendar/src/types.ts @@ -101,9 +101,9 @@ interface CalendarStateBase { export interface CalendarState extends CalendarStateBase { /** The currently selected date. */ - readonly value: CalendarDate, + readonly value: CalendarDate | null, /** Sets the currently selected date. */ - setValue(value: CalendarDate): void + setValue(value: CalendarDate | null): void } export interface RangeCalendarState extends CalendarStateBase { diff --git a/packages/@react-stately/calendar/src/useCalendarState.ts b/packages/@react-stately/calendar/src/useCalendarState.ts index f8be57d53e1..a489d99aa76 100644 --- a/packages/@react-stately/calendar/src/useCalendarState.ts +++ b/packages/@react-stately/calendar/src/useCalendarState.ts @@ -136,8 +136,12 @@ export function useCalendarState(props: Calenda setFocusedDate(date); } - function setValue(newValue: CalendarDate) { + function setValue(newValue: CalendarDate | null) { if (!props.isDisabled && !props.isReadOnly) { + if (newValue === null) { + setControlledValue(null); + return; + } newValue = constrainValue(newValue, minValue, maxValue); newValue = previousAvailableDate(newValue, startDate, isDateUnavailable); if (!newValue) { diff --git a/packages/react-aria-components/docs/Calendar.mdx b/packages/react-aria-components/docs/Calendar.mdx index 35e4ea59e94..6c507fce615 100644 --- a/packages/react-aria-components/docs/Calendar.mdx +++ b/packages/react-aria-components/docs/Calendar.mdx @@ -859,6 +859,55 @@ function CalendarValue() { ``` +#### Reset value + +This example shows a custom `Footer` component that can be placed inside the `Calendar.` In this component, we use `CalendarStateContext` to get the `setValue` method from the `CalendarState` object. Next, by clicking on the button, we set the `setValue` to `null` to reset the `Calendar` `value`. + +```tsx example +import {useContext} from 'react'; +import {CalendarStateContext, Button} from 'react-aria-components'; + +function Footer() { + const state = useContext(CalendarStateContext); + {/*- begin highlight -*/} + const { setValue } = state; + {/*- end highlight -*/} + + return ( +
+ +
+ ); +} + + +
+ + + +
+ + {date => } + + {/*- begin highlight -*/} +