Skip to content

Commit b9d2cf5

Browse files
tests passing with map and set
1 parent 3d789ec commit b9d2cf5

File tree

3 files changed

+64
-65
lines changed

3 files changed

+64
-65
lines changed

packages/kit-headless/src/components/select/select-display-text.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from '@builder.io/qwik';
88

99
import SelectContextId from './select-context';
10-
import { useSelect } from './use-select';
1110

1211
type SelectValueProps = PropsOf<'span'> & {
1312
/**
@@ -21,20 +20,18 @@ export const SelectDisplayText = component$((props: SelectValueProps) => {
2120
const context = useContext(SelectContextId);
2221
const valueId = `${context.localId}-value`;
2322

24-
const { extractedStrOrArrFromMap$ } = useSelect();
25-
2623
const displayStrSig = useComputed$(async () => {
2724
if (context.multiple) {
2825
// for more customization when multiple is true
2926
return <Slot />;
3027
}
3128

32-
const currDisplayValue = await extractedStrOrArrFromMap$('displayValue');
33-
34-
if (currDisplayValue.length > 0) {
35-
return currDisplayValue;
36-
} else {
29+
if (context.selectedIndexSetSig.value.size === 0) {
3730
return placeholder;
31+
} else {
32+
return context.multiple
33+
? context.currDisplayValueSig.value
34+
: context.currDisplayValueSig.value?.[0] ?? placeholder;
3835
}
3936
});
4037

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ export const SelectImpl = component$<SelectProps<boolean> & InternalSelectProps>
184184

185185
useContextProvider(SelectContextId, context);
186186

187-
const { getActiveDescendant$, extractedStrOrArrFromMap$, selectionManager$ } =
188-
useSelect();
187+
const { getActiveDescendant$, selectionManager$ } = useSelect();
189188

190189
useTask$(async function reactiveUserValue({ track }) {
191190
const bindValueSig = props['bind:value'];
@@ -200,6 +199,8 @@ export const SelectImpl = component$<SelectProps<boolean> & InternalSelectProps>
200199
// for both SSR and CSR, we need to set the initial index
201200
context.highlightedIndexSig.value = index;
202201
}
202+
} else {
203+
await selectionManager$(index, 'remove');
203204
}
204205
}
205206
});
@@ -233,32 +234,39 @@ export const SelectImpl = component$<SelectProps<boolean> & InternalSelectProps>
233234
const bindDisplayTextSig = props['bind:displayText'];
234235
track(() => selectedIndexSetSig.value);
235236

236-
const currValue = await extractedStrOrArrFromMap$('value');
237+
const values = [];
238+
const displayValues = [];
239+
240+
for (const index of context.selectedIndexSetSig.value) {
241+
const item = context.itemsMapSig.value.get(index);
242+
243+
if (item) {
244+
values.push(item.value);
245+
displayValues.push(item.displayValue);
246+
}
247+
}
237248

238249
if (onChange$ && selectedIndexSetSig.value.size > 0) {
239-
await onChange$(currValue);
250+
await onChange$(context.multiple ? values : values[0]);
240251
}
241252

242253
// sync the user's given signal when an option is selected
243254
if (bindValueSig && bindValueSig.value) {
244255
const currUserSigValues = JSON.stringify(bindValueSig.value);
245-
const newUserSigValues = JSON.stringify(currValue);
256+
const newUserSigValues = JSON.stringify(values);
246257

247258
if (currUserSigValues !== newUserSigValues) {
248-
bindValueSig.value = currValue;
249-
console.log('BINDVALUE SIG', bindValueSig.value);
259+
bindValueSig.value = values;
250260
}
251261
}
252262

253-
const currDisplayValue = await extractedStrOrArrFromMap$('displayValue');
263+
context.currDisplayValueSig.value = displayValues;
254264

255265
// sync the user's given signal for the display value
256-
if (bindDisplayTextSig && currDisplayValue) {
257-
if (typeof currDisplayValue === 'string') {
258-
bindDisplayTextSig.value = [currDisplayValue];
259-
} else {
260-
bindDisplayTextSig.value = currDisplayValue;
261-
}
266+
if (bindDisplayTextSig && context.currDisplayValueSig.value) {
267+
bindDisplayTextSig.value = context.multiple
268+
? context.currDisplayValueSig.value
269+
: context.currDisplayValueSig.value[0];
262270
}
263271
});
264272

packages/kit-headless/src/components/select/use-select.tsx

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,50 @@ import SelectContextId from './select-context';
88
export function useSelect() {
99
const context = useContext(SelectContextId);
1010

11-
const selectionManager$ = $(async (index: number | null, action: 'add' | 'toggle') => {
12-
if (index === null) return;
13-
14-
// Check if the current index is disabled, and if so, find the next enabled index
15-
const currItem = context.itemsMapSig.value.get(index);
16-
17-
const enabledIndex =
18-
currItem && currItem.disabled ? await getNextEnabledItemIndex$(index) : index;
19-
20-
if (action === 'add') {
21-
if (context.multiple) {
22-
context.selectedIndexSetSig.value = new Set([
23-
...context.selectedIndexSetSig.value,
24-
index,
25-
]);
26-
} else {
27-
context.selectedIndexSetSig.value = new Set([index]);
11+
const selectionManager$ = $(
12+
async (index: number | null, action: 'add' | 'toggle' | 'remove') => {
13+
if (index === null) return;
14+
15+
const currItem = context.itemsMapSig.value.get(index);
16+
17+
const enabledIndex =
18+
currItem && currItem.disabled ? await getNextEnabledItemIndex$(index) : index;
19+
20+
if (action === 'add') {
21+
if (context.multiple) {
22+
context.selectedIndexSetSig.value = new Set([
23+
...context.selectedIndexSetSig.value,
24+
index,
25+
]);
26+
} else {
27+
context.selectedIndexSetSig.value = new Set([index]);
28+
}
2829
}
29-
}
3030

31-
if (action === 'toggle') {
32-
if (context.selectedIndexSetSig.value.has(enabledIndex)) {
31+
if (action === 'toggle') {
32+
if (context.selectedIndexSetSig.value.has(enabledIndex)) {
33+
context.selectedIndexSetSig.value = new Set(
34+
[...context.selectedIndexSetSig.value].filter(
35+
(selectedIndex) => selectedIndex !== enabledIndex,
36+
),
37+
);
38+
} else {
39+
context.selectedIndexSetSig.value = new Set([
40+
...context.selectedIndexSetSig.value,
41+
enabledIndex,
42+
]);
43+
}
44+
}
45+
46+
if (action === 'remove') {
3347
context.selectedIndexSetSig.value = new Set(
3448
[...context.selectedIndexSetSig.value].filter(
35-
(selectedIndex) => selectedIndex !== enabledIndex,
49+
(selectedIndex) => selectedIndex !== index,
3650
),
3751
);
38-
} else {
39-
context.selectedIndexSetSig.value = new Set([
40-
...context.selectedIndexSetSig.value,
41-
enabledIndex,
42-
]);
4352
}
44-
}
45-
});
53+
},
54+
);
4655

4756
const getNextEnabledItemIndex$ = $((index: number) => {
4857
let offset = 1;
@@ -96,26 +105,11 @@ export function useSelect() {
96105
return `${context.localId}-${index}`;
97106
});
98107

99-
const extractedStrOrArrFromMap$ = $((propertyType: 'value' | 'displayValue') => {
100-
const values = [];
101-
102-
for (const index of context.selectedIndexSetSig.value) {
103-
const item = context.itemsMapSig.value.get(index);
104-
105-
if (item) {
106-
values.push(item[propertyType]);
107-
}
108-
}
109-
110-
return values.length === 1 ? values[0] : values;
111-
});
112-
113108
return {
114109
getNextEnabledItemIndex$,
115110
getPrevEnabledItemIndex$,
116111
getActiveDescendant$,
117112
selectionManager$,
118-
extractedStrOrArrFromMap$,
119113
};
120114
}
121115

0 commit comments

Comments
 (0)