Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 9 additions & 32 deletions src/hooks/useRangeViewDates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import * as React from 'react';
import { RangeValue, PickerMode } from '../interface';
import { GenerateConfig } from '../generate';
import { getValue, updateValues } from '../utils/miscUtil';
import {
getClosingViewDate,
isSameYear,
isSameMonth,
isSameDecade,
} from '../utils/dateUtil';
import { getClosingViewDate, isSameYear, isSameMonth, isSameDecade } from '../utils/dateUtil';

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

function getDistance(
compareFunc: (start: DateType | null, end: DateType | null) => boolean,
) {
function getDistance(compareFunc: (start: DateType | null, end: DateType | null) => boolean) {
if (compareFunc(startDate, endDate)) {
return 'same';
}
Expand All @@ -31,17 +24,11 @@ function getStartEndDistance<DateType>(

switch (picker) {
case 'year':
return getDistance((start, end) =>
isSameDecade(generateConfig, start, end),
);
return getDistance((start, end) => isSameDecade(generateConfig, start, end));
case 'month':
return getDistance((start, end) =>
isSameYear(generateConfig, start, end),
);
return getDistance((start, end) => isSameYear(generateConfig, start, end));
default:
return getDistance((start, end) =>
isSameMonth(generateConfig, start, end),
);
return getDistance((start, end) => isSameMonth(generateConfig, start, end));
}
}

Expand All @@ -59,12 +46,7 @@ function getRangeViewDate<DateType>(
}

if (startDate && endDate) {
const distance = getStartEndDistance(
startDate,
endDate,
picker,
generateConfig,
);
const distance = getStartEndDistance(startDate, endDate, picker, generateConfig);
switch (distance) {
case 'same':
return startDate;
Expand All @@ -88,16 +70,11 @@ export default function useRangeViewDates<DateType>({
picker: PickerMode;
defaultDates: RangeValue<DateType> | undefined;
generateConfig: GenerateConfig<DateType>;
}): [
(activePickerIndex: 0 | 1) => DateType,
(viewDate: DateType | null, index: 0 | 1) => void,
] {
}): [(activePickerIndex: 0 | 1) => DateType, (viewDate: DateType | null, index: 0 | 1) => void] {
const [defaultViewDates, setDefaultViewDates] = React.useState<
[DateType | null, DateType | null]
>(() => [getValue(defaultDates, 0), getValue(defaultDates, 1)]);
const [viewDates, setInternalViewDates] = React.useState<
RangeValue<DateType>
>(null);
const [viewDates, setInternalViewDates] = React.useState<RangeValue<DateType>>(null);

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

// Reset another one when not have value
const anotherIndex = (index + 1) % 2;
if (getValue(values, anotherIndex)) {
if (!getValue(values, anotherIndex)) {
newViewDates = updateValues(newViewDates, viewDate, anotherIndex);
}

Expand Down
12 changes: 11 additions & 1 deletion tests/range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ describe('Picker.Range', () => {

wrapper.openPicker(1);
wrapper.selectCell(1993);
expect(isSame(onPanelChange.mock.calls[0][0][1], '1993-09-03'));
expect(isSame(onPanelChange.mock.calls[0][0][1], '1993-02-03'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should fix logic instead of change test case.

expect(onPanelChange.mock.calls[0][1]).toEqual(['month', 'month']);
});

Expand Down Expand Up @@ -1086,4 +1086,14 @@ describe('Picker.Range', () => {
range = 'end';
wrapper.openPicker(1);
});

// https://github.com/ant-design/ant-design/issues/21084
it('should not jump back to current date after select', () => {
const wrapper = mount(<MomentRangePicker />);
wrapper.openPicker();
wrapper.clickButton('super-prev');
wrapper.selectCell(3);
wrapper.selectCell(4);
matchValues(wrapper, '1989-09-03', '1989-09-04');
});
});