Skip to content

Commit bb5b193

Browse files
feat: ✨ Tab 添加 lazy 属性支持配置是否开启懒加载
Closes: #641
1 parent fad777d commit bb5b193

3 files changed

Lines changed: 21 additions & 36 deletions

File tree

docs/component/tabs.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ const tab = ref('Design')
190190
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
191191
| -------- | ---------- | ------- | ------ | ------ | -------- |
192192
| name | 标签页名称 | string | - | - | - |
193-
| title | 标题 | string | - | - | - |
194-
| disabled | 禁用 | boolean | - | false | - |
193+
| title | 标题 | string | - | - | - |
194+
| disabled | 禁用 | boolean | - | false | - |
195+
| lazy | 延迟渲染,默认开启,开启`animated`后此选项始终为`false` | boolean | - | true | $LOWEST_VERSION$ |
195196

196197
## Tabs Events
197198

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ export const tabProps = {
1414
/**
1515
* 是否禁用,无法点击
1616
*/
17-
disabled: makeBooleanProp(false)
17+
disabled: makeBooleanProp(false),
18+
/**
19+
* 是否懒加载,切换到该tab时才加载内容
20+
* @default true
21+
*/
22+
lazy: makeBooleanProp(true)
1823
}
1924

2025
export type TabProps = ExtractPropTypes<typeof tabProps>

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

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<view :class="`wd-tab ${customClass}`" :style="customStyle">
3-
<view v-if="painted" class="wd-tab__body" :style="tabBodyStyle">
3+
<view class="wd-tab__body" v-if="shouldBeRender" :style="tabBodyStyle">
44
<slot />
55
</view>
66
</view>
@@ -25,24 +25,30 @@ import { tabProps } from './types'
2525
2626
const props = defineProps(tabProps)
2727
28-
const painted = ref<boolean>(false) // 初始状态tab不会渲染,必须通过tabs来设置painted使tab渲染
29-
const isShow = ref<boolean>(false)
3028
const { proxy } = getCurrentInstance() as any
3129
const { parent: tabs, index } = useParent(TABS_KEY)
3230
3331
// 激活项下标
34-
const activeIndex = computed(() => {
35-
return isDef(tabs) ? tabs.state.activeIndex : 0
32+
const active = computed(() => {
33+
return isDef(tabs) ? tabs.state.activeIndex === index.value : false
3634
})
3735
36+
const painted = ref<boolean>(active.value) // 初始状态tab不会渲染,必须通过tabs来设置painted使tab渲染
37+
3838
const tabBodyStyle = computed(() => {
3939
const style: CSSProperties = {}
40-
if (!isShow.value && (!isDef(tabs) || !tabs.props.animated)) {
40+
if (!active.value && (!isDef(tabs) || !tabs.props.animated)) {
4141
style.display = 'none'
4242
}
4343
return objToStyle(style)
4444
})
4545
46+
const shouldBeRender = computed(() => !props.lazy || painted.value || active.value)
47+
48+
watch(active, (val) => {
49+
if (val) painted.value = true
50+
})
51+
4652
watch(
4753
() => props.name,
4854
(newValue) => {
@@ -60,18 +66,6 @@ watch(
6066
}
6167
)
6268
63-
watch(
64-
() => activeIndex.value,
65-
(newValue) => {
66-
if (newValue === index.value) {
67-
setShow(true, true)
68-
} else {
69-
setShow(painted.value, false)
70-
}
71-
},
72-
{ deep: true, immediate: true }
73-
)
74-
7569
/**
7670
* @description 检测tab绑定的name是否和其它tab的name冲突
7771
* @param {Object} self 自身
@@ -88,21 +82,6 @@ function checkName(self: any) {
8882
}
8983
})
9084
}
91-
92-
/**
93-
* 设置子组件展示
94-
* @param setPainted
95-
* @param setIsShow
96-
*/
97-
function setShow(setPainted: boolean, setIsShow: boolean) {
98-
painted.value = setPainted
99-
isShow.value = setIsShow
100-
}
101-
102-
defineExpose({
103-
setShow,
104-
painted
105-
})
10685
</script>
10786
<style lang="scss" scoped>
10887
@import './index.scss';

0 commit comments

Comments
 (0)