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

fix: stabilize multi-select autocomplete values

parent e5b0ea0c
import { import {
ChevronDown, ChevronDown,
Check,
Close, Close,
FIELD_SUFFIX_ICON_STYLE, FIELD_SUFFIX_ICON_STYLE,
Search, Search,
...@@ -27,6 +28,10 @@ function getSelectedKeySet(selectionMode, selectedKey, selectedKeys) { ...@@ -27,6 +28,10 @@ function getSelectedKeySet(selectionMode, selectedKey, selectedKeys) {
return new Set(selectedKey === null || selectedKey === undefined ? [] : [selectedKey]); return new Set(selectedKey === null || selectedKey === undefined ? [] : [selectedKey]);
} }
function toSelectionKey(value) {
return String(value ?? '');
}
export default function XinyuanAutocomplete({ export default function XinyuanAutocomplete({
'aria-label': ariaLabel, 'aria-label': ariaLabel,
className, className,
...@@ -62,8 +67,12 @@ export default function XinyuanAutocomplete({ ...@@ -62,8 +67,12 @@ export default function XinyuanAutocomplete({
selectedKey, selectedKey,
selectedKeys, selectedKeys,
); );
const selectedKeyStringSet = new Set(
Array.from(selectedKeySet).map(toSelectionKey),
);
const selectedOptions = options.filter((option) => const selectedOptions = options.filter((option) =>
selectedKeySet.has(option.id), selectedKeySet.has(option.id) ||
selectedKeyStringSet.has(toSelectionKey(option.id)),
); );
const resolvedValueText = const resolvedValueText =
valueText ?? valueText ??
...@@ -74,7 +83,38 @@ export default function XinyuanAutocomplete({ ...@@ -74,7 +83,38 @@ export default function XinyuanAutocomplete({
selectionMode === 'multiple' selectionMode === 'multiple'
? { selectedKeys } ? { selectedKeys }
: { selectedKey }; : { selectedKey };
const listBoxSelectionProps =
selectionMode === 'multiple'
? {
selectionMode,
selectedKeys,
}
: {};
const visibleOptions = isLoading || errorText ? [] : options; 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 ? ( const defaultValue = selectionMode === 'multiple' && selectedOptions.length > 0 ? (
<TagGroup <TagGroup
aria-label="已选择项" aria-label="已选择项"
...@@ -161,6 +201,7 @@ export default function XinyuanAutocomplete({ ...@@ -161,6 +201,7 @@ export default function XinyuanAutocomplete({
<ListBox <ListBox
aria-label={ariaLabel || placeholder} aria-label={ariaLabel || placeholder}
items={visibleOptions} items={visibleOptions}
{...listBoxSelectionProps}
renderEmptyState={() => ( renderEmptyState={() => (
<div <div
className="grid min-h-20 place-items-center px-3 py-4" className="grid min-h-20 place-items-center px-3 py-4"
...@@ -170,22 +211,38 @@ export default function XinyuanAutocomplete({ ...@@ -170,22 +211,38 @@ export default function XinyuanAutocomplete({
</div> </div>
)} )}
> >
{(option) => ( {(option) => {
<ListBox.Item const isSelected = selectedKeyStringSet.has(toSelectionKey(option.id));
id={option.id}
isDisabled={option.isDisabled} return (
textValue={option.textValue || option.label} <ListBox.Item
> id={option.id}
{renderOption ? ( isDisabled={option.isDisabled}
renderOption(option) textValue={option.textValue || option.label}
) : ( {...(selectionMode === 'multiple'
<Label className="truncate" title={option.label}> ? { onPress: () => handleMultipleOptionPress(option) }
{option.label} : {})}
</Label> >
)} {renderOption ? (
<ListBox.ItemIndicator /> renderOption(option)
</ListBox.Item> ) : (
)} <Label className="truncate" title={option.label}>
{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> </ListBox>
</Autocomplete.Filter> </Autocomplete.Filter>
</Autocomplete.Popover> </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