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

Add options calendarFocus and preventUnnecessaryRefocus #428

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ preview(DateRange) | Object | | displays a
showPreview(DateRange) | bool | true | visibility of preview
editableDateInputs(Calendar) | bool | false | whether dates can be edited in the Calendar's input fields
dragSelectionEnabled(Calendar) | bool | true | whether dates can be selected via drag n drop
calendarFocus(Calendar) | String | 'forwards' | Whether calendar focus month should be forward-driven or backwards-driven. can be 'forwards' or 'backwards'
preventSnapRefocus(Calendar) | bool | false | prevents unneceessary refocus of shown range on selection
onPreviewChange(DateRange) | Object | | Callback function for preview changes
dateDisplayFormat | String | `MMM d, yyyy` | selected range preview formatter. Check out [date-fns's format option](https://date-fns.org/docs/format)
dayDisplayFormat | String | `d` | selected range preview formatter. Check out [date-fns's format option](https://date-fns.org/docs/format)
Expand Down
18 changes: 17 additions & 1 deletion src/components/Calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ReactList from 'react-list';
import { shallowEqualObjects } from 'shallow-equal';
import {
addMonths,
subMonths,
format,
eachDayOfInterval,
startOfWeek,
Expand Down Expand Up @@ -77,6 +78,14 @@ class Calendar extends PureComponent {
}
focusToDate = (date, props = this.props, preventUnnecessary = true) => {
if (!props.scroll.enabled) {
if (preventUnnecessary && props.preventSnapRefocus) {
const focusedDateDiff = differenceInCalendarMonths(date, this.state.focusedDate);
const isAllowedForward = props.calendarFocus === 'forwards' && focusedDateDiff >= 0;
const isAllowedBackward = props.calendarFocus === 'backwards' && focusedDateDiff <= 0;
if ((isAllowedForward || isAllowedBackward) && Math.abs(focusedDateDiff) < props.months) {
return;
}
}
this.setState({ focusedDate: date });
return;
}
Expand Down Expand Up @@ -484,7 +493,10 @@ class Calendar extends PureComponent {
isVertical ? this.styles.monthsVertical : this.styles.monthsHorizontal
)}>
{new Array(this.props.months).fill(null).map((_, i) => {
const monthStep = addMonths(this.state.focusedDate, i);
let monthStep = addMonths(this.state.focusedDate, i);;
if (this.props.calendarFocus === 'backwards') {
monthStep = subMonths(this.state.focusedDate, this.props.months - 1 - i);
}
return (
<Month
{...this.props}
Expand Down Expand Up @@ -544,6 +556,8 @@ Calendar.defaultProps = {
editableDateInputs: false,
dragSelectionEnabled: true,
fixedHeight: false,
calendarFocus: 'forwards',
preventSnapRefocus: false,
ariaLabels: {},
};

Expand Down Expand Up @@ -598,6 +612,8 @@ Calendar.propTypes = {
editableDateInputs: PropTypes.bool,
dragSelectionEnabled: PropTypes.bool,
fixedHeight: PropTypes.bool,
calendarFocus: PropTypes.string,
preventSnapRefocus: PropTypes.bool,
ariaLabels: ariaLabelsShape,
};

Expand Down
26 changes: 26 additions & 0 deletions src/components/DateRangePicker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ const [state, setState] = useState([
/>;
```

#### Example: Backwards 2 Month View with preventSnapRefocus

```jsx inside Markdown
import { addDays } from 'date-fns';
import { useState } from 'react';

const [state, setState] = useState([
{
startDate: new Date(),
endDate: addDays(new Date(), 7),
key: 'selection'
}
]);

<DateRangePicker
onChange={item => setState([item.selection])}
showSelectionPreview={true}
moveRangeOnFirstSelection={false}
months={2}
ranges={state}
direction="horizontal"
preventSnapRefocus={true}
calendarFocus="backwards"
/>;
```

#### Example: Vertical Infinite

```jsx inside Markdown
Expand Down