fix calendar-onPanelChange some time not trigger (#15063)

This commit is contained in:
zombieJ 2019-02-26 19:56:55 +08:00 committed by GitHub
parent 9e8bddd864
commit ed1872fe08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -147,4 +147,18 @@ describe('Calendar', () => {
expect(wrapper.render()).toMatchSnapshot();
MockDate.reset();
});
it('should trigger onPanelChange when click last month of date', () => {
const onPanelChange = jest.fn();
const date = new Moment('1990-09-03');
const wrapper = mount(<Calendar onPanelChange={onPanelChange} value={date} />);
wrapper
.find('.ant-fullcalendar-cell')
.at(0)
.simulate('click');
expect(onPanelChange).toBeCalled();
expect(onPanelChange.mock.calls[0][0].month()).toEqual(date.month() - 1);
});
});

View File

@ -128,15 +128,21 @@ export default class Calendar extends React.Component<CalendarProps, CalendarSta
};
setValue = (value: moment.Moment, way: 'select' | 'changePanel') => {
const prevValue = this.props.value || this.state.value;
const { mode } = this.state;
if (!('value' in this.props)) {
this.setState({ value });
}
if (way === 'select') {
if (prevValue && prevValue.month() !== value.month()) {
this.onPanelChange(value, mode);
}
if (this.props.onSelect) {
this.props.onSelect(value);
}
} else if (way === 'changePanel') {
this.onPanelChange(value, this.state.mode);
this.onPanelChange(value, mode);
}
};