mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(Calendar): scroll to current month when show (#5526)
This commit is contained in:
parent
ac3808cd47
commit
c6489f4006
@ -117,6 +117,7 @@ export default createComponent({
|
|||||||
value(val) {
|
value(val) {
|
||||||
if (val) {
|
if (val) {
|
||||||
this.initRect();
|
this.initRect();
|
||||||
|
this.scrollIntoView();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -126,7 +127,7 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
if (!this.poppable) {
|
if (this.value || !this.poppable) {
|
||||||
this.initRect();
|
this.initRect();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -144,6 +145,28 @@ export default createComponent({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// scroll to current month
|
||||||
|
scrollIntoView() {
|
||||||
|
this.$nextTick(() => {
|
||||||
|
const { type, currentDate } = this;
|
||||||
|
const targetDate = type === 'range' ? currentDate[0] : currentDate;
|
||||||
|
|
||||||
|
/* istanbul ignore if */
|
||||||
|
if (!targetDate) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.months.some((month, index) => {
|
||||||
|
if (compareMonth(month, targetDate) === 0) {
|
||||||
|
this.$refs.months[index].$el.scrollIntoView();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
getInitialDate() {
|
getInitialDate() {
|
||||||
const { type, defaultDate, minDate } = this;
|
const { type, defaultDate, minDate } = this;
|
||||||
|
|
||||||
|
@ -407,3 +407,23 @@ test('color prop when type is range', async () => {
|
|||||||
|
|
||||||
expect(wrapper).toMatchSnapshot();
|
expect(wrapper).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should scroll to current month when show', async done => {
|
||||||
|
const wrapper = mount(Calendar, {
|
||||||
|
propsData: {
|
||||||
|
type: 'range',
|
||||||
|
minDate: new Date(2010, 0, 10),
|
||||||
|
maxDate: new Date(2010, 11, 10),
|
||||||
|
defaultDate: [new Date(2010, 3, 1), new Date(2010, 5, 1)]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Element.prototype.scrollIntoView = function() {
|
||||||
|
expect(this).toEqual(wrapper.findAll('.van-calendar__month').at(3).element);
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
|
||||||
|
wrapper.setProps({ value: true });
|
||||||
|
|
||||||
|
await later();
|
||||||
|
});
|
||||||
|
@ -30,6 +30,11 @@ function mockHTMLElementOffset() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mockScrollIntoView() {
|
||||||
|
Element.prototype.scrollIntoView = function() {};
|
||||||
|
}
|
||||||
|
|
||||||
|
mockScrollIntoView();
|
||||||
mockHTMLElementOffset();
|
mockHTMLElementOffset();
|
||||||
|
|
||||||
export function mockGetBoundingClientRect(
|
export function mockGetBoundingClientRect(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user