Skip to content

Commit 48631f5

Browse files
committed
feat: 优化tabs 组件tab-content自适应高度
1 parent 5eadfad commit 48631f5

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

components/tabs/index.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
| onPlusClick | ‘+’icon被点击时的回调 | () => {} | | false |
1919
| onTabClick | tab 被点击的回调 | (index: Number) => void | | false |
2020
| onChange | tab变化时触发 | (index: Number) => void | | false |
21-
| swipeable | 是否可以滑动内容切换 | Boolean | true | false |
21+
| swipeable | 是否可以滑动内容切换,同时可控制高度是否自适应 | Boolean | true | false |
2222
| duration | 当swipeable为true时滑动动画时长,单位ms | Number | 500(ms) | false |
2323
| tabBarBackgroundColor | tabBar背景色 | String | | false |
2424
| tabBarActiveTextColor | tabBar激活Tab文字颜色 | String | | false |
@@ -34,6 +34,17 @@
3434
| 属性名 | 描述 | 类型 | 默认值 | 必选 |
3535
| ---- | ---- | ---- | ---- | ---- |
3636
| index | 列表项的唯一索引 | String | | | |
37+
| tabId | tab 内容序列索引 | Number | {{index}} | | |
38+
| activeTab | 选项卡当前激活序列索引 | Number | {{activeTab}} | | |
39+
40+
## tab-content 高度自适应说明
41+
42+
tabs 组件内容区域高度是否能够自适应,需要注意 `swipeable` 的值:
43+
44+
* `swipeable='{{true}}'`:内容区域可滑动,且相对应 tab 标签卡;但**内容区域高度为固定值**,需要在 acss 文件中设定 `height` 值,否则高度会异常;
45+
* `swipeable='{{false}}'`:内容区域不可滑动,此时高度表现形式有两种,且可以不需要在 acss 文件设定 `height` 值;
46+
* `<tab-content>`**** `tabId``activeTab` 两个属性,此时的高度将以所有内容区域中最高的为基准展示;
47+
* `<tab-content>`**包含** `tabId``activeTab` 两个属性时 `tabId="{{index}}" activeTab="{{activeTab}}"`,内容区域所展示的高度将会随着不同模块的高度而改变;
3748

3849
## 示例
3950

@@ -56,9 +67,16 @@
5667
onChange="handleTabChange"
5768
onPlusClick="handlePlusClick"
5869
activeTab="{{activeTab}}"
70+
swipeable="{{false}}"
5971
>
6072
<block a:for="{{tabs}}">
61-
<tab-content key="{{index}}">
73+
<tab-content key="{{index}}" tabId="{{index}}" activeTab="{{activeTab}}" a:if="{{index === 1}}">
74+
<view class="tab-content" style="height: 100px;">高度为 100px {{item.title}}</view>
75+
</tab-content>
76+
<tab-content key="{{index}}" tabId="{{index}}" activeTab="{{activeTab}}" a:elif="{{index === 2}}">
77+
<view class="tab-content" style="height: 200px;">改变 tab-content 高度为 200px {{item.title}}</view>
78+
</tab-content>
79+
<tab-content key="{{index}}" tabId="{{index}}" activeTab="{{activeTab}}" a:else>
6280
<view class="tab-content">content of {{item.title}}</view>
6381
</tab-content>
6482
</block>
@@ -108,6 +126,8 @@ Page({
108126
display: flex;
109127
justify-content: center;
110128
align-items: center;
111-
height: 300px;
129+
padding: 40rpx;
130+
/* 如果 swipeable="{{true}}",需要增加 height */
131+
/* height: 350px; */
112132
}
113133
```
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
<swiper-item style="{{style}}" class="am-tabs-pane-wrap" key="tabs-pane-{{key}}">
1+
<swiper-item a:if="{{style}}" style="{{style}}" class="am-tabs-pane-wrap" key="tabs-pane-{{key}}">
22
<slot></slot>
33
</swiper-item>
4+
<view a:else style="{{tabId !== '' && activeTab !== '' ? tabId === activeTab?'':'height: 0px;' : ''}}" class="am-tabs-pane-wrap" key="tabs-pane-{{key}}">
5+
<slot></slot>
6+
</view>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
Component({
2-
2+
props: {
3+
tabId: '',
4+
activeTab: '',
5+
},
36
});

0 commit comments

Comments
 (0)