Skip to content

Commit 90a2fc0

Browse files
committed
fix(SwipeAction): 替换 SwipeAction 实现方式
1 parent b0c6a8f commit 90a2fc0

File tree

1 file changed

+12
-17
lines changed
  • packages/taro-ui/src/components/swipe-action

1 file changed

+12
-17
lines changed

packages/taro-ui/src/components/swipe-action/index.tsx

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ export default class AtSwipeAction extends React.Component<
2929
this.maxOffsetSize = maxDistance
3030
this.state = {
3131
componentId: uuid(),
32-
offsetSize: 0,
32+
// eslint-disable-next-line no-extra-boolean-cast
33+
offsetSize: !!isOpened ? -this.maxOffsetSize : 0,
3334
_isOpened: !!isOpened,
3435
needAnimation: false
3536
}
36-
this.moveX = 0
37+
this.moveX = this.state.offsetSize
3738
this.eleWidth = areaWidth
3839
this.moveRatio = moveRatio || 0.5
3940
}
@@ -43,17 +44,16 @@ export default class AtSwipeAction extends React.Component<
4344
const { _isOpened } = this.state
4445

4546
if (isOpened !== _isOpened) {
46-
const isOpened = !!this.props.isOpened
4747
this.moveX = isOpened ? 0 : this.maxOffsetSize
48-
this._reset(isOpened) // TODO: Check behavior
48+
this._reset(!!isOpened) // TODO: Check behavior
4949
}
5050
}
5151

5252
private _reset(isOpened: boolean): void {
5353
if (isOpened) {
5454
this.setState({
5555
_isOpened: true,
56-
offsetSize: 0
56+
offsetSize: -this.maxOffsetSize
5757
})
5858
} else {
5959
this.setState(
@@ -62,7 +62,7 @@ export default class AtSwipeAction extends React.Component<
6262
},
6363
() => {
6464
this.setState({
65-
offsetSize: this.maxOffsetSize,
65+
offsetSize: 0,
6666
_isOpened: false
6767
})
6868
}
@@ -101,22 +101,22 @@ export default class AtSwipeAction extends React.Component<
101101
}
102102

103103
onTouchEnd = e => {
104-
if (this.moveX === 0) {
104+
if (this.moveX === -this.maxOffsetSize) {
105105
this._reset(true)
106106
this.handleOpened(e)
107107
return
108108
}
109-
if (this.moveX === this.maxOffsetSize) {
109+
if (this.moveX === 0) {
110110
this._reset(false)
111111
this.handleClosed(e)
112112
return
113113
}
114-
if (this.state._isOpened && this.moveX > 0) {
114+
if (this.state._isOpened && this.moveX < 0) {
115115
this._reset(false)
116116
this.handleClosed(e)
117117
return
118118
}
119-
if (this.maxOffsetSize - this.moveX < this.maxOffsetSize * this.moveRatio) {
119+
if (Math.abs(this.moveX) < this.maxOffsetSize * this.moveRatio) {
120120
this._reset(false)
121121
this.handleClosed(e)
122122
} else {
@@ -129,12 +129,6 @@ export default class AtSwipeAction extends React.Component<
129129
this.moveX = e.detail.x
130130
}
131131

132-
componentDidMount(): void {
133-
const isOpened = !!this.props.isOpened
134-
this.moveX = isOpened ? 0 : this.maxOffsetSize
135-
this._reset(isOpened)
136-
}
137-
138132
public render(): JSX.Element {
139133
const { componentId, offsetSize } = this.state
140134
const { options } = this.props
@@ -163,7 +157,8 @@ export default class AtSwipeAction extends React.Component<
163157
onTouchEnd={this.onTouchEnd}
164158
onChange={this.onChange}
165159
style={{
166-
width: `${this.eleWidth}px`
160+
width: `${this.eleWidth}px`,
161+
left: `${this.maxOffsetSize}px`
167162
}}
168163
>
169164
{this.props.children}

0 commit comments

Comments
 (0)