Skip to content

Commit cb7cbf3

Browse files
authored
feat: ✨ Tabs 新增 autoLineWidth 设置底部条宽度自动同步文本内容' (#679)
1 parent 1e039cb commit cb7cbf3

4 files changed

Lines changed: 53 additions & 7 deletions

File tree

docs/component/tabs.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ const tab = ref('例子')
5757
}
5858
```
5959

60+
## 自动调整底部条宽度
61+
62+
设置 `auto-line-width` 属性,自动调整底部条宽度为文本内容宽度。
63+
64+
```html
65+
<wd-tabs v-model="tab" @change="handleChange" auto-line-width>
66+
<block v-for="item in tabs" :key="item">
67+
<wd-tab :title="`${item}`" :name="item">
68+
<view class="content">内容{{ tab }}</view>
69+
</wd-tab>
70+
</block>
71+
</wd-tabs>
72+
```
73+
74+
```typescript
75+
const tabs = ref(['Wot', 'Design', 'Uni'])
76+
const tab = ref('Design')
77+
```
78+
6079
## 粘性布局
6180

6281
设置 `sticky` 属性,使用粘性布局。可以设置 `offset-top` 属性,当距离窗口顶部多少像素时,固定标签头。在`H5`端使用自定义导航栏时需要参考[sticky 的吸顶距离](/component/sticky.html#吸顶距离)进行配置。
@@ -156,6 +175,7 @@ const tab = ref('例子')
156175
| sticky | 粘性布局 | boolean | - | false | - |
157176
| offset-top | 粘性布局时距离窗口顶部距离 | number | - | 0 | - |
158177
| swipeable | 开启手势滑动 | boolean | - | false | - |
178+
| autoLineWidth | 自动调整底部条宽度 | boolean | - | false | - |
159179
| lineWidth | 底部条宽度,单位像素 | number | - | 19 | - |
160180
| lineHeight | 底部条高度,单位像素 | number | - | 3 | - |
161181
| color | 文字颜色 | string | - | - | - |

src/pages/tabs/Index.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
</wd-tabs>
2424
</demo-block>
2525

26+
<demo-block title="自动调整底部条宽度" transparent>
27+
<wd-tabs v-model="autoLineWidthTab" @change="handleChange" auto-line-width>
28+
<block v-for="item in autoLineWidthTabs" :key="item">
29+
<wd-tab :title="`${item}`" :name="item">
30+
<view class="content">内容{{ autoLineWidthTab }}</view>
31+
</wd-tab>
32+
</block>
33+
</wd-tabs>
34+
</demo-block>
35+
2636
<demo-block title="粘性布局" transparent>
2737
<wd-tabs v-model="tab2" sticky @change="handleChange">
2838
<block v-for="item in 4" :key="item">
@@ -110,6 +120,9 @@ import { ref } from 'vue'
110120
const tabs = ref(['', '', '', '', '例子'])
111121
const tab = ref('')
112122
123+
const autoLineWidthTabs = ref(['Wot', 'Design', 'Uni'])
124+
const autoLineWidthTab = ref('Design')
125+
113126
const tab1 = ref<number>(0)
114127
const tab2 = ref<number>(0)
115128
const tab3 = ref<number>(1)

src/uni_modules/wot-design-uni/components/wd-tabs/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export const tabsProps = {
4343
* 开启手势滑动
4444
*/
4545
swipeable: makeBooleanProp(false),
46+
/**
47+
* 自动调整底部条宽度,设置了 lineWidth 后无效
48+
*/
49+
autoLineWidth: makeBooleanProp(false),
4650
/**
4751
* 底部条宽度,单位像素
4852
*/

src/uni_modules/wot-design-uni/components/wd-tabs/wd-tabs.vue

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
:class="`wd-tabs__nav-item ${state.activeIndex === index ? 'is-active' : ''} ${item.disabled ? 'is-disabled' : ''}`"
2020
:style="state.activeIndex === index ? (color ? 'color:' + color : '') : inactiveColor ? 'color:' + inactiveColor : ''"
2121
>
22-
{{ item.title }}
22+
<text class="wd-tabs__nav-item-text">{{ item.title }}</text>
2323
<view class="wd-tabs__line wd-tabs__line--inner" v-if="state.activeIndex === index && state.useInnerLine"></view>
2424
</view>
2525
<!--下划线-->
@@ -91,7 +91,7 @@
9191
:class="`wd-tabs__nav-item ${state.activeIndex === index ? 'is-active' : ''} ${item.disabled ? 'is-disabled' : ''}`"
9292
:style="state.activeIndex === index ? (color ? 'color:' + color : '') : inactiveColor ? 'color:' + inactiveColor : ''"
9393
>
94-
{{ item.title }}
94+
<text class="wd-tabs__nav-item-text">{{ item.title }}</text>
9595
<view class="wd-tabs__line wd-tabs__line--inner" v-if="state.activeIndex === index && state.useInnerLine"></view>
9696
</view>
9797
<!--下划线-->
@@ -153,6 +153,7 @@ import { useChildren } from '../composables/useChildren'
153153
import { useTranslate } from '../composables/useTranslate'
154154
155155
const $item = '.wd-tabs__nav-item'
156+
const $itemText = '.wd-tabs__nav-item-text'
156157
const $container = '.wd-tabs__nav-container'
157158
158159
const props = defineProps(tabsProps)
@@ -319,14 +320,20 @@ function toggleMap() {
319320
* 更新 underline的偏移量
320321
* @param animation 是否开启动画
321322
*/
322-
function updateLineStyle(animation: boolean = true) {
323+
async function updateLineStyle(animation: boolean = true) {
323324
if (!state.inited) return
324-
const { lineWidth, lineHeight } = props
325-
326-
getRect($item, true, proxy).then((rects) => {
325+
const { autoLineWidth, lineWidth, lineHeight } = props
326+
try {
327+
const rects = await getRect($item, true, proxy)
327328
const lineStyle: CSSProperties = {}
328329
if (isDef(lineWidth)) {
329330
lineStyle.width = addUnit(lineWidth)
331+
} else {
332+
if (autoLineWidth) {
333+
const textRects = await getRect($itemText, true, proxy)
334+
const textWidth = Number(textRects[state.activeIndex].width)
335+
lineStyle.width = addUnit(textWidth)
336+
}
330337
}
331338
if (isDef(lineHeight)) {
332339
lineStyle.height = addUnit(lineHeight)
@@ -342,7 +349,9 @@ function updateLineStyle(animation: boolean = true) {
342349
state.useInnerLine = false
343350
state.lineStyle = objToStyle(lineStyle)
344351
}
345-
})
352+
} catch (error) {
353+
console.error('[wot design] error(wd-tabs): update line style failed', error)
354+
}
346355
}
347356
/**
348357
* @description 通过控制tab的active来展示选定的tab

0 commit comments

Comments
 (0)