Skip to content

Commit

Permalink
refactor(components): [tree-v2] use useNamespace (element-plus#5757)
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan authored Feb 10, 2022
1 parent ac4b582 commit 95608a8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
4 changes: 3 additions & 1 deletion packages/components/tree-v2/src/tree-node-content.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { h, defineComponent, inject } from 'vue'
import { useNamespace } from '@element-plus/hooks'
import { ROOT_TREE_INJECTION_KEY, treeNodeContentProps } from './virtual-tree'

export default defineComponent({
name: 'ElTreeNodeContent',
props: treeNodeContentProps,
setup(props) {
const tree = inject(ROOT_TREE_INJECTION_KEY)
const ns = useNamespace('tree')
return () => {
const node = props.node
const { data } = node!
return tree?.ctx.slots.default
? tree.ctx.slots.default({ node, data })
: h('span', { class: 'el-tree-node__label' }, [node?.label])
: h('span', { class: ns.be('node', 'label') }, [node?.label])
}
},
})
25 changes: 14 additions & 11 deletions packages/components/tree-v2/src/tree-node.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<template>
<div
ref="node$"
class="el-tree-node"
:class="{
'is-expanded': expanded,
'is-current': current,
'is-focusable': !disabled,
'is-checked': !disabled && checked,
}"
:class="[
ns.b('node'),
ns.is('expanded', expanded),
ns.is('current', current),
ns.is('focusable', !disabled),
ns.is('checked', !disabled && checked),
]"
role="treeitem"
tabindex="-1"
:aria-expanded="expanded"
Expand All @@ -18,18 +18,18 @@
@contextmenu="handleContextMenu"
>
<div
class="el-tree-node__content"
:class="ns.be('node', 'content')"
:style="{ paddingLeft: `${(node.level - 1) * indent}px` }"
>
<el-icon
v-if="icon"
:class="[
ns.is('leaf', !!node?.isLeaf),
ns.is('hidden', hiddenExpandIcon),
{
'is-leaf': node?.isLeaf,
'is-hidden': hiddenExpandIcon,
expanded: !node?.isLeaf && expanded,
},
'el-tree-node__expand-icon',
ns.be('node', 'expand-icon'),
]"
@click.stop="handleExpandIconClick"
>
Expand All @@ -53,6 +53,7 @@ import { computed, defineComponent, inject } from 'vue'
import { CaretRight } from '@element-plus/icons-vue'
import ElIcon from '@element-plus/components/icon'
import ElCheckbox from '@element-plus/components/checkbox'
import { useNamespace } from '@element-plus/hooks'
import ElNodeContent from './tree-node-content'
import {
ROOT_TREE_INJECTION_KEY,
Expand All @@ -75,6 +76,7 @@ export default defineComponent({
emits: treeNodeEmits,
setup(props, { emit }) {
const tree = inject(ROOT_TREE_INJECTION_KEY)
const ns = useNamespace('tree')
const indent = computed(() => {
return tree?.props.indent ?? 16
Expand Down Expand Up @@ -102,6 +104,7 @@ export default defineComponent({
}
return {
ns,
indent,
icon,
handleClick,
Expand Down
15 changes: 7 additions & 8 deletions packages/components/tree-v2/src/tree.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<template>
<div
class="el-tree"
:class="{
'el-tree--highlight-current': highlightCurrent,
}"
:class="[ns.b(), { [ns.m('highlight-current')]: highlightCurrent }]"
role="tree"
>
<fixed-size-list
v-if="isNotEmpty"
class-name="el-tree-virtual-list"
:class-name="ns.b('virtual-list')"
:data="flattenTree"
:total="flattenTree.length"
:height="height"
Expand All @@ -33,8 +30,8 @@
></el-tree-node>
</template>
</fixed-size-list>
<div v-else class="el-tree__empty-block">
<span class="el-tree__empty-text">{{
<div v-else :class="ns.e('empty-block')">
<span :class="ns.e('empty-text')">{{
emptyText ?? t('el.tree.emptyText')
}}</span>
</div>
Expand All @@ -43,7 +40,7 @@

<script lang="ts">
import { defineComponent, getCurrentInstance, provide } from 'vue'
import { useLocale } from '@element-plus/hooks'
import { useLocale, useNamespace } from '@element-plus/hooks'
import { FixedSizeList } from '@element-plus/components/virtual-list'
import { useTree } from './composables/useTree'
import ElTreeNode from './tree-node.vue'
Expand All @@ -65,6 +62,7 @@ export default defineComponent({
instance: getCurrentInstance(),
})
const { t } = useLocale()
const ns = useNamespace('tree')
const {
flattenTree,
isNotEmpty,
Expand Down Expand Up @@ -108,6 +106,7 @@ export default defineComponent({
return {
t,
ns,
flattenTree,
itemSize: 26,
isNotEmpty,
Expand Down

0 comments on commit 95608a8

Please sign in to comment.