Skip to content

Commit

Permalink
[vue]: refactor accordion
Browse files Browse the repository at this point in the history
  • Loading branch information
arpansaha13 committed Oct 7, 2023
1 parent 3d18336 commit 735de61
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions vue/src/machine-coding/accordion/AccordionGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</div>
</template>

<!-- The AccordionGroup handles the open state of all accordions under it -->

<script setup lang="ts">
import { type PropType, reactive, watch, ref } from 'vue'
import Accordion from './Accordion.vue'
Expand All @@ -24,16 +26,19 @@ const props = defineProps({
type: Array as PropType<AccordionData[]>,
required: true,
},
/** Whether multiple accordions can be open at the same time or not */
allowMultipleOpen: {
type: Boolean,
default: true,
}
})

const open = reactive(Array(props.data.length).fill(false))
const prev = ref(null)

function update(val, i) {
/** Record the index of the previous accordion that was opened/closed */
const prev = ref<number | null>(null)

function update(val: boolean, i: number) {
if (!props.allowMultipleOpen && prev.value !== null) {
open[prev.value] = false
}
Expand All @@ -42,6 +47,7 @@ function update(val, i) {
}

watch(props, (newProps) => {
// If `allowMultipleOpen` is toggled, then close all accordions except the immediate previous one
if (!newProps.allowMultipleOpen) {
open.fill(false)
if (prev.value) open[prev.value] = true
Expand Down

0 comments on commit 735de61

Please sign in to comment.