Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit dee3ff2

Browse files
committed
Add options to allow specified calendar focus directions and fixed focuses
1 parent d8b46b8 commit dee3ff2

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ preview(DateRange) | Object | | displays a
126126
showPreview(DateRange) | bool | true | visibility of preview
127127
editableDateInputs(Calendar) | bool | false | whether dates can be edited in the Calendar's input fields
128128
dragSelectionEnabled(Calendar) | bool | true | whether dates can be selected via drag n drop
129+
calendarFocus(Calendar) | String | 'forwards' | Whether calendar focus month should be forward-driven or backwards-driven. can be 'forwards' or 'backwards'
130+
preventUnnecessaryRefocus(Calendar) | bool | false | prevents unneceessary refocus of shown range on selection
129131
onPreviewChange(DateRange) | Object | | Callback function for preview changes
130132
dateDisplayFormat | String | `MMM d, yyyy` | selected range preview formatter. Check out [date-fns's format option](https://date-fns.org/docs/format)
131133
dayDisplayFormat | String | `d` | selected range preview formatter. Check out [date-fns's format option](https://date-fns.org/docs/format)

src/components/Calendar/index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ReactList from 'react-list';
99
import { shallowEqualObjects } from 'shallow-equal';
1010
import {
1111
addMonths,
12+
subMonths,
1213
format,
1314
eachDayOfInterval,
1415
startOfWeek,
@@ -76,6 +77,18 @@ class Calendar extends PureComponent {
7677
}
7778
focusToDate = (date, props = this.props, preventUnnecessary = true) => {
7879
if (!props.scroll.enabled) {
80+
if (preventUnnecessary && props.preventUnnecessaryRefocus) {
81+
const focusedDateDiff = differenceInCalendarMonths(
82+
date,
83+
this.state.focusedDate,
84+
this.dateOptions
85+
);
86+
const isAllowedForward = props.calendarFocus === 'forwards' && focusedDateDiff >= 0;
87+
const isAllowedBackward = props.calendarFocus === 'backwards' && focusedDateDiff <= 0;
88+
if ((isAllowedForward || isAllowedBackward) && Math.abs(focusedDateDiff) < props.months) {
89+
return;
90+
}
91+
}
7992
this.setState({ focusedDate: date });
8093
return;
8194
}
@@ -468,7 +481,12 @@ class Calendar extends PureComponent {
468481
isVertical ? this.styles.monthsVertical : this.styles.monthsHorizontal
469482
)}>
470483
{new Array(this.props.months).fill(null).map((_, i) => {
471-
const monthStep = addMonths(this.state.focusedDate, i);
484+
let monthStep;
485+
if (this.props.calendarFocus === 'forwards') {
486+
monthStep = addMonths(this.state.focusedDate, i);
487+
} else {
488+
monthStep = subMonths(this.state.focusedDate, this.props.months - 1 - i);
489+
}
472490
return (
473491
<Month
474492
{...this.props}
@@ -528,6 +546,8 @@ Calendar.defaultProps = {
528546
editableDateInputs: false,
529547
dragSelectionEnabled: true,
530548
fixedHeight: false,
549+
calendarFocus: 'forwards',
550+
preventUnnecessaryRefocus: false,
531551
};
532552

533553
Calendar.propTypes = {
@@ -581,6 +601,8 @@ Calendar.propTypes = {
581601
editableDateInputs: PropTypes.bool,
582602
dragSelectionEnabled: PropTypes.bool,
583603
fixedHeight: PropTypes.bool,
604+
calendarFocus: PropTypes.string,
605+
preventUnnecessaryRefocus: PropTypes.bool,
584606
};
585607

586608
export default Calendar;

src/components/DateRangePicker/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ const [state, setState] = useState([
2424
/>;
2525
```
2626

27+
#### Example: Backwards 2 Month View with preventUnnecessaryRefocus
28+
29+
```jsx inside Markdown
30+
import { addDays } from 'date-fns';
31+
import { useState } from 'react';
32+
33+
const [state, setState] = useState([
34+
{
35+
startDate: new Date(),
36+
endDate: addDays(new Date(), 7),
37+
key: 'selection'
38+
}
39+
]);
40+
41+
<DateRangePicker
42+
onChange={item => setState([item.selection])}
43+
showSelectionPreview={true}
44+
moveRangeOnFirstSelection={false}
45+
months={2}
46+
ranges={state}
47+
direction="horizontal"
48+
preventUnnecessaryRefocus={true}
49+
calendarFocus="backwards"
50+
/>;
51+
```
52+
2753
#### Example: Vertical Infinite
2854

2955
```jsx inside Markdown

0 commit comments

Comments
 (0)