Commit 4b89489e authored by David Yang's avatar David Yang

fix: stabilize multi-select autocomplete values

parent 6763c26b
......@@ -112,7 +112,7 @@ export default function FormSelect({
selectionProps.selectedKeys = new Set(selectedValues.map(toOptionKey));
selectionProps.onSelectionChange = (keys) => {
const nextKeys =
keys === 'all' ? options.map((option) => option.value) : [...keys];
keys === 'all' ? options.map((option) => option.id) : [...keys];
emitChange(nextKeys);
};
} else {
......
import {
ChevronDown,
Check,
Close,
FIELD_SUFFIX_ICON_STYLE,
Search,
......@@ -25,6 +26,10 @@ function getSelectedKeySet(selectionMode, selectedKey, selectedKeys) {
return new Set(selectedKey === null || selectedKey === undefined ? [] : [selectedKey]);
}
function toSelectionKey(value) {
return String(value ?? '');
}
export default function XinyuanAutocomplete({
'aria-label': ariaLabel,
className,
......@@ -59,8 +64,12 @@ export default function XinyuanAutocomplete({
selectedKey,
selectedKeys,
);
const selectedKeyStringSet = new Set(
Array.from(selectedKeySet).map(toSelectionKey),
);
const selectedOptions = options.filter((option) =>
selectedKeySet.has(option.id),
selectedKeySet.has(option.id) ||
selectedKeyStringSet.has(toSelectionKey(option.id)),
);
const resolvedValueText =
valueText ??
......@@ -71,7 +80,38 @@ export default function XinyuanAutocomplete({
selectionMode === 'multiple'
? { selectedKeys }
: { selectedKey };
const listBoxSelectionProps =
selectionMode === 'multiple'
? {
selectionMode,
selectedKeys,
}
: {};
const visibleOptions = isLoading || errorText ? [] : options;
const handleMultipleOptionPress = (option) => {
if (
selectionMode !== 'multiple' ||
!onSelectionChange ||
option?.isDisabled
) {
return;
}
const optionKey = toSelectionKey(option.id);
if (!optionKey) {
return;
}
const nextKeys = new Set(
Array.from(selectedKeyStringSet).filter(Boolean),
);
if (nextKeys.has(optionKey)) {
nextKeys.delete(optionKey);
} else {
nextKeys.add(optionKey);
}
onSelectionChange(nextKeys);
};
return (
<Autocomplete
......@@ -126,6 +166,7 @@ export default function XinyuanAutocomplete({
<ListBox
aria-label={ariaLabel || placeholder}
items={visibleOptions}
{...listBoxSelectionProps}
renderEmptyState={() => (
<div
className="grid min-h-20 place-items-center px-3 py-4"
......@@ -135,11 +176,17 @@ export default function XinyuanAutocomplete({
</div>
)}
>
{(option) => (
{(option) => {
const isSelected = selectedKeyStringSet.has(toSelectionKey(option.id));
return (
<ListBox.Item
id={option.id}
isDisabled={option.isDisabled}
textValue={option.textValue || option.label}
{...(selectionMode === 'multiple'
? { onPress: () => handleMultipleOptionPress(option) }
: {})}
>
{renderOption ? (
renderOption(option)
......@@ -148,9 +195,19 @@ export default function XinyuanAutocomplete({
{option.label}
</Label>
)}
{selectionMode === 'multiple' ? (
<span
aria-hidden="true"
className="ml-auto inline-flex w-5 justify-center text-foreground"
>
{isSelected ? <Check size={14} /> : null}
</span>
) : (
<ListBox.ItemIndicator />
</ListBox.Item>
)}
</ListBox.Item>
);
}}
</ListBox>
</Autocomplete.Filter>
</Autocomplete.Popover>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment