Skip to content

Commit

Permalink
fix(DatePicker): fixed oneTap to work in month view (#2342)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonguo committed Feb 10, 2022
1 parent beac483 commit d5368cd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
8 changes: 7 additions & 1 deletion docs/pages/components/date-picker/fragments/one-tap.md
@@ -1,7 +1,13 @@
<!--start-code-->

```js
const instance = <DatePicker oneTap style={{ width: 200 }} />;
const instance = (
<div>
<DatePicker oneTap style={{ width: 200 }} />
<hr />
<DatePicker oneTap format="yyyy-MM" style={{ width: 200 }} />
</div>
);
ReactDOM.render(instance);
```

Expand Down
31 changes: 19 additions & 12 deletions src/DatePicker/DatePicker.tsx
Expand Up @@ -223,18 +223,6 @@ const DatePicker: RsRefForwardingComponent<'div', DatePickerProps> = React.forwa
[onChangeCalendarDate, onSelect]
);

/**
* A callback triggered when the date on the calendar changes.
*/
const handleChangePageDate = useCallback(
(nextPageDate: Date) => {
setCalendarDate(nextPageDate);
reset();
handleDateChange(nextPageDate);
},
[handleDateChange, reset, setCalendarDate]
);

/**
* A callback triggered when the time on the calendar changes.
*/
Expand Down Expand Up @@ -374,6 +362,25 @@ const DatePicker: RsRefForwardingComponent<'div', DatePickerProps> = React.forwa
[formatStr, handleDateChange, oneTap, calendarDate, setCalendarDate, updateValue]
);

/**
* A callback triggered when the date on the calendar changes.
*/
const handleChangePageDate = useCallback(
(nextPageDate: Date, event: React.MouseEvent) => {
setCalendarDate(nextPageDate);
reset();
handleDateChange(nextPageDate);

// Show only the calendar month panel. formatStr = 'yyyy-MM'
const onlyShowMonth = DateUtils.shouldMonth(formatStr) && !DateUtils.shouldDate(formatStr);

if (oneTap && onlyShowMonth) {
updateValue(event, nextPageDate);
}
},
[formatStr, handleDateChange, oneTap, reset, setCalendarDate, updateValue]
);

const disabledDate = useCallback(
(date: Date): boolean => disabledDateProp?.(date) ?? false,
[disabledDateProp]
Expand Down
16 changes: 15 additions & 1 deletion src/DatePicker/test/DatePickerSpec.js
Expand Up @@ -449,7 +449,7 @@ describe('DatePicker', () => {
assert.ok(instance.className.match(/\bcustom-prefix\b/));
});

it('Should call onChange after setting oneTap', () => {
it('Should call onChange after setting oneTap and clicking date', () => {
const onChangeSpy = sinon.spy();

const instance = getInstance(<DatePicker onChange={onChangeSpy} oneTap defaultOpen />);
Expand All @@ -462,6 +462,20 @@ describe('DatePicker', () => {
assert.isTrue(isSameDay(onChangeSpy.firstCall.firstArg, new Date()));
});

it('Should call onChange after setting oneTap and clicking month', () => {
const onChangeSpy = sinon.spy();
const instance = getInstance(
<DatePicker onChange={onChangeSpy} format="yyyy-MM" oneTap defaultOpen />
);

const activeMonth = instance.overlay.querySelector(
'.rs-calendar-month-dropdown-cell-active .rs-calendar-month-dropdown-cell-content'
);

ReactTestUtils.Simulate.click(activeMonth);
assert.isTrue(isSameDay(onChangeSpy.firstCall.firstArg, new Date()));
});

it('Should be show meridian', () => {
const instance = getInstance(
<DatePicker
Expand Down

1 comment on commit d5368cd

@vercel
Copy link

@vercel vercel bot commented on d5368cd Feb 10, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.