feat(Calendar): add scrollToDate method (#7847)

This commit is contained in:
neverland 2021-01-02 17:02:10 +08:00 committed by GitHub
parent 9582cd2fae
commit 5ac6996e56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 16 deletions

View File

@ -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. Use [ref](https://vuejs.org/v2/api/#ref) to get Calendar instance and call instance methods.
| Name | Description | Attribute | Return value | | Name | Description | Attribute | Return value |
| ----- | ----------------------------------- | --------- | ------------ | | --- | --- | --- | --- |
| reset | Reset selected date to default date | - | - | | reset | Reset selected date to default date | - | - |
| scrollToDate `v2.12.2` | Scroll to date | _date: Date_ | - |
### Less Variables ### Less Variables

View File

@ -320,9 +320,10 @@ export default {
通过 ref 可以获取到 Calendar 实例并调用实例方法,详见[组件实例方法](#/zh-CN/advanced-usage#zu-jian-shi-li-fang-fa)。 通过 ref 可以获取到 Calendar 实例并调用实例方法,详见[组件实例方法](#/zh-CN/advanced-usage#zu-jian-shi-li-fang-fa)。
| 方法名 | 说明 | 参数 | 返回值 | | 方法名 | 说明 | 参数 | 返回值 |
| ------ | ---------------------- | ---- | ------ | | ---------------------- | ---------------------- | ------------ | ------ |
| reset | 重置选中的日期到默认值 | - | - | | reset | 重置选中的日期到默认值 | - | - |
| scrollToDate `v2.12.2` | 滚动到某个日期 | _date: Date_ | - |
### 样式变量 ### 样式变量

View File

@ -192,17 +192,9 @@ export default createComponent({
}); });
}, },
// scroll to current month // @exposed-api
scrollIntoView() { scrollToDate(targetDate) {
raf(() => { raf(() => {
const { currentDate } = this;
if (!currentDate) {
return;
}
const targetDate =
this.type === 'single' ? currentDate : currentDate[0];
const displayed = this.value || !this.poppable; const displayed = this.value || !this.poppable;
/* istanbul ignore if */ /* 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() { getInitialDate() {
const { type, minDate, maxDate, defaultDate } = this; const { type, minDate, maxDate, defaultDate } = this;

2
types/calendar.d.ts vendored
View File

@ -2,4 +2,6 @@ import { VanComponent } from './component';
export class Calendar extends VanComponent { export class Calendar extends VanComponent {
reset(): void; reset(): void;
scrollToDate(date: Date): void;
} }