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({ ...@@ -112,7 +112,7 @@ export default function FormSelect({
selectionProps.selectedKeys = new Set(selectedValues.map(toOptionKey)); selectionProps.selectedKeys = new Set(selectedValues.map(toOptionKey));
selectionProps.onSelectionChange = (keys) => { selectionProps.onSelectionChange = (keys) => {
const nextKeys = const nextKeys =
keys === 'all' ? options.map((option) => option.value) : [...keys]; keys === 'all' ? options.map((option) => option.id) : [...keys];
emitChange(nextKeys); emitChange(nextKeys);
}; };
} else { } else {
......
import { import {
ChevronDown, ChevronDown,
Check,
Close, Close,
FIELD_SUFFIX_ICON_STYLE, FIELD_SUFFIX_ICON_STYLE,
Search, Search,
...@@ -25,6 +26,10 @@ function getSelectedKeySet(selectionMode, selectedKey, selectedKeys) { ...@@ -25,6 +26,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,
...@@ -59,8 +64,12 @@ export default function XinyuanAutocomplete({ ...@@ -59,8 +64,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 ??
...@@ -71,7 +80,38 @@ export default function XinyuanAutocomplete({ ...@@ -71,7 +80,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);
};
return ( return (
<Autocomplete <Autocomplete
...@@ -126,6 +166,7 @@ export default function XinyuanAutocomplete({ ...@@ -126,6 +166,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"
...@@ -135,11 +176,17 @@ export default function XinyuanAutocomplete({ ...@@ -135,11 +176,17 @@ export default function XinyuanAutocomplete({
</div> </div>
)} )}
> >
{(option) => ( {(option) => {
const isSelected = selectedKeyStringSet.has(toSelectionKey(option.id));
return (
<ListBox.Item <ListBox.Item
id={option.id} id={option.id}
isDisabled={option.isDisabled} isDisabled={option.isDisabled}
textValue={option.textValue || option.label} textValue={option.textValue || option.label}
{...(selectionMode === 'multiple'
? { onPress: () => handleMultipleOptionPress(option) }
: {})}
> >
{renderOption ? ( {renderOption ? (
renderOption(option) renderOption(option)
...@@ -148,9 +195,19 @@ export default function XinyuanAutocomplete({ ...@@ -148,9 +195,19 @@ export default function XinyuanAutocomplete({
{option.label} {option.label}
</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.ItemIndicator />
</ListBox.Item>
)} )}
</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