Skip to content

Commit

Permalink
fix: 修复设置hoverStartTime后,立即出现点击态
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarryxin authored and zhiqingchen committed Apr 2, 2021
1 parent 8c485f8 commit f7bbd62
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions packages/taro-components-rn/src/components/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ class _Button extends React.Component<ButtonProps, ButtonState> {
size: 'default',
type: 'default',
hoverStartTime: 20,
hoverStayTime: 70
hoverStayTime: 70,
disabled: false,
}

$touchable = React.createRef<TouchableWithoutFeedback>()

isTouchEnd = false
pressInTimer: number
pressOutTimer: number

state: ButtonState = {
valve: new Animated.Value(0),
isHover: false
Expand Down Expand Up @@ -85,11 +90,35 @@ class _Button extends React.Component<ButtonProps, ButtonState> {
}

onPressIn = (): void => {
this.setState({ isHover: true })
const { hoverStartTime, hoverStyle } = this.props
this.isTouchEnd = false
if (hoverStyle) {
this.pressInTimer = setTimeout(() => {
this.setState({ isHover: true }, () => {
if (this.isTouchEnd) {
this.shortPress()
}
})
clearTimeout(this.pressInTimer)
}, hoverStartTime)
}
}

shortPress = (): void => {
const { hoverStayTime } = this.props
this.pressOutTimer = setTimeout(() => {
this.setState({ isHover: false })
clearTimeout(this.pressOutTimer)
}, hoverStayTime)
}

onPressOut = (): void => {
this.setState({ isHover: false })
const { hoverStyle } = this.props
const { isHover } = this.state
this.isTouchEnd = true
if (hoverStyle && isHover) {
this.shortPress()
}
}

_simulateNativePress = (evt: GestureResponderEvent): void => {
Expand Down Expand Up @@ -117,8 +146,6 @@ class _Button extends React.Component<ButtonProps, ButtonState> {
disabled,
loading,
hoverStyle,
hoverStartTime,
hoverStayTime
} = this.props

const isDefaultSize: boolean = size === 'default'
Expand Down Expand Up @@ -147,12 +174,12 @@ class _Button extends React.Component<ButtonProps, ButtonState> {

return (
<TouchableWithoutFeedback
delayPressIn={hoverStartTime}
delayPressOut={hoverStayTime}
onPress={this.onPress}
onLongPress={this.onPress}
onPressIn={this.onPressIn}
onPressOut={this.onPressOut}
ref={this.$touchable}
disabled={disabled}
>
<View
style={[
Expand Down

0 comments on commit f7bbd62

Please sign in to comment.