Skip to content

Commit 33954c5

Browse files
committed
refactor(swipe-action): 重构 swipe-action 组件
* 废弃 maxDistance、areaWidth 属性
1 parent e38a4a9 commit 33954c5

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

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

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import classNames from 'classnames'
22
import PropTypes, { InferProps } from 'prop-types'
33
import React from 'react'
4+
import Taro from '@tarojs/taro'
45
import { Text, View, MovableArea, MovableView } from '@tarojs/components'
56
import { CommonEvent } from '@tarojs/components/types/common'
67
import {
78
AtSwipeActionProps,
89
AtSwipeActionState,
910
SwipeActionOption
1011
} from '../../../types/swipe-action'
11-
import { uuid } from '../../common/utils'
12+
import { uuid, delayGetClientRect } from '../../common/utils'
1213
import AtSwipeActionOptions from './options/index'
1314

1415
export default class AtSwipeAction extends React.Component<
@@ -18,48 +19,86 @@ export default class AtSwipeAction extends React.Component<
1819
public static defaultProps: AtSwipeActionProps
1920
public static propTypes: InferProps<AtSwipeActionProps>
2021

21-
private maxOffsetSize: number
2222
private moveX: number
23-
private eleWidth: number
2423
private moveRatio: number
2524

2625
public constructor(props: AtSwipeActionProps) {
2726
super(props)
28-
const { isOpened, maxDistance, areaWidth, moveRatio } = props
29-
this.maxOffsetSize = maxDistance
27+
const { isOpened, moveRatio } = props
3028
this.state = {
3129
componentId: uuid(),
3230
// eslint-disable-next-line no-extra-boolean-cast
33-
offsetSize: !!isOpened ? -this.maxOffsetSize : 0,
31+
offsetSize: 0,
3432
_isOpened: !!isOpened,
35-
needAnimation: false
33+
needAnimation: false,
34+
eleWidth: 0,
35+
maxOffsetSize: 0
3636
}
3737
this.moveX = this.state.offsetSize
38-
this.eleWidth = areaWidth
3938
this.moveRatio = moveRatio || 0.5
4039
}
4140

41+
public componentDidMount(): void {
42+
this.getAreaWidth()
43+
}
44+
45+
// 当 eleWidth 发生变化时,需要重新计算 maxOffsetSize
46+
public componentDidUpdate(_, prevState: AtSwipeActionState): void {
47+
const { eleWidth } = this.state
48+
if (prevState.eleWidth !== eleWidth) {
49+
this.getMaxOffsetSize()
50+
}
51+
}
52+
4253
public UNSAFE_componentWillReceiveProps(nextProps: AtSwipeActionProps): void {
4354
const { isOpened } = nextProps
44-
const { _isOpened } = this.state
55+
const { _isOpened, maxOffsetSize } = this.state
4556

4657
if (isOpened !== _isOpened) {
47-
this.moveX = isOpened ? 0 : this.maxOffsetSize
58+
this.moveX = isOpened ? 0 : maxOffsetSize
4859
this._reset(!!isOpened) // TODO: Check behavior
4960
}
5061
}
5162

63+
/**
64+
* 获取滑动区域宽度
65+
*/
66+
private async getAreaWidth(): Promise<void> {
67+
const systemInfo = await Taro.getSystemInfo()
68+
this.setState({
69+
eleWidth: systemInfo.windowWidth
70+
})
71+
}
72+
73+
/**
74+
* 获取最大偏移量
75+
*/
76+
private async getMaxOffsetSize(): Promise<void> {
77+
const { componentId } = this.state
78+
79+
const actionOptionsRect = await delayGetClientRect({
80+
selectorStr: `#swipeActionOptions-${componentId}`
81+
})
82+
83+
const maxOffsetSize = actionOptionsRect[0].width
84+
85+
this.setState({
86+
maxOffsetSize
87+
})
88+
}
89+
5290
private _reset(isOpened: boolean): void {
5391
if (isOpened) {
92+
const { maxOffsetSize } = this.state
5493
if (process.env.TARO_ENV === 'jd') {
5594
this.setState({
5695
_isOpened: true,
57-
offsetSize: -this.maxOffsetSize + 0.01
96+
offsetSize: -maxOffsetSize + 0.01
5897
})
5998
} else {
6099
this.setState({
61100
_isOpened: true,
62-
offsetSize: -this.maxOffsetSize
101+
offsetSize: -maxOffsetSize
63102
})
64103
}
65104
} else {
@@ -108,7 +147,8 @@ export default class AtSwipeAction extends React.Component<
108147
}
109148

110149
onTouchEnd = e => {
111-
if (this.moveX === -this.maxOffsetSize) {
150+
const { maxOffsetSize } = this.state
151+
if (this.moveX === -maxOffsetSize) {
112152
this._reset(true)
113153
this.handleOpened(e)
114154
return
@@ -123,7 +163,7 @@ export default class AtSwipeAction extends React.Component<
123163
this.handleClosed(e)
124164
return
125165
}
126-
if (Math.abs(this.moveX) < this.maxOffsetSize * this.moveRatio) {
166+
if (Math.abs(this.moveX) < maxOffsetSize * this.moveRatio) {
127167
this._reset(false)
128168
this.handleClosed(e)
129169
} else {
@@ -137,7 +177,8 @@ export default class AtSwipeAction extends React.Component<
137177
}
138178

139179
public render(): JSX.Element {
140-
const { componentId, offsetSize } = this.state
180+
const { componentId, maxOffsetSize, eleWidth, offsetSize } = this.state
181+
141182
const { options, disabled } = this.props
142183
const rootClass = classNames('at-swipe-action', this.props.className)
143184

@@ -146,14 +187,13 @@ export default class AtSwipeAction extends React.Component<
146187
id={`swipeAction-${componentId}`}
147188
className={rootClass}
148189
style={{
149-
width: `${this.eleWidth}px`
190+
width: `${eleWidth}px`
150191
}}
151192
>
152193
<MovableArea
153194
className='at-swipe-action__area'
154195
style={{
155-
width: `${this.eleWidth + this.maxOffsetSize}px`,
156-
transform: `translate(-${this.maxOffsetSize}px, 0)`
196+
width: `${eleWidth}px`
157197
}}
158198
>
159199
<MovableView
@@ -165,8 +205,7 @@ export default class AtSwipeAction extends React.Component<
165205
onChange={this.onChange}
166206
disabled={disabled}
167207
style={{
168-
width: `${this.eleWidth}px`,
169-
left: `${this.maxOffsetSize}px`
208+
width: `${eleWidth + maxOffsetSize}px`
170209
}}
171210
>
172211
{this.props.children}
@@ -175,8 +214,7 @@ export default class AtSwipeAction extends React.Component<
175214
options={options}
176215
componentId={componentId}
177216
customStyle={{
178-
transform: `translate(${this.maxOffsetSize}px, 0)`,
179-
opacity: 1
217+
opacity: maxOffsetSize ? 1 : 0
180218
}}
181219
>
182220
{options.map((item, key) => (

packages/taro-ui/src/style/components/list.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ $at-list-item-horizontal-padding: 24px;
3434
color: $at-list-text-color;
3535
font-size: $font-size-lg;
3636
line-height: $line-height-zh;
37+
width: 100%;
3738
@include active;
3839
@include hairline-bottom-relative($left: $spacing-h-lg);
3940

packages/taro-ui/types/swipe-action.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ export interface AtSwipeActionProps extends AtComponent {
5656

5757
/**
5858
* 滑块最大滑动距离,一般是按钮个数乘以按钮宽度
59+
* @deprecated 已废弃,无需设置 maxDistance
5960
*/
60-
maxDistance: number
61+
maxDistance?: number
6162

6263
/**
6364
* SwipeAction 组件宽度
65+
* @deprecated 已废弃,无需设置 areaWidth
6466
*/
65-
areaWidth: number
67+
areaWidth?: number
6668

6769
/**
6870
* 判断是否需要打开的比例阈值,即 滑块滑动距离 / 滑块最大滑动距离, 默认为 0.5
@@ -75,6 +77,8 @@ export interface AtSwipeActionState {
7577
offsetSize: number
7678
_isOpened: boolean
7779
needAnimation: boolean
80+
eleWidth: number
81+
maxOffsetSize: number
7882
}
7983

8084
export interface AtSwipeActionOptionsProps extends AtComponent {

0 commit comments

Comments
 (0)