From ccc789901b2f1d7bda55e0d54d639d6a73972404 Mon Sep 17 00:00:00 2001 From: jack shelton Date: Sat, 21 Sep 2024 21:03:39 -0500 Subject: [PATCH] multiple shows correct display --- .../components/combobox/combobox-context.tsx | 2 +- .../src/components/combobox/combobox-root.tsx | 30 +++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/kit-headless/src/components/combobox/combobox-context.tsx b/packages/kit-headless/src/components/combobox/combobox-context.tsx index c6096a790..a267bca27 100644 --- a/packages/kit-headless/src/components/combobox/combobox-context.tsx +++ b/packages/kit-headless/src/components/combobox/combobox-context.tsx @@ -20,7 +20,7 @@ export type ComboboxContext = { selectedValueSetSig: Signal>; selectedValuesSig: Signal; highlightedIndexSig: Signal; - currDisplayValueSig: Signal; + displayValuesSig: Signal; isMouseOverPopupSig: Signal; disabledIndexSetSig: Signal>; removeOnBackspace: boolean; diff --git a/packages/kit-headless/src/components/combobox/combobox-root.tsx b/packages/kit-headless/src/components/combobox/combobox-root.tsx index ae2590a98..2d63c329d 100644 --- a/packages/kit-headless/src/components/combobox/combobox-root.tsx +++ b/packages/kit-headless/src/components/combobox/combobox-root.tsx @@ -122,8 +122,6 @@ export const HComboboxRootImpl = component$< onInput$, onChange$, onOpenChange$, - 'bind:value': givenValueSig, - 'bind:open': givenOpenSig, initialIndex, initialValue, loop: givenLoop, @@ -136,11 +134,15 @@ export const HComboboxRootImpl = component$< removeOnBackspace = false, name, required, + 'bind:value': givenValueSig, + 'bind:open': givenOpenSig, + 'bind:displayValue': givenDisplayValueSig, ...rest } = props; // source of truth const isListboxOpenSig = useBoundSignal(givenOpenSig, false); + const displayValuesSig = useBoundSignal(givenDisplayValueSig, []); const selectedValuesSig = useBoundSignal( givenValueSig, multiple ? (initialValue ? [initialValue] : []) : initialValue || '', @@ -167,7 +169,6 @@ export const HComboboxRootImpl = component$< const isMouseOverPopupSig = useSignal(false); const highlightedIndexSig = useSignal(initialIndex ?? null); const initialLoadSig = useSignal(true); - const currDisplayValueSig = useSignal(); const scrollOptions = givenScrollOptions ?? { behavior: 'smooth', block: 'center', @@ -217,7 +218,7 @@ export const HComboboxRootImpl = component$< selectedValuesSig, selectedValueSetSig, disabledIndexSetSig, - currDisplayValueSig, + displayValuesSig, isMouseOverPopupSig, initialLoadSig, removeOnBackspace, @@ -259,22 +260,27 @@ export const HComboboxRootImpl = component$< await onChange$?.(selectedValuesSig.value); } - const selectedValue = Array.isArray(selectedValuesSig.value) - ? selectedValuesSig.value[selectedValuesSig.value.length - 1] - : selectedValuesSig.value; + const selectedValues = Array.isArray(selectedValuesSig.value) + ? selectedValuesSig.value + : [selectedValuesSig.value]; + + const displayValues: string[] = []; for (const [index, item] of itemsMapSig.value.entries()) { - if (item.value === selectedValue) { + if (selectedValues.includes(item.value)) { + displayValues.push(item.displayValue); + /* avoid "highlight jumping" on multiple selections */ if (context.isListboxOpenSig.value === false) { context.highlightedIndexSig.value = index; } - - if (!context.inputRef.value) return; - context.inputRef.value.value = item.displayValue; - break; } } + + displayValuesSig.value = displayValues; + + if (multiple || !context.inputRef.value) return; + context.inputRef.value.value = displayValues[0]; }); useTask$(() => {