-
Notifications
You must be signed in to change notification settings - Fork 153
fix: SwipeAction swiped #1523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix: SwipeAction swiped #1523
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,27 +95,69 @@ Component( | |
| transformOptions({ | ||
| props: SwipeActionDefaultProps, | ||
| didMount() { | ||
| const { defaultSwiped, elasticity } = this.getProps(); | ||
| this.setButtonItemWidth(); | ||
| const { defaultSwiped, swiped, elasticity } = this.getProps(); | ||
| this.setData({ inertiaWidth: !isOldVersion && elasticity ? 20 : 0 }); | ||
| if (defaultSwiped) { | ||
| this.initWidth((maxSwipe: any) => { | ||
| maxSwipe && | ||
| this.setData({ | ||
| swipeX: (maxSwipe + 0.01) * (defaultSwiped === 'right' ? -1 : 1), | ||
| swipedR: defaultSwiped === 'right', | ||
| swipedL: defaultSwiped === 'left', | ||
| }); | ||
| }); | ||
|
|
||
| // 优先使用 swiped 属性(受控),其次使用 defaultSwiped(非受控) | ||
| const initialSwiped = swiped || defaultSwiped; | ||
|
|
||
| // 先设置按钮宽度,然后在回调中处理初始展开状态 | ||
| this.setButtonItemWidth(); | ||
|
|
||
| if (initialSwiped) { | ||
| // 需要等待按钮宽度设置完成后再初始化展开状态 | ||
| setTimeout(() => { | ||
| this.initWidth((maxSwipe: any) => { | ||
| if (maxSwipe) { | ||
| const direction = initialSwiped === 'left' ? 'left' : 'right'; | ||
| this.setData({ | ||
| swipeX: (maxSwipe + 0.01) * (direction === 'right' ? -1 : 1), | ||
| swipedR: direction === 'right', | ||
| swipedL: direction === 'left', | ||
| }); | ||
| } | ||
| }); | ||
|
Comment on lines
+110
to
+119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 重复的初始化逻辑应该提取为独立方法 Lines 110-119 (在 建议提取为独立的辅助方法: + applySwipedState(swiped, delay = 50) {
+ setTimeout(() => {
+ this.initWidth((maxSwipe: any) => {
+ if (maxSwipe) {
+ const direction = swiped === 'left' ? 'left' : 'right';
+ this.setData({
+ swipeX: (maxSwipe + 0.01) * (direction === 'right' ? -1 : 1),
+ swipedR: direction === 'right',
+ swipedL: direction === 'left',
+ tapTypeL: '',
+ tapTypeR: '',
+ });
+ }
+ });
+ }, delay);
+ },
didMount() {
const { defaultSwiped, swiped, elasticity } = this.getProps();
this.setData({ inertiaWidth: !isOldVersion && elasticity ? 20 : 0 });
const initialSwiped = swiped || defaultSwiped;
this.setButtonItemWidth();
if (initialSwiped) {
- setTimeout(() => {
- this.initWidth((maxSwipe: any) => {
- if (maxSwipe) {
- const direction = initialSwiped === 'left' ? 'left' : 'right';
- this.setData({
- swipeX: (maxSwipe + 0.01) * (direction === 'right' ? -1 : 1),
- swipedR: direction === 'right',
- swipedL: direction === 'left',
- });
- }
- });
- }, 50);
+ this.applySwipedState(initialSwiped);
}
},
didUpdate(prevProp) {
// ... 省略其他代码
if (swipedChanged) {
if (!swiped) {
// ... 重置逻辑
} else {
- setTimeout(() => {
- this.initWidth((maxSwipe: any) => {
- if (maxSwipe) {
- const direction = swiped === 'left' ? 'left' : 'right';
- this.setData({
- swipeX: (maxSwipe + 0.01) * (direction === 'right' ? -1 : 1),
- swipedR: direction === 'right',
- swipedL: direction === 'left',
- tapTypeL: '',
- tapTypeR: '',
- });
- }
- });
- }, 50);
+ this.applySwipedState(swiped);
}
}
},Also applies to: 145-156 🤖 Prompt for AI Agents |
||
| }, 50); | ||
|
Comment on lines
+109
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of a fixed |
||
| } | ||
| }, | ||
|
Comment on lines
97
to
122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 缺少定时器清理可能导致状态更新异常
建议将 timer ID 保存到实例变量,并在 +let initTimer = null;
+
Component(
transformOptions({
props: SwipeActionDefaultProps,
didMount() {
const { defaultSwiped, swiped, elasticity } = this.getProps();
this.setData({ inertiaWidth: !isOldVersion && elasticity ? 20 : 0 });
const initialSwiped = swiped || defaultSwiped;
this.setButtonItemWidth();
if (initialSwiped) {
- setTimeout(() => {
+ initTimer = setTimeout(() => {
this.initWidth((maxSwipe: any) => {
if (maxSwipe) {
const direction = initialSwiped === 'left' ? 'left' : 'right';
this.setData({
swipeX: (maxSwipe + 0.01) * (direction === 'right' ? -1 : 1),
swipedR: direction === 'right',
swipedL: direction === 'left',
});
}
});
}, 50);
}
},
+ didUnmount() {
+ if (initTimer) {
+ clearTimeout(initTimer);
+ initTimer = null;
+ }
+ },
🤖 Prompt for AI Agents |
||
| didUpdate(prevProp) { | ||
| const { swiped, damping, elasticity } = this.getProps(); | ||
| // 设置不同的滑动位置时需要重置 | ||
| const rs = prevProp.swiped !== swiped && !swiped; | ||
| const swipedChanged = prevProp.swiped !== swiped; | ||
| const is = prevProp.elasticity !== elasticity; | ||
| const ds = prevProp.damping !== damping; | ||
| if (rs || is || ds) { | ||
|
|
||
| // 处理 swiped 变化 | ||
| if (swipedChanged) { | ||
| if (!swiped) { | ||
| // swiped 变为 false/空,需要关闭 | ||
| this.setData({ | ||
| swipeX: 0, | ||
| swipedR: false, | ||
| swipedL: false, | ||
| tapTypeL: '', | ||
| tapTypeR: '', | ||
| }); | ||
| } else { | ||
| // swiped 变为 true/'left'/'right',需要打开 | ||
| // 需要等待下一个事件循环,确保按钮宽度已经渲染 | ||
| setTimeout(() => { | ||
| this.initWidth((maxSwipe: any) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if (maxSwipe) { | ||
| const direction = swiped === 'left' ? 'left' : 'right'; | ||
| this.setData({ | ||
| swipeX: (maxSwipe + 0.01) * (direction === 'right' ? -1 : 1), | ||
| swipedR: direction === 'right', | ||
| swipedL: direction === 'left', | ||
| tapTypeL: '', | ||
| tapTypeR: '', | ||
| }); | ||
| } | ||
| }); | ||
| }, 50); | ||
|
Comment on lines
+144
to
+157
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the implementation in |
||
| } | ||
| } else if (is || ds) { | ||
| // 只有 elasticity 或 damping 变化时,才需要重置 | ||
| this.setData({ | ||
| swipeX: 0, | ||
| swipedR: false, | ||
|
|
@@ -124,6 +166,7 @@ Component( | |
| tapTypeR: '', | ||
| }); | ||
| } | ||
|
|
||
| if (is) { | ||
| this.setData({ inertiaWidth: elasticity ? 20 : 0 }); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
maxSwipeparameter is typed asany. For better type safety and code clarity, it should be explicitly typed asnumber, since it represents a width value obtained frominitWidth.