Commit 2a30208c authored by Andy-bubu's avatar Andy-bubu

fix: stabilize multi-select autocomplete values

parent e5b0ea0c
import {
ChevronDown,
Check,
Close,
FIELD_SUFFIX_ICON_STYLE,
Search,
......@@ -27,6 +28,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,
......@@ -62,8 +67,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 ??
......@@ -74,7 +83,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);
};
const defaultValue = selectionMode === 'multiple' && selectedOptions.length > 0 ? (
<TagGroup
aria-label="已选择项"
......@@ -161,6 +201,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"
......@@ -170,11 +211,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)
......@@ -183,9 +230,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