From 3344e31a92a44fc81ad76807db35aa28b898dd0a Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 2 Jan 2021 16:31:24 +0800 Subject: [PATCH] types(IndexBar): add scrollTo method (#7845) --- src/index-bar/README.md | 8 ++++++++ src/index-bar/README.zh-CN.md | 8 ++++++++ src/index-bar/index.js | 12 ++++++------ types/index-bar.d.ts | 5 +++++ types/index.d.ts | 3 ++- 5 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 types/index-bar.d.ts diff --git a/src/index-bar/README.md b/src/index-bar/README.md index 2f61f4114..b222a7988 100644 --- a/src/index-bar/README.md +++ b/src/index-bar/README.md @@ -89,6 +89,14 @@ export default { | ------- | ------------------------------------- | | default | Anchor content, show index by default | +### IndexBar Methods + +Use [ref](https://vuejs.org/v2/api/#ref) to get IndexBar instance and call instance methods. + +| Name | Description | Attribute | Return value | +| --- | --- | --- | --- | +| scrollTo `v2.12.2` | scroll to target element | _index: number \| string_ | - | + ### Less Variables How to use: [Custom Theme](#/en-US/theme). diff --git a/src/index-bar/README.zh-CN.md b/src/index-bar/README.zh-CN.md index 913738a86..a2e55d664 100644 --- a/src/index-bar/README.zh-CN.md +++ b/src/index-bar/README.zh-CN.md @@ -93,6 +93,14 @@ export default { | ------- | -------------------------------- | | default | 锚点位置显示内容,默认为索引字符 | +### IndexBar 方法 + +通过 ref 可以获取到 IndexBar 实例并调用实例方法,详见[组件实例方法](#/zh-CN/advanced-usage#zu-jian-shi-li-fang-fa)。 + +| 方法名 | 说明 | 参数 | 返回值 | +| ------------------ | -------------- | ------------------------- | ------ | +| scrollTo `v2.12.2` | 滚动到指定锚点 | _index: number \| string_ | - | + ### 样式变量 组件提供了下列 Less 变量,可用于自定义样式,使用方法请参考[主题定制](#/zh-CN/theme)。 diff --git a/src/index-bar/index.js b/src/index-bar/index.js index 007554b50..457a3c9ee 100644 --- a/src/index-bar/index.js +++ b/src/index-bar/index.js @@ -184,12 +184,7 @@ export default createComponent({ } }, - scrollToElement(element) { - const { index } = element.dataset; - if (!index) { - return; - } - + scrollTo(index) { const match = this.children.filter( (item) => String(item.index) === index ); @@ -205,6 +200,11 @@ export default createComponent({ } }, + scrollToElement(element) { + const { index } = element.dataset; + this.scrollTo(index); + }, + onTouchEnd() { this.active = null; }, diff --git a/types/index-bar.d.ts b/types/index-bar.d.ts new file mode 100644 index 000000000..b004c9ccb --- /dev/null +++ b/types/index-bar.d.ts @@ -0,0 +1,5 @@ +import { VanComponent } from './component'; + +export class IndexBar extends VanComponent { + scrollTo(index: number | string): void; +} diff --git a/types/index.d.ts b/types/index.d.ts index 53c2eb478..c4fcdbb56 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -14,6 +14,7 @@ import { DropdownItem } from './dropdown-item'; import { Field } from './field'; import { Form } from './form'; import { ImagePreview } from './image-preview'; +import { IndexBar } from './index-bar'; import { Lazyload } from './lazyload'; import { List } from './list'; import { Locale } from './locale'; @@ -57,7 +58,6 @@ export class GoodsActionIcon extends VanComponent {} export class Icon extends VanComponent {} export class Image extends VanComponent {} export class IndexAnchor extends VanComponent {} -export class IndexBar extends VanComponent {} export class Info extends VanComponent {} export class Loading extends VanComponent {} export class NavBar extends VanComponent {} @@ -108,6 +108,7 @@ export { Form, Field, ImagePreview, + IndexBar, Lazyload, List, Locale,