Skip to content

Commit 53bc14d

Browse files
committed
improve range picker calendar display on select
1 parent 8c13ab7 commit 53bc14d

File tree

2 files changed

+10
-33
lines changed

2 files changed

+10
-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: 1 addition & 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

0 commit comments

Comments
 (0)