component Accordion { props { size = "default" color = "primary" variant = "default" class = "" id = "accordion" items = [] defaultOpen = [] multiple = false alwaysOpen = false disabled = false indicator = "plus" indicatorPosition = "start" showIndicator = true bordered = false separated = false flush = false contentItalic = false @event change = function @event open = function @event close = function } state openValues = defaultOpen functions { function itemValue(item, index) { return item.value !== undefined && item.value !== "" ? String(item.value) : String(index) } function nestedValue(parent, parentIndex, item, index) { return itemValue(parent, parentIndex) + "." + itemValue(item, index) } function isOpen(value) { return openValues.includes(value) } function allowsMultiple() { return multiple || alwaysOpen } function dispatchAccordionEvent(sourceEvent, eventName, value, item, root, customEvent) { root = sourceEvent.currentTarget.closest("[data-wrn-accordion]") if (!root) { return } customEvent = document.createEvent("CustomEvent") customEvent.initCustomEvent(eventName, true, false, { component: "Accordion", value: value, item: item, open: isOpen(value), openValues: openValues }) root.dispatchEvent(customEvent) } function toggleItem(sourceEvent, value, item, wasOpen) { if (disabled || item.disabled) { return } wasOpen = isOpen(value) if (wasOpen) { openValues = openValues.filter((entry) => entry !== value) } else if (allowsMultiple()) { openValues = openValues.concat([value]) } else { openValues = [value] } dispatchAccordionEvent( sourceEvent, wasOpen ? "close" : "open", value, item ) dispatchAccordionEvent(sourceEvent, "change", value, item) } } view {
{#each items as item, index}

{#if item.content}

{item.content}

{/if} {#if item.children && item.children.length > 0}
{#each item.children as child, childIndex}

{child.content}

{/each}
{/if}
{/each}
} }