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

Commit d7e32b6

Browse files
authored
Merge pull request #428 from lexispike/feature/fixed-focus-and-backwards-focus
Add options calendarFocus and preventUnnecessaryRefocus
2 parents 90c59ac + 4705532 commit d7e32b6

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ preview(DateRange) | Object | | displays a
128128
showPreview(DateRange) | bool | true | visibility of preview
129129
editableDateInputs(Calendar) | bool | false | whether dates can be edited in the Calendar's input fields
130130
dragSelectionEnabled(Calendar) | bool | true | whether dates can be selected via drag n drop
131+
calendarFocus(Calendar) | String | 'forwards' | Whether calendar focus month should be forward-driven or backwards-driven. can be 'forwards' or 'backwards'
132+
preventSnapRefocus(Calendar) | bool | false | prevents unneceessary refocus of shown range on selection
131133
onPreviewChange(DateRange) | Object | | Callback function for preview changes
132134
dateDisplayFormat | String | `MMM d, yyyy` | selected range preview formatter. Check out [date-fns's format option](https://date-fns.org/docs/format)
133135
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: 17 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,
@@ -77,6 +78,14 @@ class Calendar extends PureComponent {
7778
}
7879
focusToDate = (date, props = this.props, preventUnnecessary = true) => {
7980
if (!props.scroll.enabled) {
81+
if (preventUnnecessary && props.preventSnapRefocus) {
82+
const focusedDateDiff = differenceInCalendarMonths(date, this.state.focusedDate);
83+
const isAllowedForward = props.calendarFocus === 'forwards' && focusedDateDiff >= 0;
84+
const isAllowedBackward = props.calendarFocus === 'backwards' && focusedDateDiff <= 0;
85+
if ((isAllowedForward || isAllowedBackward) && Math.abs(focusedDateDiff) < props.months) {
86+
return;
87+
}
88+
}
8089
this.setState({ focusedDate: date });
8190
return;
8291
}
@@ -484,7 +493,10 @@ class Calendar extends PureComponent {
484493
isVertical ? this.styles.monthsVertical : this.styles.monthsHorizontal
485494
)}>
486495
{new Array(this.props.months).fill(null).map((_, i) => {
487-
const monthStep = addMonths(this.state.focusedDate, i);
496+
let monthStep = addMonths(this.state.focusedDate, i);;
497+
if (this.props.calendarFocus === 'backwards') {
498+
monthStep = subMonths(this.state.focusedDate, this.props.months - 1 - i);
499+
}
488500
return (
489501
<Month
490502
{...this.props}
@@ -544,6 +556,8 @@ Calendar.defaultProps = {
544556
editableDateInputs: false,
545557
dragSelectionEnabled: true,
546558
fixedHeight: false,
559+
calendarFocus: 'forwards',
560+
preventSnapRefocus: false,
547561
ariaLabels: {},
548562
};
549563

@@ -598,6 +612,8 @@ Calendar.propTypes = {
598612
editableDateInputs: PropTypes.bool,
599613
dragSelectionEnabled: PropTypes.bool,
600614
fixedHeight: PropTypes.bool,
615+
calendarFocus: PropTypes.string,
616+
preventSnapRefocus: PropTypes.bool,
601617
ariaLabels: ariaLabelsShape,
602618
};
603619

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 preventSnapRefocus
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+
preventSnapRefocus={true}
49+
calendarFocus="backwards"
50+
/>;
51+
```
52+
2753
#### Example: Vertical Infinite
2854

2955
```jsx inside Markdown

0 commit comments

Comments
 (0)