Skip to content

Commit 92cfdae

Browse files
authored
Merge pull request #1 from 605data/fix/focused-range-show-date-on-calendar
Fix/focused range show date on calendar
2 parents 90c59ac + 88010dc commit 92cfdae

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/components/Calendar/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ class Calendar extends PureComponent {
122122
date: 'date',
123123
};
124124
const targetProp = propMapper[this.props.displayMode];
125-
if (this.props[targetProp] !== prevProps[targetProp]) {
125+
if (
126+
this.props[targetProp] !== prevProps[targetProp] ||
127+
this.props.focusedRange != prevProps.focusedRange
128+
) {
126129
this.updateShownDate(this.props);
127130
}
128131

src/utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ export function calcFocusDate(currentFocusedDate, props) {
2727
}
2828
targetInterval.start = startOfMonth(targetInterval.start || new Date());
2929
targetInterval.end = endOfMonth(targetInterval.end || targetInterval.start);
30-
const targetDate = targetInterval.start || targetInterval.end || shownDate || new Date();
30+
const targetDate =
31+
(focusedRange[1]
32+
? targetInterval.end || targetInterval.start
33+
: targetInterval.start || targetInterval.end) ||
34+
shownDate ||
35+
new Date();
3136

3237
// initial focus
3338
if (!currentFocusedDate) return shownDate || targetDate;

src/utils.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { endOfMonth, startOfMonth } from 'date-fns';
2+
import { calcFocusDate } from './utils';
3+
4+
describe('calcFocusDate', () => {
5+
const ranges = [
6+
{
7+
startDate: new Date(2021, 0, 10),
8+
endDate: new Date(2022, 10, 20),
9+
},
10+
];
11+
12+
describe('when focusedRange[1] equals 0', () => {
13+
test('should return startDate', () => {
14+
expect(
15+
calcFocusDate(new Date(), {
16+
ranges,
17+
focusedRange: [0, 0],
18+
displayMode: 'dateRange',
19+
})
20+
).toEqual(startOfMonth(ranges[0].startDate));
21+
});
22+
});
23+
24+
describe('when focusedRange[1] equals 1', () => {
25+
test('should return endDate', () => {
26+
expect(
27+
calcFocusDate(new Date(), {
28+
ranges,
29+
focusedRange: [0, 1],
30+
displayMode: 'dateRange',
31+
})
32+
).toEqual(endOfMonth(ranges[0].endDate));
33+
});
34+
});
35+
});

0 commit comments

Comments
 (0)