Skip to content

Commit

Permalink
fix(h5): 为H5的View组件添加长按时间onLongPress支持,fix #2857
Browse files Browse the repository at this point in the history
  • Loading branch information
SunnyQjm committed May 29, 2019
1 parent 23acaa7 commit 460fd70
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/taro-components/src/components/view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class View extends Nerv.Component {
}
}

timeoutEvent = 0;
startTime = 0;
render () {
const {
hoverClass,
Expand Down Expand Up @@ -45,8 +47,22 @@ class View extends Nerv.Component {
}, hoverStartTime)
}
onTouchStart && onTouchStart(e)
if (this.props.onLongPress) {
this.timeoutEvent = setTimeout(() => {
this.props.onLongPress()
}, 500)
this.startTime = (new Date()).getTime()
}
}
const _onTouchMove = e => {
clearTimeout(this.timeoutEvent)
}

const _onTouchEnd = e => {
const spanTime = (new Date().getTime()) - this.startTime
if (spanTime < 350) {
clearTimeout(this.timeoutEvent)
}
if (hoverClass) {
this.setState(() => ({
touch: false
Expand Down Expand Up @@ -75,6 +91,7 @@ class View extends Nerv.Component {
className={cls}
onTouchStart={_onTouchStart}
onTouchEnd={_onTouchEnd}
onTouchMove={_onTouchMove}
>
{this.props.children}
</div>
Expand Down

0 comments on commit 460fd70

Please sign in to comment.