Skip to content

Commit 8ea4489

Browse files
reidbarberLFDanLu
andauthored
Add pageBehavior to RAC/Spectrum Calendar and RangeCalendar (#4650)
* add pageBehavior to RAC+RSP * cleanup * fix visibleMonths edge case for RangeCalendar * add to CalendarPropsBase * update imports * add types and docs for DatePicker/DateRangePicker --------- Co-authored-by: Daniel Lu <[email protected]>
1 parent d2c09f1 commit 8ea4489

File tree

20 files changed

+82
-23
lines changed

20 files changed

+82
-23
lines changed

packages/@react-spectrum/calendar/docs/Calendar.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,13 @@ By default, the `Calendar` displays a single month. The `visibleMonths` prop all
210210
<Calendar aria-label="Event date" visibleMonths={3} />
211211
</div>
212212
```
213+
214+
### Page behavior
215+
216+
By default, when pressing the next or previous buttons, pagination will advance by the `visibleMonths` value. This behavior can be changed to page by single months instead, by setting `pageBehavior` to `single`.
217+
218+
```tsx example
219+
<div style={{maxWidth: '100%', overflow: 'auto'}}>
220+
<Calendar aria-label="Event date" visibleMonths={3} pageBehavior="single" />
221+
</div>
222+
```

packages/@react-spectrum/calendar/docs/RangeCalendar.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,13 @@ By default, a `RangeCalendar` displays a single month. The `visibleMonths` prop
242242
<RangeCalendar aria-label="Trip dates" visibleMonths={3} />
243243
</div>
244244
```
245+
246+
### Page behavior
247+
248+
By default, when pressing the next or previous buttons, pagination will advance by the `visibleMonths` value. This behavior can be changed to page by single months instead, by setting `pageBehavior` to `single`.
249+
250+
```tsx example
251+
<div style={{maxWidth: '100%', overflow: 'auto'}}>
252+
<RangeCalendar aria-label="Trip dates" visibleMonths={3} pageBehavior="single" />
253+
</div>
254+
```

packages/@react-spectrum/calendar/src/Calendar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {useProviderProps} from '@react-spectrum/provider';
2424
function Calendar<T extends DateValue>(props: SpectrumCalendarProps<T>, ref: FocusableRef<HTMLElement>) {
2525
props = useProviderProps(props);
2626
let {visibleMonths = 1} = props;
27+
visibleMonths = Math.max(visibleMonths, 1);
2728
let visibleDuration = useMemo(() => ({months: visibleMonths}), [visibleMonths]);
2829
let {locale} = useLocale();
2930
let state = useCalendarState({
@@ -46,6 +47,7 @@ function Calendar<T extends DateValue>(props: SpectrumCalendarProps<T>, ref: Foc
4647
return (
4748
<CalendarBase
4849
{...props}
50+
visibleMonths={visibleMonths}
4951
state={state}
5052
calendarRef={domRef}
5153
calendarProps={calendarProps}

packages/@react-spectrum/calendar/src/RangeCalendar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {useRangeCalendarState} from '@react-stately/calendar';
2424
function RangeCalendar<T extends DateValue>(props: SpectrumRangeCalendarProps<T>, ref: FocusableRef<HTMLElement>) {
2525
props = useProviderProps(props);
2626
let {visibleMonths = 1} = props;
27+
visibleMonths = Math.max(visibleMonths, 1);
2728
let visibleDuration = useMemo(() => ({months: visibleMonths}), [visibleMonths]);
2829
let {locale} = useLocale();
2930
let state = useRangeCalendarState({
@@ -46,6 +47,7 @@ function RangeCalendar<T extends DateValue>(props: SpectrumRangeCalendarProps<T>
4647
return (
4748
<CalendarBase
4849
{...props}
50+
visibleMonths={visibleMonths}
4951
state={state}
5052
calendarRef={domRef}
5153
calendarProps={calendarProps}

packages/@react-spectrum/calendar/stories/Calendar.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export default {
7575
visibleMonths: {
7676
control: 'number'
7777
},
78+
pageBehavior: {
79+
control: 'select',
80+
options: [null, 'single', 'visible']
81+
},
7882
validationState: {
7983
control: 'select',
8084
options: [null, 'valid', 'invalid']

packages/@react-spectrum/calendar/stories/RangeCalendar.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export default {
7070
visibleMonths: {
7171
control: 'number'
7272
},
73+
pageBehavior: {
74+
control: 'select',
75+
options: [null, 'single', 'visible']
76+
},
7377
validationState: {
7478
control: 'select',
7579
options: [null, 'valid', 'invalid']

packages/@react-spectrum/datepicker/docs/DatePicker.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ By default, the calendar popover displays a single month. The `maxVisibleMonths`
355355
<DatePicker label="Appointment date" maxVisibleMonths={3} />
356356
```
357357

358+
### Page behavior
359+
360+
By default, when pressing the next or previous buttons, pagination will advance by the `maxVisibleMonths` value. This behavior can be changed to page by single months instead, by setting `pageBehavior` to `single`.
361+
362+
```tsx example
363+
<DatePicker label="Appointment date" maxVisibleMonths={3} pageBehavior="single" />
364+
```
365+
358366
### Hide time zone
359367

360368
When a `ZonedDateTime` object is provided as the value of a `DatePicker`, the time zone abbreviation is displayed by default. However, if this is displayed elsewhere or implicit based on the usecase, it can be hidden using the `hideTimeZone` option.

packages/@react-spectrum/datepicker/docs/DateRangePicker.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@ By default, the calendar popover displays a single month. The `maxVisibleMonths`
406406
<DateRangePicker label="Date range" maxVisibleMonths={3} />
407407
```
408408

409+
### Page behavior
410+
411+
By default, when pressing the next or previous buttons, pagination will advance by the `maxVisibleMonths` value. This behavior can be changed to page by single months instead, by setting `pageBehavior` to `single`.
412+
413+
```tsx example
414+
<DateRangePicker label="Date range" maxVisibleMonths={3} pageBehavior="single" />
415+
```
416+
409417
### Hide time zone
410418

411419
When `ZonedDateTime` objects are provided as the value of a `DateRangePicker`, the time zone abbreviation is displayed by default. However, if this is displayed elsewhere or implicit based on the usecase, it can be hidden using the `hideTimeZone` option.

packages/@react-spectrum/datepicker/src/DatePicker.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ function DatePicker<T extends DateValue>(props: SpectrumDatePickerProps<T>, ref:
4545
isDisabled,
4646
isReadOnly,
4747
placeholderValue,
48-
maxVisibleMonths = 1
48+
maxVisibleMonths = 1,
49+
pageBehavior
4950
} = props;
5051
let {hoverProps, isHovered} = useHover({isDisabled});
5152
let targetRef = useRef<HTMLDivElement>();
@@ -159,6 +160,7 @@ function DatePicker<T extends DateValue>(props: SpectrumDatePickerProps<T>, ref:
159160
<Calendar
160161
{...calendarProps}
161162
visibleMonths={visibleMonths}
163+
pageBehavior={pageBehavior}
162164
UNSAFE_className={classNames(datepickerStyles, 'react-spectrum-Datepicker-calendar', {'is-invalid': state.validationState === 'invalid'})} />
163165
{showTimeField &&
164166
<div className={classNames(datepickerStyles, 'react-spectrum-Datepicker-timeFields')}>

packages/@react-spectrum/datepicker/src/DateRangePicker.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ function DateRangePicker<T extends DateValue>(props: SpectrumDateRangePickerProp
4545
isReadOnly,
4646
autoFocus,
4747
placeholderValue,
48-
maxVisibleMonths = 1
48+
maxVisibleMonths = 1,
49+
pageBehavior
4950
} = props;
5051
let {hoverProps, isHovered} = useHover({isDisabled});
5152
let targetRef = useRef<HTMLDivElement>();
@@ -173,6 +174,7 @@ function DateRangePicker<T extends DateValue>(props: SpectrumDateRangePickerProp
173174
<RangeCalendar
174175
{...calendarProps}
175176
visibleMonths={visibleMonths}
177+
pageBehavior={pageBehavior}
176178
UNSAFE_className={classNames(datepickerStyles, 'react-spectrum-Datepicker-calendar', {'is-invalid': state.validationState === 'invalid'})} />
177179
{showTimeField &&
178180
<Flex gap="size-100" marginTop="size-100" UNSAFE_className={classNames(datepickerStyles, 'react-spectrum-Datepicker-timeFields')}>

0 commit comments

Comments
 (0)