Skip to content

Commit 969f5dc

Browse files
fix: updating correct map
1 parent 315e902 commit 969f5dc

File tree

3 files changed

+90
-89
lines changed

3 files changed

+90
-89
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { Component, JSXNode, PropsOf, QRL, Signal } from '@builder.io/qwik';
2+
import { HAccordionItem } from './accordion-item';
3+
import { HAccordionRootImpl } from './accordion-root';
4+
5+
export type AccordionRootProps = PropsOf<'div'> & {
6+
/** If true, multiple items can be open at the same time. */
7+
multiple?: boolean;
8+
9+
/** The reactive value controlling which item is open. */
10+
'bind:value'?: Signal<string | null>;
11+
12+
/** The initial value of the currently open item. */
13+
value?: string;
14+
15+
/** The initial index of the currently open item. */
16+
initialIndex?: number;
17+
18+
/** A QRL that is called when the selected item changes. */
19+
onChange$?: QRL<(value: string) => void>;
20+
21+
/** A map of the item indexes and their disabled state. */
22+
itemsMap?: Map<number, boolean>;
23+
};
24+
25+
export const HAccordionRoot: Component<AccordionRootProps> = (
26+
props: AccordionRootProps,
27+
) => {
28+
const { children: accordionChildren, ...rest } = props;
29+
30+
let currItemIndex = 0;
31+
let initialIndex = null;
32+
const itemsMap = new Map();
33+
34+
const childrenToProcess = (
35+
Array.isArray(accordionChildren) ? [...accordionChildren] : [accordionChildren]
36+
) as Array<JSXNode>;
37+
38+
while (childrenToProcess.length) {
39+
const child = childrenToProcess.shift();
40+
41+
if (!child) {
42+
continue;
43+
}
44+
45+
if (Array.isArray(child)) {
46+
childrenToProcess.unshift(...child);
47+
continue;
48+
}
49+
50+
switch (child.type) {
51+
case HAccordionItem: {
52+
child.props._index = currItemIndex;
53+
if (props.value !== undefined && props.value === child.props.value) {
54+
initialIndex = currItemIndex;
55+
}
56+
itemsMap.set(currItemIndex, child.props.disabled === true);
57+
58+
currItemIndex++;
59+
break;
60+
}
61+
62+
default: {
63+
if (child) {
64+
const anyChildren = Array.isArray(child.children)
65+
? [...child.children]
66+
: [child.children];
67+
childrenToProcess.unshift(...(anyChildren as JSXNode[]));
68+
}
69+
70+
break;
71+
}
72+
}
73+
}
74+
75+
return (
76+
<HAccordionRootImpl
77+
initialIndex={initialIndex ?? undefined}
78+
itemsMap={itemsMap}
79+
{...rest}
80+
>
81+
{props.children}
82+
</HAccordionRootImpl>
83+
);
84+
};

packages/kit-headless/src/components/accordion/accordion-root.tsx

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,13 @@
11
import {
2-
Component,
3-
JSXNode,
4-
PropsOf,
5-
QRL,
62
Signal,
73
Slot,
84
component$,
5+
useComputed$,
96
useContextProvider,
107
useSignal,
11-
useComputed$,
128
} from '@builder.io/qwik';
9+
import { AccordionRootProps } from './accordion-inline';
1310
import { accordionContextId } from './accordion-context';
14-
import { HAccordionItem } from './accordion-item';
15-
16-
export type AccordionRootProps = PropsOf<'div'> & {
17-
/** If true, multiple items can be open at the same time. */
18-
multiple?: boolean;
19-
20-
/** The reactive value controlling which item is open. */
21-
'bind:value'?: Signal<string | null>;
22-
23-
/** The initial value of the currently open item. */
24-
value?: string;
25-
26-
/** The initial index of the currently open item. */
27-
initialIndex?: number;
28-
29-
/** A QRL that is called when the selected item changes. */
30-
onChange$?: QRL<(value: string) => void>;
31-
32-
/** A map of the item indexes and their disabled state. */
33-
itemsMap?: Map<number, boolean>;
34-
};
35-
36-
export const HAccordionRoot: Component<AccordionRootProps> = (
37-
props: AccordionRootProps,
38-
) => {
39-
const { children: accordionChildren, ...rest } = props;
40-
41-
let currItemIndex = 0;
42-
let initialIndex = null;
43-
const itemsMap = new Map();
44-
45-
const childrenToProcess = (
46-
Array.isArray(accordionChildren) ? [...accordionChildren] : [accordionChildren]
47-
) as Array<JSXNode>;
48-
49-
while (childrenToProcess.length) {
50-
const child = childrenToProcess.shift();
51-
52-
if (!child) {
53-
continue;
54-
}
55-
56-
if (Array.isArray(child)) {
57-
childrenToProcess.unshift(...child);
58-
continue;
59-
}
60-
61-
switch (child.type) {
62-
case HAccordionItem: {
63-
child.props._index = currItemIndex;
64-
if (props.value !== undefined && props.value === child.props.value) {
65-
initialIndex = currItemIndex;
66-
}
67-
itemsMap.set(currItemIndex, child.props.disabled === true);
68-
69-
currItemIndex++;
70-
break;
71-
}
72-
73-
default: {
74-
if (child) {
75-
const anyChildren = Array.isArray(child.children)
76-
? [...child.children]
77-
: [child.children];
78-
childrenToProcess.unshift(...(anyChildren as JSXNode[]));
79-
}
80-
81-
break;
82-
}
83-
}
84-
}
85-
86-
return (
87-
<HAccordionRootImpl
88-
initialIndex={initialIndex ?? undefined}
89-
itemsMap={itemsMap}
90-
{...rest}
91-
>
92-
{props.children}
93-
</HAccordionRootImpl>
94-
);
95-
};
9611

9712
export const HAccordionRootImpl = component$((props: AccordionRootProps) => {
9813
const {
@@ -104,11 +19,13 @@ export const HAccordionRootImpl = component$((props: AccordionRootProps) => {
10419
...rest
10520
} = props;
10621

22+
itemsMap;
23+
10724
const selectedIndexSig = useSignal<number>(initialIndex ?? -1);
10825
const triggerRefsArray = useSignal<Array<Signal>>([]);
10926

11027
const itemsMapSig = useComputed$(() => {
111-
return itemsMap ?? new Map();
28+
return props.itemsMap!;
11229
});
11330

11431
const context = {

packages/kit-headless/src/components/accordion/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { HAccordionRoot as Root } from './accordion-root';
1+
export { HAccordionRoot as Root } from './accordion-inline';
22
export { HAccordionItem as Item } from './accordion-item';
33
export { HAccordionTrigger as Trigger } from './accordion-trigger';
44
export { HAccordionContent as Content } from './accordion-content';

0 commit comments

Comments
 (0)