feat: Tab 添加 lazy 属性支持配置是否开启懒加载

 Closes: #641
This commit is contained in:
不如摸鱼去 2024-11-19 22:26:19 +08:00
parent fad777dfa8
commit bb5b19325f
3 changed files with 21 additions and 36 deletions

View File

@ -190,8 +190,9 @@ const tab = ref('Design')
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 | | 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
| -------- | ---------- | ------- | ------ | ------ | -------- | | -------- | ---------- | ------- | ------ | ------ | -------- |
| name | 标签页名称 | string | - | - | - | | name | 标签页名称 | string | - | - | - |
| title | 标题 | string | - | - | - | | title | 标题 | string | - | - | - |
| disabled | 禁用 | boolean | - | false | - | | disabled | 禁用 | boolean | - | false | - |
| lazy | 延迟渲染,默认开启,开启`animated`后此选项始终为`false` | boolean | - | true | $LOWEST_VERSION$ |
## Tabs Events ## Tabs Events

View File

@ -14,7 +14,12 @@ export const tabProps = {
/** /**
* *
*/ */
disabled: makeBooleanProp(false) disabled: makeBooleanProp(false),
/**
* tab时才加载内容
* @default true
*/
lazy: makeBooleanProp(true)
} }
export type TabProps = ExtractPropTypes<typeof tabProps> export type TabProps = ExtractPropTypes<typeof tabProps>

View File

@ -1,6 +1,6 @@
<template> <template>
<view :class="`wd-tab ${customClass}`" :style="customStyle"> <view :class="`wd-tab ${customClass}`" :style="customStyle">
<view v-if="painted" class="wd-tab__body" :style="tabBodyStyle"> <view class="wd-tab__body" v-if="shouldBeRender" :style="tabBodyStyle">
<slot /> <slot />
</view> </view>
</view> </view>
@ -25,24 +25,30 @@ import { tabProps } from './types'
const props = defineProps(tabProps) const props = defineProps(tabProps)
const painted = ref<boolean>(false) // tabtabspainted使tab
const isShow = ref<boolean>(false)
const { proxy } = getCurrentInstance() as any const { proxy } = getCurrentInstance() as any
const { parent: tabs, index } = useParent(TABS_KEY) const { parent: tabs, index } = useParent(TABS_KEY)
// //
const activeIndex = computed(() => { const active = computed(() => {
return isDef(tabs) ? tabs.state.activeIndex : 0 return isDef(tabs) ? tabs.state.activeIndex === index.value : false
}) })
const painted = ref<boolean>(active.value) // tabtabspainted使tab
const tabBodyStyle = computed(() => { const tabBodyStyle = computed(() => {
const style: CSSProperties = {} const style: CSSProperties = {}
if (!isShow.value && (!isDef(tabs) || !tabs.props.animated)) { if (!active.value && (!isDef(tabs) || !tabs.props.animated)) {
style.display = 'none' style.display = 'none'
} }
return objToStyle(style) return objToStyle(style)
}) })
const shouldBeRender = computed(() => !props.lazy || painted.value || active.value)
watch(active, (val) => {
if (val) painted.value = true
})
watch( watch(
() => props.name, () => props.name,
(newValue) => { (newValue) => {
@ -60,18 +66,6 @@ watch(
} }
) )
watch(
() => activeIndex.value,
(newValue) => {
if (newValue === index.value) {
setShow(true, true)
} else {
setShow(painted.value, false)
}
},
{ deep: true, immediate: true }
)
/** /**
* @description 检测tab绑定的name是否和其它tab的name冲突 * @description 检测tab绑定的name是否和其它tab的name冲突
* @param {Object} self 自身 * @param {Object} self 自身
@ -88,21 +82,6 @@ function checkName(self: any) {
} }
}) })
} }
/**
* 设置子组件展示
* @param setPainted
* @param setIsShow
*/
function setShow(setPainted: boolean, setIsShow: boolean) {
painted.value = setPainted
isShow.value = setIsShow
}
defineExpose({
setShow,
painted
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@import './index.scss'; @import './index.scss';