Skip to content

Commit cd66a54

Browse files
samuelreichertgjulivan
authored andcommitted
feat(combobox-web): integrate label check in MultiSelection and SingleSelection
1 parent 03222c2 commit cd66a54

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

packages/pluggableWidgets/combobox-web/src/components/MultiSelection/MultiSelection.tsx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ClearButton } from "../../assets/icons";
44
import { MultiSelector, SelectionBaseProps } from "../../helpers/types";
55
import { getSelectedCaptionsPlaceholder } from "../../helpers/utils";
66
import { useDownshiftMultiSelectProps } from "../../hooks/useDownshiftMultiSelectProps";
7+
import { useHasLabel } from "../../hooks/useHasLabel";
78
import { useLazyLoading } from "../../hooks/useLazyLoading";
89
import { ComboboxWrapper } from "../ComboboxWrapper";
910
import { InputPlaceholder } from "../Placeholder";
@@ -37,6 +38,34 @@ export function MultiSelection({
3738
const inputRef = useRef<HTMLInputElement>(null);
3839
const isSelectedItemsBoxStyle = selector.selectedItemsStyle === "boxes";
3940
const isOptionsSelected = selector.isOptionsSelected();
41+
const inputProps = getInputProps({
42+
...getDropdownProps(
43+
{
44+
preventKeyAction: isOpen
45+
},
46+
{ suppressRefError: true }
47+
),
48+
ref: inputRef,
49+
onKeyDown: (event: KeyboardEvent) => {
50+
if (
51+
(event.key === "Backspace" && inputRef.current?.selectionStart === 0) ||
52+
(event.key === "ArrowLeft" && isSelectedItemsBoxStyle && inputRef.current?.selectionStart === 0)
53+
) {
54+
setActiveIndex(selectedItems.length - 1);
55+
}
56+
if (event.key === " ") {
57+
if (highlightedIndex >= 0) {
58+
toggleSelectedItem(highlightedIndex);
59+
event.preventDefault();
60+
event.stopPropagation();
61+
}
62+
}
63+
},
64+
disabled: selector.readOnly,
65+
readOnly: selector.options.filterType === "none",
66+
"aria-required": ariaRequired.value
67+
});
68+
const hasLabel = useHasLabel(inputProps.id);
4069

4170
const memoizedselectedCaptions = useMemo(
4271
() => getSelectedCaptionsPlaceholder(selector, selectedItems),
@@ -106,35 +135,8 @@ export function MultiSelection({
106135
})}
107136
tabIndex={tabIndex}
108137
placeholder=" "
109-
{...getInputProps({
110-
...getDropdownProps(
111-
{
112-
preventKeyAction: isOpen
113-
},
114-
{ suppressRefError: true }
115-
),
116-
ref: inputRef,
117-
onKeyDown: (event: KeyboardEvent) => {
118-
if (
119-
(event.key === "Backspace" && inputRef.current?.selectionStart === 0) ||
120-
(event.key === "ArrowLeft" &&
121-
isSelectedItemsBoxStyle &&
122-
inputRef.current?.selectionStart === 0)
123-
) {
124-
setActiveIndex(selectedItems.length - 1);
125-
}
126-
if (event.key === " ") {
127-
if (highlightedIndex >= 0) {
128-
toggleSelectedItem(highlightedIndex);
129-
event.preventDefault();
130-
event.stopPropagation();
131-
}
132-
}
133-
},
134-
disabled: selector.readOnly,
135-
readOnly: selector.options.filterType === "none",
136-
"aria-required": ariaRequired.value
137-
})}
138+
{...inputProps}
139+
aria-labelledby={hasLabel ? inputProps["aria-labelledby"] : undefined}
138140
/>
139141
<InputPlaceholder isEmpty={selectedItems.length <= 0}>{memoizedselectedCaptions}</InputPlaceholder>
140142
</div>

packages/pluggableWidgets/combobox-web/src/components/SingleSelection/SingleSelection.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Fragment, ReactElement, createElement, useMemo, useRef } from "react";
33
import { ClearButton } from "../../assets/icons";
44
import { SelectionBaseProps, SingleSelector } from "../../helpers/types";
55
import { useDownshiftSingleSelectProps } from "../../hooks/useDownshiftSingleSelectProps";
6+
import { useHasLabel } from "../../hooks/useHasLabel";
67
import { useLazyLoading } from "../../hooks/useLazyLoading";
78
import { ComboboxWrapper } from "../ComboboxWrapper";
89
import { InputPlaceholder } from "../Placeholder";
@@ -44,6 +45,7 @@ export function SingleSelection({
4445

4546
const selectedItemCaption = useMemo(
4647
() => selector.caption.render(selectedItem, "label"),
48+
// eslint-disable-next-line react-hooks/exhaustive-deps
4749
[
4850
selectedItem,
4951
selector.status,
@@ -54,6 +56,17 @@ export function SingleSelection({
5456
]
5557
);
5658

59+
const inputProps = getInputProps(
60+
{
61+
disabled: selector.readOnly,
62+
readOnly: selector.options.filterType === "none",
63+
ref: inputRef,
64+
"aria-required": ariaRequired.value
65+
},
66+
{ suppressRefError: true }
67+
);
68+
const hasLabel = useHasLabel(inputProps.id);
69+
5770
return (
5871
<Fragment>
5972
<ComboboxWrapper
@@ -74,16 +87,9 @@ export function SingleSelection({
7487
"widget-combobox-input-nofilter": selector.options.filterType === "none"
7588
})}
7689
tabIndex={tabIndex}
77-
{...getInputProps(
78-
{
79-
disabled: selector.readOnly,
80-
readOnly: selector.options.filterType === "none",
81-
ref: inputRef,
82-
"aria-required": ariaRequired.value
83-
},
84-
{ suppressRefError: true }
85-
)}
90+
{...inputProps}
8691
placeholder=" "
92+
aria-labelledby={hasLabel ? inputProps["aria-labelledby"] : undefined}
8793
/>
8894
<InputPlaceholder
8995
isEmpty={!selector.currentId || !selector.caption.render(selectedItem, "label")}

0 commit comments

Comments
 (0)