Skip to content

Commit 35e5a81

Browse files
committed
improve calculate new selection behavior
1 parent 66b385a commit 35e5a81

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/components/DateRange/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ class DateRange extends Component {
3232
} else if (focusedRange[1] === 0) {
3333
// startDate selection
3434
const dayOffset = differenceInCalendarDays(endDate, startDate);
35+
const calculateEndDate = () => {
36+
if (moveRangeOnFirstSelection) {
37+
return addDays(value, dayOffset);
38+
}
39+
return !isBefore(value, endDate) ? value : endDate;
40+
};
3541
startDate = value;
36-
endDate = moveRangeOnFirstSelection ? addDays(value, dayOffset) : value;
42+
endDate = calculateEndDate();
3743
if (maxDate) endDate = min([endDate, maxDate]);
3844
nextFocusRange = [focusedRange[0], 1];
3945
} else {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,57 @@
1+
import React from 'react';
2+
import { subDays, addDays, isSameDay } from 'date-fns';
13
import DateRange from '../DateRange';
4+
import renderer from 'react-test-renderer';
5+
6+
let testRenderer = null;
7+
const endDate = new Date();
8+
const startDate = subDays(endDate, 7);
9+
10+
const commonProps = {
11+
ranges: [{ startDate, endDate, key: 'selection' }],
12+
onChange: () => {},
13+
moveRangeOnFirstSelection: false,
14+
};
15+
16+
const compareRanges = (newRange, assertionRange) => {
17+
expect(isSameDay(newRange.startDate, assertionRange.startDate)).toEqual(true);
18+
expect(isSameDay(newRange.endDate, assertionRange.endDate)).toEqual(true);
19+
};
20+
21+
beforeEach(() => {
22+
testRenderer = renderer.create(<DateRange {...commonProps} />);
23+
});
224

325
describe('DateRange', () => {
426
test('Should resolve', () => {
527
expect(DateRange).toEqual(expect.anything());
628
});
29+
30+
test('calculate new selection without moving end date', () => {
31+
const instance = testRenderer.getInstance();
32+
const methodResult = instance.calcNewSelection(subDays(endDate, 10), true);
33+
compareRanges(methodResult.range, {
34+
startDate: subDays(endDate, 10),
35+
endDate,
36+
});
37+
});
38+
39+
test('calculate new selection by resetting end date if start date is not before', () => {
40+
const instance = testRenderer.getInstance();
41+
const methodResult = instance.calcNewSelection(addDays(endDate, 2), true);
42+
compareRanges(methodResult.range, {
43+
startDate: addDays(endDate, 2),
44+
endDate: addDays(endDate, 2),
45+
});
46+
});
47+
48+
test('calculate new selection based on moveRangeOnFirstSelection prop', () => {
49+
testRenderer.update(<DateRange {...commonProps} moveRangeOnFirstSelection />);
50+
const instance = testRenderer.getInstance();
51+
const methodResult = instance.calcNewSelection(subDays(endDate, 10), true);
52+
compareRanges(methodResult.range, {
53+
startDate: subDays(endDate, 10),
54+
endDate: subDays(endDate, 3),
55+
});
56+
});
757
});

0 commit comments

Comments
 (0)