Skip to content

Commit

Permalink
fix(AtIndexes): 修复 psaren#49 onClick 以及 onScrollIntoView 事件不生效
Browse files Browse the repository at this point in the history
  • Loading branch information
b2nil committed Sep 18, 2020
1 parent b0ca205 commit 98ecf55
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default {
onClick(item) {
Taro.showToast({
title: `onClick: ${item}`,
title: `onClick: ${JSON.stringify(item.name)}: ${JSON.stringify(item.key)}`,
icon: 'none',
})
},
Expand Down
59 changes: 56 additions & 3 deletions packages/taro-ui-vue/src/components/indexes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export default {
startTop: 0,
// 右侧导航元素高度
itemHeight: 0,
// scroll-view 列表项目的高度数组
scrollItemHeights: [],
// 当前索引
currentIndex: -1,
listId: isTest() ? 'indexes-list-AOTU2018' : `list-${uuid()}`,
Expand Down Expand Up @@ -149,6 +151,7 @@ export default {
}

this.updateState({
_scrollTop: this.scrollItemHeights[idx],
_scrollIntoView,
_tipText,
})
Expand Down Expand Up @@ -190,14 +193,20 @@ export default {
Taro.vibrateShort()
}
},
getItemHeight () {
delayQuerySelector(this, '.at-indexes__menu').then((rect) => {
async getItemHeight () {
await delayQuerySelector(this, '.at-indexes__menu').then((rect) => {
const arr = [...rect, { top: 0, height: 0 }]
const len = this.list.length
this.menuHeight = arr[0].height
this.startTop = arr[0].top
this.itemHeight = Math.floor(this.menuHeight / (len + 1))
})

if (this.list.length > 0) {
await this._getScrollListItemHeights(this.list).then(res => {
this.scrollItemHeights = [...res]
})
}
},
initData() {
if (this.isWeb) {
Expand All @@ -208,10 +217,54 @@ export default {
}, 100)
}
},
_getHeight(selector: string, delay?: number) {
return new Promise<number>(resolve => {
delayQuerySelector(this, selector, delay).then(rect => {
// @ts-ignore
if(rect && rect[0]) {
// @ts-ignore
resolve(rect[0].height)
}
})
})
},
/**
*
* @param {Array<ListItem>} list
*/
_getScrollListItemHeights(list) {
return new Promise<number[]>(resolve => {
if (list.length > 0) {
let rawHeights: Promise<number>[] = []
let itemHeights: number[] = []

// 获取 #at-indexes__top 的高度
rawHeights.push(this._getHeight(`#at-indexes__top`))

// 获取 #at-indexes——list-${key} 的高度
list.forEach((item) => {
rawHeights.push(this._getHeight(`#at-indexes__list-${item.key}`))
})

Promise.all(rawHeights).then(res => {
let height = 0
itemHeights.push(height)

for (let i = 0; i < res.length; i++) {
height += res[i]
itemHeights.push(height)
}

resolve(itemHeights)
})
}
})
},
handleScroll(e) {
if (e && e.detail) {
this.setState({
_scrollTop: e.detail.scrollTop,
// _scrollTop: e.detail.scrollTop,
_scrollIntoView: ''
})
}
},
Expand Down
26 changes: 13 additions & 13 deletions packages/taro-ui-vue/src/components/indexes/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<template>
<view :class="rootCls" :style="customStyle">
<view
:class="rootCls"
:style="customStyle"
>
<AtToast
:custom-style="toastStyle"
:is-opened="state._isShowToast"
Expand All @@ -14,28 +17,27 @@
<view
class="at-indexes__menu-item"
@tap="jumpTarget('at-indexes__top', 0)"
>
{{ topKey }}
</view>
>{{ topKey }}</view>
<view
v-for="(dataList, i) in list"
:key="dataList.key"
class="at-indexes__menu-item"
@tap="jumpTarget(`at-indexes__list-${dataList.key}`, i + 1)"
>
{{ dataList.key }}
</view>
>{{ dataList.key }}</view>
</view>
<scroll-view
:id="listId"
class="at-indexes__body"
:scroll-y="true"
:scroll-with-animation="animation"
:scroll-top="isWEB ? state._scrollTop : undefined"
:scroll-top="state._scrollTop"
:scroll-into-view="!isWEB ? state._scrollIntoView : ''"
@scroll="handleScroll"
>
<view id="at-indexes__top" class="at-indexes__content">
<view
id="at-indexes__top"
class="at-indexes__content"
>
<slot />
</view>
<view
Expand All @@ -44,16 +46,14 @@
:key="dataList.key"
class="at-indexes__list"
>
<view class="at-indexes__list-title">
{{ dataList.title }}
</view>
<view class="at-indexes__list-title">{{ dataList.title }}</view>
<AtList>
<template v-if="dataList.items">
<AtListItem
v-for="item in dataList.items"
:key="item.name"
:title="item.name"
@tap="handleClick"
:onClick="handleClick.bind(this, item)"
/>
</template>
</AtList>
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-ui-vue/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const getEnvs = (): ENVS => {

function delay(delayTime = 500): Promise<null> {
return new Promise((resolve) => {
if ([Taro.ENV_TYPE.WEB, Taro.ENV_TYPE.SWAN].includes(ENV)) {
if ([Taro.ENV_TYPE.WEB, Taro.ENV_TYPE.SWAN, Taro.ENV_TYPE.WEAPP].includes(ENV)) {
setTimeout(() => {
resolve()
}, delayTime)
Expand Down

0 comments on commit 98ecf55

Please # to comment.