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 -*/} +