|
1 | | -import { Component, JSXNode, PropsOf, QRL, Signal } from '@builder.io/qwik'; |
2 | | -import { HAccordionRootImpl } from './accordion-root'; |
| 1 | +import { Component } from '@builder.io/qwik'; |
| 2 | +import { type AccordionRootProps, HAccordionRootImpl } from './accordion-root'; |
3 | 3 | import { Accordion } from '@qwik-ui/headless'; |
| 4 | +import { findComponent, processChildren } from '../../utils/inline-component'; |
4 | 5 |
|
5 | 6 | type InternalProps = { |
6 | 7 | accordionItemComponent?: typeof Accordion.Item; |
7 | 8 | }; |
8 | 9 |
|
9 | | -export type AccordionRootProps = PropsOf<'div'> & { |
10 | | - /** If true, multiple items can be open at the same time. */ |
11 | | - multiple?: boolean; |
12 | | - |
13 | | - /** |
14 | | - * @deprecated Use the multiple prop instead. |
15 | | - */ |
16 | | - behavior?: 'single' | 'multi'; |
17 | | - |
18 | | - /** The reactive value controlling which item is open. */ |
19 | | - 'bind:value'?: Signal<string | null>; |
20 | | - |
21 | | - /** The initial value of the currently open item. */ |
22 | | - value?: string; |
23 | | - |
24 | | - /** The initial index of the currently open item. */ |
25 | | - initialIndex?: number; |
26 | | - |
27 | | - /** A QRL that is called when the selected item changes. */ |
28 | | - onChange$?: QRL<(value: string) => void>; |
29 | | - |
30 | | - /** A map of the item indexes and their disabled state. */ |
31 | | - itemsMap?: Map<number, boolean>; |
32 | | - |
33 | | - /** If true, the accordion is disabled. */ |
34 | | - disabled?: boolean; |
35 | | - |
36 | | - /** If true, the accordion is collapsible. */ |
37 | | - collapsible?: boolean; |
38 | | - |
39 | | - /** If true, the accordion is animated. */ |
40 | | - animated?: boolean; |
41 | | -}; |
42 | | - |
43 | 10 | export const HAccordionRoot: Component<AccordionRootProps & InternalProps> = ( |
44 | 11 | props: AccordionRootProps & InternalProps, |
45 | 12 | ) => { |
46 | | - const { children: accordionChildren, accordionItemComponent, ...rest } = props; |
47 | | - |
| 13 | + const { |
| 14 | + children, |
| 15 | + accordionItemComponent: GivenItem, |
| 16 | + value: initialValue, |
| 17 | + ...rest |
| 18 | + } = props; |
| 19 | + const Item = GivenItem || Accordion.Item; |
48 | 20 | let currItemIndex = 0; |
49 | 21 | let initialIndex = null; |
50 | 22 | const itemsMap = new Map(); |
51 | 23 |
|
52 | | - const InternalItemComponent = accordionItemComponent || Accordion.Item; |
53 | | - const childrenToProcess = ( |
54 | | - Array.isArray(accordionChildren) ? [...accordionChildren] : [accordionChildren] |
55 | | - ) as Array<JSXNode>; |
56 | | - |
57 | | - while (childrenToProcess.length) { |
58 | | - const child = childrenToProcess.shift(); |
| 24 | + // code executes when the item component's shell is "seen" |
| 25 | + findComponent(Item, (itemProps) => { |
| 26 | + itemProps._index = currItemIndex; |
59 | 27 |
|
60 | | - if (!child) { |
61 | | - continue; |
62 | | - } |
| 28 | + itemsMap.set(currItemIndex, itemProps.disabled); |
63 | 29 |
|
64 | | - if (Array.isArray(child)) { |
65 | | - childrenToProcess.unshift(...child); |
66 | | - continue; |
| 30 | + if (initialValue && initialValue === itemProps.value) { |
| 31 | + initialIndex = currItemIndex; |
67 | 32 | } |
68 | 33 |
|
69 | | - switch (child.type) { |
70 | | - case InternalItemComponent: { |
71 | | - child.props._index = currItemIndex; |
72 | | - if (props.value !== undefined && props.value === child.props.value) { |
73 | | - initialIndex = currItemIndex; |
74 | | - } |
75 | | - itemsMap.set(currItemIndex, child.props.disabled === true); |
76 | | - |
77 | | - currItemIndex++; |
78 | | - break; |
79 | | - } |
| 34 | + currItemIndex++; |
| 35 | + }); |
80 | 36 |
|
81 | | - default: { |
82 | | - if (child) { |
83 | | - const anyChildren = Array.isArray(child.children) |
84 | | - ? [...child.children] |
85 | | - : [child.children]; |
86 | | - childrenToProcess.unshift(...(anyChildren as JSXNode[])); |
87 | | - } |
88 | | - |
89 | | - break; |
90 | | - } |
91 | | - } |
92 | | - } |
| 37 | + processChildren(children); |
93 | 38 |
|
94 | 39 | return ( |
95 | 40 | <HAccordionRootImpl |
|
0 commit comments