Great question! You can use computed properties in Vue to create reactive, derived state based on your existing reactive data.
In your current code, here are a few places where computed could be helpful:
Example 1: Use a computed to get the releaseBuilds of the active section
Right now, you access release builds with:
v-for="releaseBuild in activeSection.releaseBuilds"
If you want, you can define a computed that returns the release builds of the currently active section, or an empty array if none is active. This can protect against null and make the template cleaner:
import { computed } from 'vue'
const activeReleaseBuilds = computed(() => {
return activeSection.value?.releaseBuilds || []
})
Then in your template:
v-for="releaseBuild in activeReleaseBuilds"
Example 2: Computed version of expanded state per section
If you want the expansion states to reset when the active section changes, you could create a computed that returns the expansion states only for the active section's release builds:
const expandedReleases = ref<boolean[]>([])
const expandedReleasesForActiveSection = computed({
get() {
if (!activeSection.value) return []
// Initialize expansion array same length as active release builds
if (expandedReleases.value.length !== activeSection.value.releaseBuilds.length) {
expandedReleases.value = activeSection.value.releaseBuilds.map(() => false)
}
return expandedReleases.value
},
set(newVal: boolean[]) {
expandedReleases.value = newVal
}
})
Then in your template, instead of expandedReleases[releaseBuild._id] (which used the global _id), you can access via index of the release build in the current array.
You would need to modify your template to get the index:
v-for="(releaseBuild, idx) in activeReleaseBuilds" :key="releaseBuild._id"
Then expansion toggle:
@click="expandedReleasesForActiveSection[idx] = !expandedReleasesForActiveSection[idx]"
And the conditions:
v-show="expandedReleasesForActiveSection[idx]"
Example 3: Using a computed for activeSection with fallback
You can also define activeSection as a normal value, and a computed getter that returns a guaranteed section, e.g.:
const selectedSectionIndex = ref(0)
const activeSection = computed(() => {
return sections[selectedSectionIndex.value] || new DownloadSection('', [])
})
And then:
<button
v-for="(section, idx) in sections"
:key="section._id"
@click="selectedSectionIndex = idx"
:class="{ active: selectedSectionIndex === idx }"
>
{{ section.name }}
</button>
<template v-for="(releaseBuild, idx) in activeSection.releaseBuilds" :key="releaseBuild._id">
...
</template>
This pattern separates your state from the data nicely.
Summary
Using computed is helpful when you want a reactive "view" or "projection" of your reactive data.
- To get
releaseBuilds of the current active section safely: use a computed.
- To handle expansion state only for currently visible builds: use a computed combined with
ref.
- To control current active section by index: use a
ref with a computed activeSection.
Here's a small snippet incorporating computed for active section and releases:
import { computed, ref } from 'vue'
const selectedSectionIndex = ref(0)
const activeSection = computed(() => sections[selectedSectionIndex.value])
const expandedReleases = ref<boolean[]>([])
// Reset expansions when activeSection changes
watch(activeSection, (newSection) => {
expandedReleases.value = newSection.releaseBuilds.map(() => false)
}, { immediate: true })
Then template binds:
<button v-for="(section, idx) in sections" :key="section._id" @click="selectedSectionIndex = idx" :class="{ active: idx === selectedSectionIndex }">
{{ section.name }}
</button>
<div v-for="(releaseBuild, idx) in activeSection.releaseBuilds" :key="releaseBuild._id">
<div @click="expandedReleases[idx] = !expandedReleases[idx]">
{{ releaseBuild.displayName }}
</div>
<div v-show="expandedReleases[idx]">
<!-- content -->
</div>
</div>
If you want, I can help refactor your entire script to use this pattern. Let me know!