fix(Calendar): should reset to default date when calling reset method (#7967)

This commit is contained in:
neverland 2021-01-23 10:47:22 +08:00 committed by GitHub
parent c71b48ed9f
commit c30740ecc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 27 deletions

View File

@ -311,7 +311,7 @@ export default createComponent({
}); });
}; };
const reset = (date = getInitialDate(state.currentDate)) => { const reset = (date = getInitialDate()) => {
state.currentDate = date; state.currentDate = date;
scrollIntoView(); scrollIntoView();
}; };
@ -500,7 +500,9 @@ export default createComponent({
); );
watch(() => props.show, init); watch(() => props.show, init);
watch([() => props.type, () => props.minDate, () => props.maxDate], reset); watch([() => props.type, () => props.minDate, () => props.maxDate], () => {
reset(getInitialDate(state.currentDate));
});
watch( watch(
() => props.defaultDate, () => props.defaultDate,
(value) => { (value) => {

View File

@ -196,31 +196,6 @@ test('default range date', async () => {
); );
}); });
test('reset method', async () => {
const wrapper = mount(Calendar, {
props: {
minDate,
maxDate,
type: 'range',
poppable: false,
defaultDate: [minDate, getNextDay(minDate)],
},
});
await later();
const days = wrapper.findAll('.van-calendar__day');
days.at(15).trigger('click');
days.at(18).trigger('click');
wrapper.vm.reset();
wrapper.find('.van-calendar__confirm').trigger('click');
expect(formatRange(wrapper.emitted('confirm')[0][0])).toEqual(
'2010/1/10-2010/1/11'
);
});
test('set show-confirm to false', async () => { test('set show-confirm to false', async () => {
const wrapper = mount(Calendar, { const wrapper = mount(Calendar, {
props: { props: {

View File

@ -0,0 +1,50 @@
import Calendar from '..';
import { mount } from '../../../test';
import { getNextDay, getPrevDay } from '../utils';
import { minDate, maxDate } from './utils';
test('should reset to default date when calling reset method', async () => {
const defaultDate = [minDate, getNextDay(minDate)];
const wrapper = mount(Calendar, {
props: {
minDate,
maxDate,
type: 'range',
poppable: false,
lazyRender: false,
defaultDate,
},
});
const days = wrapper.findAll('.van-calendar__day');
await days[15].trigger('click');
await days[18].trigger('click');
wrapper.vm.reset();
wrapper.find('.van-calendar__confirm').trigger('click');
expect(wrapper.emitted('confirm')[0][0]).toEqual(defaultDate);
});
test('should reset to specific date when calling reset method with date', async () => {
const wrapper = mount(Calendar, {
props: {
minDate,
maxDate,
type: 'range',
poppable: false,
lazyRender: false,
defaultDate: [minDate, getNextDay(minDate)],
},
});
const days = wrapper.findAll('.van-calendar__day');
await days[15].trigger('click');
await days[18].trigger('click');
const newDate = [getPrevDay(maxDate), maxDate];
wrapper.vm.reset(newDate);
wrapper.find('.van-calendar__confirm').trigger('click');
expect(wrapper.emitted('confirm')[0][0]).toEqual(newDate);
});