From 5ac6996e5654a93c2eacd4714828828677e938d2 Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 2 Jan 2021 17:02:10 +0800 Subject: [PATCH] feat(Calendar): add scrollToDate method (#7847) --- src/calendar/README.md | 7 ++++--- src/calendar/README.zh-CN.md | 7 ++++--- src/calendar/index.js | 23 +++++++++++++---------- types/calendar.d.ts | 2 ++ 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/calendar/README.md b/src/calendar/README.md index b15546804..8a8876457 100644 --- a/src/calendar/README.md +++ b/src/calendar/README.md @@ -314,9 +314,10 @@ Following props are supported when the type is multiple Use [ref](https://vuejs.org/v2/api/#ref) to get Calendar instance and call instance methods. -| Name | Description | Attribute | Return value | -| ----- | ----------------------------------- | --------- | ------------ | -| reset | Reset selected date to default date | - | - | +| Name | Description | Attribute | Return value | +| --- | --- | --- | --- | +| reset | Reset selected date to default date | - | - | +| scrollToDate `v2.12.2` | Scroll to date | _date: Date_ | - | ### Less Variables diff --git a/src/calendar/README.zh-CN.md b/src/calendar/README.zh-CN.md index 8b8150f3c..f018efd85 100644 --- a/src/calendar/README.zh-CN.md +++ b/src/calendar/README.zh-CN.md @@ -320,9 +320,10 @@ export default { 通过 ref 可以获取到 Calendar 实例并调用实例方法,详见[组件实例方法](#/zh-CN/advanced-usage#zu-jian-shi-li-fang-fa)。 -| 方法名 | 说明 | 参数 | 返回值 | -| ------ | ---------------------- | ---- | ------ | -| reset | 重置选中的日期到默认值 | - | - | +| 方法名 | 说明 | 参数 | 返回值 | +| ---------------------- | ---------------------- | ------------ | ------ | +| reset | 重置选中的日期到默认值 | - | - | +| scrollToDate `v2.12.2` | 滚动到某个日期 | _date: Date_ | - | ### 样式变量 diff --git a/src/calendar/index.js b/src/calendar/index.js index f22ca018e..96ba188cd 100644 --- a/src/calendar/index.js +++ b/src/calendar/index.js @@ -192,17 +192,9 @@ export default createComponent({ }); }, - // scroll to current month - scrollIntoView() { + // @exposed-api + scrollToDate(targetDate) { raf(() => { - const { currentDate } = this; - - if (!currentDate) { - return; - } - - const targetDate = - this.type === 'single' ? currentDate : currentDate[0]; const displayed = this.value || !this.poppable; /* istanbul ignore if */ @@ -222,6 +214,17 @@ export default createComponent({ }); }, + // scroll to current month + scrollIntoView() { + const { currentDate } = this; + + if (currentDate) { + const targetDate = + this.type === 'single' ? currentDate : currentDate[0]; + this.scrollToDate(targetDate); + } + }, + getInitialDate() { const { type, minDate, maxDate, defaultDate } = this; diff --git a/types/calendar.d.ts b/types/calendar.d.ts index 264f418f8..bc815f8d0 100644 --- a/types/calendar.d.ts +++ b/types/calendar.d.ts @@ -2,4 +2,6 @@ import { VanComponent } from './component'; export class Calendar extends VanComponent { reset(): void; + + scrollToDate(date: Date): void; }