Last active
March 24, 2022 13:27
-
-
Save techbysalma/8b177650a863e5359bf85515cf10a2d1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <template> | |
| <div> | |
| <span>{{ branch.label }}</span> | |
| <span | |
| v-show="hasChildren" | |
| > | |
| 🌠 | |
| </span> | |
| <!-- add toggle function to show or hide children --> | |
| <!-- try to use a flat array --> | |
| <!-- add and delete children --> | |
| </div> | |
| <div | |
| v-if="hasChildren" | |
| :style="{ marginLeft: `${addSpacing}px`}" | |
| > | |
| <Tree | |
| v-for="child in branch.children" | |
| :key="child.id" | |
| :branch="child" | |
| :spacing="addSpacing" | |
| /> | |
| </div> | |
| </template> | |
| <script> | |
| import { computed, onMounted, getCurrentInstance } from 'vue' | |
| export default { | |
| name: 'Tree', | |
| props: { | |
| branch: { | |
| type: Object, | |
| required: true | |
| }, | |
| spacing: { | |
| type: Number, | |
| default: 0 | |
| } | |
| }, | |
| setup(props) { | |
| const hasChildren = computed(() => props.branch?.children?.length > 0) | |
| const addSpacing = computed(() => props.spacing + 10 ) | |
| return { | |
| hasChildren, | |
| addSpacing | |
| } | |
| } | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment