Skip to content

Commit

Permalink
fix: RangePicker throws error when input end value first
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Jun 18, 2024
1 parent ce8620d commit eef21a3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/examples/modes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const defaultStartValue = moment('2019-09-03 05:02:03');
const defaultEndValue = moment('2019-11-28 01:02:03');

function formatDate(date: Moment | null) {
return date ? date.format('YYYY-MM-DD HH:mm:ss') : 'null';
return date ? date.format('YYYY-MM-DD HH:mm:ss') : null;
}

export default () => {
Expand Down Expand Up @@ -52,4 +52,4 @@ export default () => {
</div>
</div>
);
};
};
2 changes: 1 addition & 1 deletion src/PickerInput/hooks/useRangePickerValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export default function useRangePickerValue<DateType extends object, ValueType e
}

// Year offset
if (pickerMode === 'year') {
if (pickerMode === 'year' && startDate) {
const srcYear = Math.floor(generateConfig.getYear(startDate) / 10);
const tgtYear = Math.floor(generateConfig.getYear(endDate) / 10);
if (srcYear !== tgtYear) {
Expand Down
2 changes: 1 addition & 1 deletion tests/keyboard.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Picker.Keyboard', () => {
charCode: keyCode,
...info,
});
// document.querySelector('.rc-picker-panel').simulate('keyDown', { which: keyCode, ...info });
document.querySelector('.rc-picker-panel').simulate('keyDown', { which: keyCode, ...info });
}

beforeEach(() => {
Expand Down
17 changes: 17 additions & 0 deletions tests/picker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
confirmOK,
// MomentPicker,
DayPicker,
DayRangePicker,
findCell,
getDay,
isOpen,
Expand Down Expand Up @@ -288,6 +289,22 @@ describe('Picker.Basic', () => {
expect(document.querySelector(selected)).toBeFalsy();
});
});

// https://github.com/ant-design/ant-design/issues/49400
it('should not throw errow when input end year first', () => {
const { container } = render(
<DayRangePicker picker="year" />,
);
openPicker(container);
fireEvent.focus(container.querySelectorAll('input')[1]);
expect(() => {
fireEvent.change(container.querySelectorAll('input')[1], {
target: {
value: '2024',
},
});
}).not.toThrow();
});
});

describe('focus test', () => {
Expand Down

0 comments on commit eef21a3

Please sign in to comment.