Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-level nesting issue with "v-for" #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions src/components/GGanttChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@
<script setup lang="ts">
import {
computed,
isVNode,
provide,
ref,
toRefs,
useSlots,
type ComputedRef,
type Ref,
type ToRefs
type ToRefs,
type VNode
} from "vue"

import GGanttGrid from "./GGanttGrid.vue"
Expand Down Expand Up @@ -161,30 +163,24 @@ const colors = computed(() =>
? colorScheme.value
: colorSchemes[colorScheme.value as ColorSchemeKey] || colorSchemes.default
)
const getChartRows = () => {
function getChartRows() {
const defaultSlot = slots.default?.()
const allBars: ChartRow[] = []

if (!defaultSlot) {
return allBars
if (!defaultSlot) return allBars

function getBars(children: VNode[]) {
children.forEach((child) => {
if (child.props?.bars) {
const { label, bars } = child.props
allBars.push({ label, bars })
} else if (Array.isArray(child.children)) {
getBars(child.children.filter(isVNode))
}
})
}
defaultSlot.forEach((child) => {
if (child.props?.bars) {
const { label, bars } = child.props
allBars.push({ label, bars })
// if using v-for to generate rows, rows will be children of a single "fragment" v-node:
} else if (Array.isArray(child.children)) {
child.children.forEach((grandchild) => {
const granchildNode = grandchild as {
props?: ChartRow
}
if (granchildNode?.props?.bars) {
const { label, bars } = granchildNode.props
allBars.push({ label, bars })
}
})
}
})

getBars(defaultSlot)
return allBars
}

Expand Down