Skip to content

Commit

Permalink
fix(rn): 修复 MovableView 组件状态变更无效及滑动时跳变的问题 (#9901)
Browse files Browse the repository at this point in the history
* fix: 修复组件 MovableView 属性状态更改无效的问题

* fix: 切换方向后滑动跳变的问题
  • Loading branch information
iChengbo authored Jul 27, 2021
1 parent 7f41948 commit edb78a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
13 changes: 7 additions & 6 deletions packages/taro-components-rn/src/components/MovableArea/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ class _MovableArea extends React.Component<MovableAreaProps, any> {
constructor(props: MovableAreaProps) {
super(props)
this.state = {
child: null
width: this.props.width || 100,
height: this.props.height || 100,
}
}

_onLayout = (event: LayoutChangeEvent): void => {
const { width, height } = event.nativeEvent.layout
const child = React.cloneElement(this.props.children, { layout: { width, height } })
this.setState({
width,
height,
child
})
}

render(): JSX.Element {
const { style, width = 100, height = 100 } = this.props
const { child } = this.state
return <View style={[{ height, width, overflow: 'hidden' }, style]} onLayout={this._onLayout}>{child}</View>
const { style } = this.props
const { width, height } = this.state
return <View style={[{ height, width, overflow: 'hidden' }, style]} onLayout={this._onLayout}>
{React.cloneElement(this.props.children, { layout: { width, height } })}
</View>
}
}

Expand Down
29 changes: 13 additions & 16 deletions packages/taro-components-rn/src/components/MovableView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { AnimatedValueProps, MovableViewProps } from './PropsType'
class _MovableView extends React.Component<MovableViewProps, any> {
static defaultProps = {
direction: 'none',
onDragStart: (): void => {},
onDragEnd: (): void => {},
onChange: (): void => {},
onDragStart: (): void => { },
onDragEnd: (): void => { },
onChange: (): void => { },
disabled: false,
animation: true
}
Expand All @@ -22,7 +22,6 @@ class _MovableView extends React.Component<MovableViewProps, any> {
super(props)
this.state = {
pan: new Animated.ValueXY(),
disabled: props.disabled,
xOffset: 0,
yOffset: 0
}
Expand All @@ -31,20 +30,25 @@ class _MovableView extends React.Component<MovableViewProps, any> {

createPanResponder = (): void => {
this.panResponder = PanResponder.create({
onMoveShouldSetPanResponder: () => !this.state.disabled,
onMoveShouldSetPanResponderCapture: () => !this.state.disabled,
onMoveShouldSetPanResponder: () => !this.props.disabled,
onMoveShouldSetPanResponderCapture: () => !this.props.disabled,
onPanResponderGrant: () => {
const { pan } = this.state
pan.setOffset({ x: pan.x._value, y: pan.y._value })
const { direction } = this.props
pan.setOffset({
x: direction === 'all' || direction === 'horizontal' ? pan.x._value : 0,
y: direction === 'all' || direction === 'vertical' ? pan.y._value : 0
})
this.props.onDragStart && this.props.onDragStart()
},
onPanResponderMove: (e, gestureState) => {
const { direction } = this.props
Animated.event(
[
null,
{
...(this.props.direction !== 'none' && this.props.direction !== 'vertical' ? { dx: this.state.pan.x } : {}),
...(this.props.direction !== 'none' && this.props.direction !== 'horizontal' ? { dy: this.state.pan.y } : {}),
dx: direction === 'all' || direction === 'horizontal' ? this.state.pan.x : new Animated.Value(0),
dy: direction === 'all' || direction === 'vertical' ? this.state.pan.y : new Animated.Value(0),
}
],
{
Expand Down Expand Up @@ -79,13 +83,6 @@ class _MovableView extends React.Component<MovableViewProps, any> {
pan.removeAllListeners()
}

changeDisableStatus = (): void => {
const { disabled } = this.state
this.setState({
disabled: !disabled
})
}

_onLayout = (event: LayoutChangeEvent): void => {
const { width, height } = event.nativeEvent.layout
this.W = width
Expand Down

0 comments on commit edb78a5

Please sign in to comment.