Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
risk_case_library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
David Yang
risk_case_library
Commits
f1f68f53
Commit
f1f68f53
authored
Aug 27, 2026
by
Andy-bubu
1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: stabilize multi-select autocomplete values
parent
177dadc8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
9 deletions
+94
-9
XinyuanAutocomplete.jsx
src/components/XinyuanAutocomplete.jsx
+94
-9
No files found.
src/components/XinyuanAutocomplete.jsx
View file @
f1f68f53
import
{
import
{
ChevronDown
,
ChevronDown
,
Check
,
Close
,
Close
,
Search
,
Search
,
}
from
'@/components/AppIcons'
;
}
from
'@/components/AppIcons'
;
...
@@ -16,6 +17,16 @@ function defaultFilter(textValue, inputValue) {
...
@@ -16,6 +17,16 @@ function defaultFilter(textValue, inputValue) {
return
!
query
||
String
(
textValue
||
''
).
toLocaleLowerCase
().
includes
(
query
);
return
!
query
||
String
(
textValue
||
''
).
toLocaleLowerCase
().
includes
(
query
);
}
}
function
getSelectedKeySet
(
selectionMode
,
selectedKey
,
selectedKeys
)
{
if
(
selectionMode
===
'multiple'
)
{
return
new
Set
(
selectedKeys
||
[]);
}
return
new
Set
(
selectedKey
===
null
||
selectedKey
===
undefined
?
[]
:
[
selectedKey
]);
}
const
toSelectionKey
=
(
value
)
=>
String
(
value
??
''
);
export
default
function
XinyuanAutocomplete
({
export
default
function
XinyuanAutocomplete
({
'aria-label'
:
ariaLabel
,
'aria-label'
:
ariaLabel
,
className
,
className
,
...
@@ -32,21 +43,73 @@ export default function XinyuanAutocomplete({
...
@@ -32,21 +43,73 @@ export default function XinyuanAutocomplete({
renderOption
,
renderOption
,
searchPlaceholder
=
'输入关键词搜索'
,
searchPlaceholder
=
'输入关键词搜索'
,
selectedKey
,
selectedKey
,
selectedKeys
,
selectionMode
=
'single'
,
shouldFilter
=
true
,
shouldFilter
=
true
,
valueText
,
valueText
,
variant
=
'secondary'
,
variant
=
'secondary'
,
})
{
})
{
const
selectedOption
=
options
.
find
((
option
)
=>
option
.
id
===
selectedKey
);
const
selectedKeySet
=
getSelectedKeySet
(
const
resolvedValue
=
valueText
??
selectedOption
?.
label
??
placeholder
;
selectionMode
,
selectedKey
,
selectedKeys
,
);
const
selectedKeyStringSet
=
new
Set
(
Array
.
from
(
selectedKeySet
).
map
(
toSelectionKey
),
);
const
selectedOptions
=
options
.
filter
((
option
)
=>
selectedKeyStringSet
.
has
(
toSelectionKey
(
option
.
id
)),
);
const
resolvedValue
=
valueText
??
(
selectedOptions
.
length
>
0
?
selectedOptions
.
map
((
option
)
=>
option
.
label
).
join
(
'、'
)
:
placeholder
);
const
rootSelectionProps
=
selectionMode
===
'multiple'
?
{
selectedKeys
:
selectedKeyStringSet
}
:
{
selectedKey
};
const
listBoxSelectionProps
=
selectionMode
===
'multiple'
?
{
selectionMode
,
selectedKeys
:
selectedKeyStringSet
,
}
:
{};
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
{
...
rootSelectionProps
}
aria
-
label=
{
ariaLabel
||
placeholder
}
aria
-
label=
{
ariaLabel
||
placeholder
}
className=
{
className
}
className=
{
className
}
fullWidth
fullWidth
isDisabled=
{
isDisabled
}
isDisabled=
{
isDisabled
}
select
edKey=
{
selectedKey
}
select
ionMode=
{
selectionMode
}
variant=
{
variant
}
variant=
{
variant
}
onSelectionChange=
{
onSelectionChange
}
onSelectionChange=
{
onSelectionChange
}
>
>
...
@@ -75,19 +138,41 @@ export default function XinyuanAutocomplete({
...
@@ -75,19 +138,41 @@ export default function XinyuanAutocomplete({
</
SearchField
>
</
SearchField
>
<
ListBox
<
ListBox
aria
-
label=
{
ariaLabel
||
placeholder
}
aria
-
label=
{
ariaLabel
||
placeholder
}
dependencies=
{
[
Array
.
from
(
selectedKeyStringSet
).
sort
().
join
(
'
\
u0000'
)]
}
items=
{
visibleOptions
}
items=
{
visibleOptions
}
{
...
listBoxSelectionProps
}
renderEmptyState=
{
()
=>
(
renderEmptyState=
{
()
=>
(
<
div
className=
"grid min-h-20 place-items-center px-3 py-4"
>
<
div
className=
"grid min-h-20 place-items-center px-3 py-4"
>
{
isLoading
?
<
Spinner
size=
"sm"
/>
:
errorText
||
emptyText
}
{
isLoading
?
<
Spinner
size=
"sm"
/>
:
errorText
||
emptyText
}
</
div
>
</
div
>
)
}
)
}
>
>
{
(
option
)
=>
(
{
(
option
)
=>
{
<
ListBox
.
Item
id=
{
option
.
id
}
textValue=
{
option
.
textValue
||
option
.
label
}
>
const
isSelected
=
selectedKeyStringSet
.
has
(
toSelectionKey
(
option
.
id
));
{
renderOption
?
renderOption
(
option
)
:
<
Label
>
{
option
.
label
}
</
Label
>
}
<
ListBox
.
ItemIndicator
/>
return
(
</
ListBox
.
Item
>
<
ListBox
.
Item
)
}
id=
{
option
.
id
}
isDisabled=
{
option
.
isDisabled
}
textValue=
{
option
.
textValue
||
option
.
label
}
{
...
(
selectionMode
===
'
multiple
'
?
{
onPress
:
()
=
>
handleMultipleOptionPress(option) }
:
{}
)}
>
{
renderOption
?
renderOption
(
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
>
...
...
David Yang
@david.yang
mentioned in commit
17393f5a
·
Aug 27, 2026
mentioned in commit
17393f5a
mentioned in commit 17393f5a03280427d52edc0d2874a934f7c59f7f
Toggle commit list
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment