Skip to content

Commit 5a54895

Browse files
morenyangzombieJ
andauthored
fix: ant-design/ant-design#21084 improve range picker calendar display on select (#21)
* improve range picker calendar display on select * add test case on range picker affects * Update range.spec.tsx Co-authored-by: 二货机器人 <[email protected]>
1 parent 8c13ab7 commit 5a54895

File tree

2 files changed

+20
-33
lines changed

2 files changed

+20
-33
lines changed

src/hooks/useRangeViewDates.ts

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import * as React from 'react';
22
import { RangeValue, PickerMode } from '../interface';
33
import { GenerateConfig } from '../generate';
44
import { getValue, updateValues } from '../utils/miscUtil';
5-
import {
6-
getClosingViewDate,
7-
isSameYear,
8-
isSameMonth,
9-
isSameDecade,
10-
} from '../utils/dateUtil';
5+
import { getClosingViewDate, isSameYear, isSameMonth, isSameDecade } from '../utils/dateUtil';
116

127
function getStartEndDistance<DateType>(
138
startDate: DateType,
@@ -17,9 +12,7 @@ function getStartEndDistance<DateType>(
1712
): 'same' | 'closing' | 'far' {
1813
const startNext = getClosingViewDate(startDate, picker, generateConfig, 1);
1914

20-
function getDistance(
21-
compareFunc: (start: DateType | null, end: DateType | null) => boolean,
22-
) {
15+
function getDistance(compareFunc: (start: DateType | null, end: DateType | null) => boolean) {
2316
if (compareFunc(startDate, endDate)) {
2417
return 'same';
2518
}
@@ -31,17 +24,11 @@ function getStartEndDistance<DateType>(
3124

3225
switch (picker) {
3326
case 'year':
34-
return getDistance((start, end) =>
35-
isSameDecade(generateConfig, start, end),
36-
);
27+
return getDistance((start, end) => isSameDecade(generateConfig, start, end));
3728
case 'month':
38-
return getDistance((start, end) =>
39-
isSameYear(generateConfig, start, end),
40-
);
29+
return getDistance((start, end) => isSameYear(generateConfig, start, end));
4130
default:
42-
return getDistance((start, end) =>
43-
isSameMonth(generateConfig, start, end),
44-
);
31+
return getDistance((start, end) => isSameMonth(generateConfig, start, end));
4532
}
4633
}
4734

@@ -59,12 +46,7 @@ function getRangeViewDate<DateType>(
5946
}
6047

6148
if (startDate && endDate) {
62-
const distance = getStartEndDistance(
63-
startDate,
64-
endDate,
65-
picker,
66-
generateConfig,
67-
);
49+
const distance = getStartEndDistance(startDate, endDate, picker, generateConfig);
6850
switch (distance) {
6951
case 'same':
7052
return startDate;
@@ -88,16 +70,11 @@ export default function useRangeViewDates<DateType>({
8870
picker: PickerMode;
8971
defaultDates: RangeValue<DateType> | undefined;
9072
generateConfig: GenerateConfig<DateType>;
91-
}): [
92-
(activePickerIndex: 0 | 1) => DateType,
93-
(viewDate: DateType | null, index: 0 | 1) => void,
94-
] {
73+
}): [(activePickerIndex: 0 | 1) => DateType, (viewDate: DateType | null, index: 0 | 1) => void] {
9574
const [defaultViewDates, setDefaultViewDates] = React.useState<
9675
[DateType | null, DateType | null]
9776
>(() => [getValue(defaultDates, 0), getValue(defaultDates, 1)]);
98-
const [viewDates, setInternalViewDates] = React.useState<
99-
RangeValue<DateType>
100-
>(null);
77+
const [viewDates, setInternalViewDates] = React.useState<RangeValue<DateType>>(null);
10178

10279
const startDate = getValue(values, 0);
10380
const endDate = getValue(values, 1);
@@ -128,7 +105,7 @@ export default function useRangeViewDates<DateType>({
128105

129106
// Reset another one when not have value
130107
const anotherIndex = (index + 1) % 2;
131-
if (getValue(values, anotherIndex)) {
108+
if (!getValue(values, anotherIndex)) {
132109
newViewDates = updateValues(newViewDates, viewDate, anotherIndex);
133110
}
134111

tests/range.spec.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ describe('Picker.Range', () => {
525525

526526
wrapper.openPicker(1);
527527
wrapper.selectCell(1993);
528-
expect(isSame(onPanelChange.mock.calls[0][0][1], '1993-09-03'));
528+
expect(isSame(onPanelChange.mock.calls[0][0][1], '1993-02-03'));
529529
expect(onPanelChange.mock.calls[0][1]).toEqual(['month', 'month']);
530530
});
531531

@@ -1086,4 +1086,14 @@ describe('Picker.Range', () => {
10861086
range = 'end';
10871087
wrapper.openPicker(1);
10881088
});
1089+
1090+
// https://github.com/ant-design/ant-design/issues/21084
1091+
it('should not jump back to current date after select', () => {
1092+
const wrapper = mount(<MomentRangePicker />);
1093+
wrapper.openPicker();
1094+
wrapper.clickButton('super-prev');
1095+
wrapper.selectCell(3);
1096+
wrapper.selectCell(4);
1097+
matchValues(wrapper, '1989-09-03', '1989-09-04');
1098+
});
10891099
});

0 commit comments

Comments
 (0)