Skip to content

Commit 47cd150

Browse files
✨ Add disable combobox actions when only one item
Prevents opening the combobox when the list contains only the selected item. This avoids unnecessary UI interactions in scenarios where no other actions are possible.
1 parent 0934c6a commit 47cd150

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

packages/core/src/components/combobox/ComboBox.tsx

+28-8
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ const ComboBox: FC<ComboBoxProps> = ({
193193
[areaProvider.shouldChangeColor],
194194
);
195195

196+
const shouldDisableActions = useMemo(() => {
197+
if (!selectedItem) {
198+
return false;
199+
}
200+
201+
const combinedLists = lists.flatMap((list) => list.list);
202+
203+
return (
204+
combinedLists.length === 1 &&
205+
combinedLists.some((item) => item.value === selectedItem.value)
206+
);
207+
}, [lists, selectedItem]);
208+
196209
useEffect(() => {
197210
if (styledComboBoxElementRef.current && !container) {
198211
const el = styledComboBoxElementRef.current as HTMLElement;
@@ -240,6 +253,10 @@ const ComboBox: FC<ComboBoxProps> = ({
240253
);
241254

242255
const handleOpen = useCallback(() => {
256+
if (shouldDisableActions) {
257+
return;
258+
}
259+
243260
if (styledComboBoxElementRef.current && newContainer) {
244261
const {
245262
left: comboBoxLeft,
@@ -265,7 +282,7 @@ const ComboBox: FC<ComboBoxProps> = ({
265282

266283
setIsAnimating(true);
267284
}
268-
}, [newContainer, direction]);
285+
}, [shouldDisableActions, newContainer, direction]);
269286

270287
const handleClose = useCallback(() => {
271288
setIsAnimating(false);
@@ -720,13 +737,15 @@ const ComboBox: FC<ComboBoxProps> = ({
720737
<Icon icons={['fa fa-times']} />
721738
</StyledComboBoxClearIconWrapper>
722739
)}
723-
<StyledComboBoxIconWrapper
724-
$shouldShowBorderLeft={
725-
shouldShowClearIcon === true && internalSelectedItem !== undefined
726-
}
727-
>
728-
<Icon icons={['fa fa-chevron-down']} />
729-
</StyledComboBoxIconWrapper>
740+
{!shouldDisableActions && (
741+
<StyledComboBoxIconWrapper
742+
$shouldShowBorderLeft={
743+
shouldShowClearIcon === true && internalSelectedItem !== undefined
744+
}
745+
>
746+
<Icon icons={['fa fa-chevron-down']} />
747+
</StyledComboBoxIconWrapper>
748+
)}
730749
</StyledComboBoxHeader>
731750
{portal}
732751
</StyledComboBox>
@@ -755,6 +774,7 @@ const ComboBox: FC<ComboBoxProps> = ({
755774
placeholderText,
756775
shouldShowClearIcon,
757776
handleClear,
777+
shouldDisableActions,
758778
portal,
759779
],
760780
);

0 commit comments

Comments
 (0)