Skip to content

Commit

Permalink
fix: 兼容swipe-action放入自定义组件时
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyongming committed Aug 15, 2024
1 parent 73a514a commit 6228449
Show file tree
Hide file tree
Showing 5 changed files with 839 additions and 246 deletions.
8 changes: 4 additions & 4 deletions packages/taro-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-json": "^4.0.2",
"@rollup/plugin-node-resolve": "^7.1.1",
"@tarojs/components": "3.6.6",
"@tarojs/react": "3.6.6",
"@tarojs/runtime-rn": "3.6.6",
"@tarojs/taro": "3.6.6",
"@tarojs/components": "3.6.34",
"@tarojs/react": "3.6.34",
"@tarojs/runtime-rn": "3.6.34",
"@tarojs/taro": "3.6.34",
"@types/classnames": "^2.2.9",
"@types/node": "~16.6.0",
"@types/react-native": "^0.64.13",
Expand Down
82 changes: 52 additions & 30 deletions packages/taro-ui/src/components/swipe-action/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,41 @@ export default class AtSwipeAction extends React.Component<
}
}

/**
* 获取滑块区域与options操作区域选择器
*/
private getSelectorStr(type: string): string {
const { componentId } = this.state
const { parentSelector } = this.props
return (parentSelector ? `${parentSelector} >>> ` : '') + `#${type}-${componentId}`
}

/**
* 获取滑动区域宽度
*/
private async getAreaWidth(): Promise<void> {
const systemInfo = await Taro.getSystemInfo()
const actionRect = await delayGetClientRect({
selectorStr: this.getSelectorStr('swipeAction')
})

let eleWidth = actionRect[0].width

if (!eleWidth) {
const systemInfo = await Taro.getSystemInfo()
eleWidth = systemInfo.windowWidth;
}

this.setState({
eleWidth: systemInfo.windowWidth
eleWidth
})
}

/**
* 获取最大偏移量
*/
private async getMaxOffsetSize(): Promise<void> {
const { componentId } = this.state

const actionOptionsRect = await delayGetClientRect({
selectorStr: `#swipeActionOptions-${componentId}`
selectorStr: this.getSelectorStr('swipeActionOptions')
})

const maxOffsetSize = actionOptionsRect[0].width
Expand All @@ -86,32 +103,29 @@ export default class AtSwipeAction extends React.Component<
}

private _reset(isOpened: boolean): void {
if (isOpened) {
const { maxOffsetSize } = this.state
if (process.env.TARO_ENV === 'jd') {
this.setState({
_isOpened: true,
offsetSize: -maxOffsetSize + 0.01
})
this.setState({
offsetSize: this.moveX
}, () => {
if (isOpened) {
const { maxOffsetSize } = this.state
if (process.env.TARO_ENV === 'jd') {
this.setState({
_isOpened: true,
offsetSize: -maxOffsetSize + 0.01
})
} else {
this.setState({
_isOpened: true,
offsetSize: -maxOffsetSize
})
}
} else {
this.setState({
_isOpened: true,
offsetSize: -maxOffsetSize
offsetSize: 0,
_isOpened: false
})
}
} else {
this.setState(
{
offsetSize: this.moveX
},
() => {
this.setState({
offsetSize: 0,
_isOpened: false
})
}
)
}
})
}

private handleOpened = (event: CommonEvent): void => {
Expand Down Expand Up @@ -165,18 +179,20 @@ export default class AtSwipeAction extends React.Component<
const { options, disabled } = this.props
const rootClass = classNames('at-swipe-action', this.props.className)

const areaWidth = eleWidth > 0 ? `${eleWidth}px` : '100%'

return (
<View
id={`swipeAction-${componentId}`}
className={rootClass}
style={{
width: `${eleWidth}px`
width: areaWidth
}}
>
<MovableArea
className='at-swipe-action__area'
style={{
width: `${eleWidth}px`
width: areaWidth
}}
>
<MovableView
Expand All @@ -191,7 +207,13 @@ export default class AtSwipeAction extends React.Component<
width: `${eleWidth + maxOffsetSize}px`
}}
>
{this.props.children}
<View
style={{
width: areaWidth
}}
>
{this.props.children}
</View>
{Array.isArray(options) && options.length > 0 ? (
<AtSwipeActionOptions
options={options}
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-ui/src/style/components/swipe-action.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
display: flex;
height: 100%;
z-index: 2;
min-width: 100%;

&.animtion {
transition: transform 300ms $timing-func;
Expand All @@ -34,6 +35,7 @@
color: $at-swipe-action-color;
font-size: $at-swipe-action-font-size;
width: 120px;
box-sizing: border-box;

text-align: center;
background-color: $at-swipe-action-option-bg-color;
Expand All @@ -50,4 +52,4 @@
z-index: 1;
opacity: 0;
}
}
}
5 changes: 5 additions & 0 deletions packages/taro-ui/types/swipe-action.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export interface AtSwipeActionProps extends AtComponent {
* @deprecated 已废弃,无需设置 areaWidth
*/
areaWidth?: number

/**
* 用于兼容获取跨自定义组件的后代元素
*/
parentSelector?: string
}

export interface AtSwipeActionState {
Expand Down
Loading

0 comments on commit 6228449

Please sign in to comment.