Skip to content

Commit

Permalink
Merge pull request #428 from lexispike/feature/fixed-focus-and-backwa…
Browse files Browse the repository at this point in the history
…rds-focus

Add options calendarFocus and preventUnnecessaryRefocus
  • Loading branch information
kamyar committed Aug 29, 2021
2 parents 90c59ac + 4705532 commit d7e32b6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -128,6 +128,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
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
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

0 comments on commit d7e32b6

Please sign in to comment.