Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the month and year cannot be switched after the datepicker is rendered in Calendar #535

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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) {
Copy link
Member

Choose a reason for hiding this comment

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

这个 value object 变化重置是 by design,做 date 判断反而是非预期的。我去那边评论一下~

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