-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
416 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,8 @@ | |
"ivy-table-column", | ||
"ivy-drawer", | ||
"ivy-tooltip", | ||
"ivy-details" | ||
"ivy-details", | ||
"ivy-tree" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Tree 树形控件 | ||
|
||
## 基础用法 | ||
|
||
<ivy-tree ref="el1" node-key="id"></ivy-tree> | ||
|
||
<script setup> | ||
import { onMounted, ref } from 'vue' | ||
|
||
const data = [ | ||
{ | ||
id: 1, | ||
label: 'Level one 1', | ||
children: [ | ||
{ | ||
id: 4, | ||
label: 'Level two 1-1', | ||
children: [ | ||
{ | ||
id: 9, | ||
label: 'Level three 1-1-1', | ||
}, | ||
{ | ||
id: 10, | ||
label: 'Level three 1-1-2', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 2, | ||
label: 'Level one 2', | ||
children: [ | ||
{ | ||
id: 5, | ||
label: 'Level two 2-1', | ||
}, | ||
{ | ||
id: 6, | ||
label: 'Level two 2-2', | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 3, | ||
label: 'Level one 3', | ||
children: [ | ||
{ | ||
id: 7, | ||
label: 'Level two 3-1', | ||
}, | ||
{ | ||
id: 8, | ||
label: 'Level two 3-2', | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 9, | ||
label: 'Level two 4', | ||
}, | ||
] | ||
|
||
const el1 = ref() | ||
const setElData = (el, data) => { | ||
el.value.setData(data) | ||
} | ||
onMounted(()=>{ | ||
setElData(el1, data) | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { type SetupContext } from 'vue' | ||
import CollapseTransition from '@/utils/collapse-transition' | ||
import { CaretRight } from '@/utils/icons' | ||
|
||
const TreeItem = (props: Record<string, any>, { emit }: SetupContext) => { | ||
const { data, props: childProps } = props | ||
|
||
const handleClick = () => { | ||
data.isOpen = !data.isOpen | ||
emit('item-click', data) | ||
} | ||
return ( | ||
<div class="tree-item"> | ||
<div class="tree-item__label" onClick={handleClick}> | ||
{data.isLeaf ? ( | ||
<span class="tree-item__icon"> </span> | ||
) : ( | ||
<CaretRight | ||
class="tree-item__icon" | ||
style={{ transform: `rotate(${data.isOpen ? 90 : 0}deg)` }} | ||
></CaretRight> | ||
)} | ||
<span>{data[childProps.label]}</span> | ||
</div> | ||
{data.isLeaf ? null : ( | ||
<CollapseTransition> | ||
<div class="tree-item__children" v-show={data.isOpen}> | ||
{data[childProps.children].map((c: any) => ( | ||
<TreeItem data={c} props={childProps}></TreeItem> | ||
))} | ||
</div> | ||
</CollapseTransition> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
TreeItem.emits = ['item-click'] | ||
|
||
export { TreeItem } |
107 changes: 107 additions & 0 deletions
107
packages/ivy-design-wc/src/components/tree/index.ce.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<script setup lang="ts"> | ||
import { onMounted, ref } from 'vue' | ||
import { TreeItem } from './child' | ||
import useExpose from '@/hooks/useExpose' | ||
defineOptions({ | ||
name: 'Tree', | ||
inheritAttrs: false | ||
}) | ||
const props = defineProps({ | ||
nodeKey: String, | ||
propLabel: { | ||
type: String, | ||
default: 'label' | ||
}, | ||
propChildren: { | ||
type: String, | ||
default: 'children' | ||
} | ||
}) | ||
const keyMap = ref({ | ||
label: props.propLabel, | ||
children: props.propChildren | ||
}) // key 映射 | ||
type DataSources = Array<{ | ||
[key: string]: any | ||
}> | ||
const dataSources = ref<DataSources>([]) // 数据源 | ||
// const checkedKeys = ref([]) // 选中的 key | ||
const parseData = (data: DataSources) => { | ||
return data.map((item) => { | ||
const { [props.propChildren]: children } = item | ||
if (children) { | ||
item.isOpen = false | ||
item.isLeaf = false | ||
item.children = parseData(children) | ||
} else { | ||
item.isLeaf = true | ||
} | ||
return item | ||
}) | ||
} | ||
const { setExposes } = useExpose() | ||
onMounted(() => { | ||
setExposes({ | ||
setCheckedKeys: (keys: string[]) => { | ||
console.log(keys) | ||
}, | ||
setData(data: DataSources) { | ||
dataSources.value = parseData(data) | ||
} | ||
}) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="tree"> | ||
<template | ||
v-for="(item, index) in dataSources" | ||
:key="props.nodeKey ? item[props.nodeKey] : index" | ||
> | ||
<TreeItem :data="item" :props="keyMap" /> | ||
</template> | ||
</div> | ||
</template> | ||
|
||
<style lang="scss"> | ||
:host { | ||
display: block; | ||
--ivy-tree-font-size: 14px; | ||
--ivy-tree-line-height: 1.5; | ||
} | ||
.tree { | ||
font-size: var(--ivy-tree-font-size); | ||
line-height: var(--ivy-tree-line-height); | ||
&-item { | ||
cursor: default; | ||
&__label { | ||
display: flex; | ||
align-items: center; | ||
} | ||
&__icon { | ||
display: inline-flex; | ||
font-size: 14px; | ||
width: 1em; | ||
height: 1em; | ||
transition: transform 0.3s; | ||
} | ||
&__children { | ||
padding-left: 20px; | ||
box-sizing: border-box; | ||
} | ||
} | ||
} | ||
.collapse-transition { | ||
transition: 0.3s height ease-in-out, 0.3s padding-top ease-in-out, | ||
0.3s padding-bottom ease-in-out; | ||
} | ||
</style> |
Oops, something went wrong.