Skip to content

Commit

Permalink
fix: the month and year cannot be switched after the datepicker is re…
Browse files Browse the repository at this point in the history
…ndered in Calendar
  • Loading branch information
edc-hui committed Nov 30, 2022
1 parent 66b486d commit dfbe7bd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/PickerPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ function PickerPanel<DateType>(props: PickerPanelProps<DateType>) {
},
});

const originalValue = React.useRef(value);

const setViewDate = (date: DateType) => {
setInnerViewDate(date);
if (onPickerValueChange) {
Expand Down Expand Up @@ -334,8 +336,9 @@ function PickerPanel<DateType>(props: PickerPanelProps<DateType>) {

// ============================ Effect ============================
React.useEffect(() => {
if (value && !initRef.current) {
if (value && !isEqual(generateConfig, value, originalValue.current) && !initRef.current) {
setInnerViewDate(value);
originalValue.current = value;
}
}, [value]);

Expand Down
36 changes: 36 additions & 0 deletions tests/panel.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import zhCN from '../src/locale/zh_CN';
import {
clickButton,
confirmOK,
findCell,
getMoment,
isOpen,
isSame,
MomentPickerPanel,
selectCell,
Calendar
} from './util/commonUtil';

jest.mock('../src/utils/uiUtil', () => {
Expand Down Expand Up @@ -557,4 +560,37 @@ describe('Picker.Panel', () => {
});
});
});

it('calendar', () => {
const { container } = render(<Calendar />);
expect(container.querySelector('.rc-picker-month-panel')).toBeTruthy();
const inputElements: any = container.querySelectorAll('.rc-picker-month-panel input');
expect(inputElements.length).toBe(12);
const inputElement = inputElements[0];
expect(inputElement.value).toBe("");

fireEvent.mouseDown(inputElement);
fireEvent.focus(inputElement);
expect(isOpen()).toBeTruthy();
const monthDOM = document.querySelector('.rc-picker-header-view > .rc-picker-month-btn') as HTMLElement;
const lastMonthBtnValue = monthDOM.innerHTML;
fireEvent.click(document.querySelector('.rc-picker-dropdown .rc-picker-header-prev-btn'));
const currentMonthBtnValue = monthDOM.innerHTML;
expect(lastMonthBtnValue !== currentMonthBtnValue).toBeTruthy();

const cell = findCell(2, 1);
fireEvent.click(cell);
jest.runAllTimers();
expect(inputElement.value).not.toBeNull();
expect(isOpen()).toBeFalsy();

fireEvent.mouseDown(inputElement);
fireEvent.focus(inputElement);
expect(isOpen()).toBeTruthy();
const monthDOMNew = document.querySelector('.rc-picker-header-view > .rc-picker-month-btn') as HTMLElement;
const lastMonthBtnValueNew = monthDOMNew.innerHTML;
fireEvent.click(document.querySelector('.rc-picker-dropdown .rc-picker-header-prev-btn'));
const currentMonthBtnValueNew = monthDOMNew.innerHTML;
expect(lastMonthBtnValueNew !== currentMonthBtnValueNew).toBeTruthy();
});
});
27 changes: 26 additions & 1 deletion tests/util/commonUtil.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import React, { useState } from 'react';
// import { mount as originMount, ReactWrapper } from 'enzyme';
import { fireEvent } from '@testing-library/react';
import moment, { Moment, unitOfTime } from 'moment';
import Picker, { PickerPanel, PickerProps } from '../../src';
import momentGenerateConfig from '../../src/generate/moment';
import enUS from '../../src/locale/en_US';
import zhCN from '../../src/locale/zh_CN';
import { PickerBaseProps, PickerDateProps, PickerTimeProps } from '../../src/Picker';
import {
PickerPanelBaseProps,
Expand Down Expand Up @@ -95,6 +96,30 @@ export const MomentPickerPanel = (props: MomentPickerPanelProps) => (
<PickerPanel<Moment> generateConfig={momentGenerateConfig} locale={enUS} {...props} />
);

export const Calendar = () => {
const [date, setDate] = useState<Moment | null>(null);
return (
<PickerPanel<Moment>
locale={zhCN}
mode="month"
generateConfig={momentGenerateConfig}
monthCellRender={(value) => {
return (
<Picker<Moment>
locale={zhCN}
generateConfig={momentGenerateConfig}
value={date ? moment(date) : null}
onChange={(dateValue) => {
setDate(dateValue);
}}
/>
)
}}
hideHeader
/>
)
}

// Moment Range Picker
export type MomentRangePickerProps =
| InjectDefaultProps<RangePickerBaseProps<Moment>>
Expand Down

0 comments on commit dfbe7bd

Please sign in to comment.