diff --git a/packages/components/fab/README.md b/packages/components/fab/README.md
index 593bc211ab..1375034777 100644
--- a/packages/components/fab/README.md
+++ b/packages/components/fab/README.md
@@ -46,6 +46,8 @@ isComponent: true
{{ collapsible }}
+该示例会在页面空闲约 3 秒后自动收缩;点击收缩状态的箭头即可展开,并重新开始计时。页面滚动时会重置收缩计时。
+
## FAQ
### 为什么通过 style/customStyle 设置 top/left 调整初试定位后,会使页面内容无法点击以及拖拽异常?
diff --git a/packages/components/fab/__test__/demo.test.js b/packages/components/fab/__test__/demo.test.js
index f742538c61..b4ae9c0700 100644
--- a/packages/components/fab/__test__/demo.test.js
+++ b/packages/components/fab/__test__/demo.test.js
@@ -14,6 +14,22 @@ describe('Fab', () => {
const container = simulate.render(id);
container.attach(document.createElement('parent-wrapper'));
expect(container.toJSON()).toMatchSnapshot();
+
+ if (demoName !== 'collapsible') return;
+
+ expect(container.data.collapsed).toBe(false);
+
+ container.instance.clearCollapseTimer();
+ jest.useFakeTimers();
+ container.instance.startCollapseTimer();
+ jest.advanceTimersByTime(3000);
+ expect(container.data.collapsed).toBe(true);
+
+ container.instance.onExpand();
+ expect(container.data.collapsed).toBe(false);
+
+ jest.clearAllTimers();
+ jest.useRealTimers();
});
});
});
diff --git a/packages/components/fab/_example/collapsible/index.js b/packages/components/fab/_example/collapsible/index.js
index 1cb8e867cf..8bb2e17375 100644
--- a/packages/components/fab/_example/collapsible/index.js
+++ b/packages/components/fab/_example/collapsible/index.js
@@ -1,10 +1,19 @@
import pageScrollMixin from 'tdesign-miniprogram/mixins/page-scroll';
+const COLLAPSE_DELAY = 3000;
+
Component({
behaviors: [pageScrollMixin()],
data: {
- scrolling: false,
- timer: null,
+ collapsed: false,
+ },
+ lifetimes: {
+ attached() {
+ this.startCollapseTimer();
+ },
+ detached() {
+ this.clearCollapseTimer();
+ },
},
methods: {
handleClick(e) {
@@ -17,15 +26,24 @@ Component({
console.log('handleDragEnd: ', e);
},
onScroll() {
+ this.startCollapseTimer();
+ },
+ onExpand() {
+ if (!this.data.collapsed) return;
+ this.setData({ collapsed: false });
+ this.startCollapseTimer();
+ },
+ clearCollapseTimer() {
+ if (!this.timer) return;
clearTimeout(this.timer);
- this.setData({
- scrolling: true,
- });
+ this.timer = null;
+ },
+ startCollapseTimer() {
+ this.clearCollapseTimer();
this.timer = setTimeout(() => {
- this.setData({
- scrolling: false,
- });
- }, 100);
+ this.setData({ collapsed: true });
+ this.timer = null;
+ }, COLLAPSE_DELAY);
},
},
});
diff --git a/packages/components/fab/_example/collapsible/index.wxml b/packages/components/fab/_example/collapsible/index.wxml
index a08997add1..8877e47975 100644
--- a/packages/components/fab/_example/collapsible/index.wxml
+++ b/packages/components/fab/_example/collapsible/index.wxml
@@ -1,10 +1,11 @@
-
+
添加
@@ -18,7 +19,8 @@
分享
-
-
-
+
+
+
+
diff --git a/packages/components/fab/_example/collapsible/index.wxss b/packages/components/fab/_example/collapsible/index.wxss
index a9eba36271..377c0b4c76 100644
--- a/packages/components/fab/_example/collapsible/index.wxss
+++ b/packages/components/fab/_example/collapsible/index.wxss
@@ -38,6 +38,9 @@
}
.symbol {
+ position: fixed;
+ right: 0;
+ bottom: 64px;
width: 32px;
height: 32px;
display: flex;