Skip to content

[WC-2949]: fix accessibility issue on combobox label #1629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/pluggableWidgets/combobox-web/src/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function Combobox(props: ComboboxContainerProps): ReactElement {
noOptionsText: props.noOptionsText?.value,
readOnlyStyle: props.readOnlyStyle,
ariaRequired: props.ariaRequired,
ariaLabel: props.ariaLabel?.value,
a11yConfig: {
ariaLabels: {
clearSelection: props.clearButtonAriaLabel?.value ?? "",
Expand Down
8 changes: 8 additions & 0 deletions packages/pluggableWidgets/combobox-web/src/Combobox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@
</property>
</propertyGroup>
<propertyGroup caption="Aria labels">
<property key="ariaLabel" type="textTemplate" required="false">
<caption>Aria label</caption>
<description>Used to describe the combo box.</description>
<translations>
<translation lang="en_US">Combo box</translation>
<translation lang="nl_NL">Keuzelijst</translation>
</translations>
</property>
<property key="clearButtonAriaLabel" type="textTemplate" required="false">
<caption>Clear selection button</caption>
<description>Used to clear all selected values.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ exports[`Combo box (Association) renders combobox widget 1`] = `
aria-autocomplete="list"
aria-controls="downshift-0-menu"
aria-expanded="false"
aria-labelledby="comboBox1-label"
aria-required="true"
autocomplete="off"
class="widget-combobox-input"
Expand Down Expand Up @@ -129,7 +128,6 @@ exports[`Combo box (Association) toggles combobox menu on: input CLICK(focus) /
aria-autocomplete="list"
aria-controls="downshift-2-menu"
aria-expanded="false"
aria-labelledby="comboBox1-label"
aria-required="true"
autocomplete="off"
class="widget-combobox-input"
Expand Down Expand Up @@ -240,7 +238,6 @@ exports[`Combo box (Association) toggles combobox menu on: input TOGGLE BUTTON 1
aria-autocomplete="list"
aria-controls="downshift-6-menu"
aria-expanded="false"
aria-labelledby="comboBox1-label"
aria-required="true"
autocomplete="off"
class="widget-combobox-input"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ exports[`Combo box (Association) renders combobox widget 1`] = `
aria-autocomplete="list"
aria-controls="downshift-0-menu"
aria-expanded="false"
aria-labelledby="comboBox1-label"
aria-required="true"
autocomplete="off"
class="widget-combobox-input"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ exports[`Combo box (Static values) renders combobox widget 1`] = `
aria-autocomplete="list"
aria-controls="downshift-0-menu"
aria-expanded="false"
aria-labelledby="comboBox1-label"
aria-required="true"
autocomplete="off"
class="widget-combobox-input"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import classNames from "classnames";
import { Fragment, KeyboardEvent, ReactElement, createElement, useMemo, useRef } from "react";
import { ClearButton } from "../../assets/icons";
import { MultiSelector, SelectionBaseProps } from "../../helpers/types";
import { getSelectedCaptionsPlaceholder } from "../../helpers/utils";
import { getInputLabel, getSelectedCaptionsPlaceholder } from "../../helpers/utils";
import { useDownshiftMultiSelectProps } from "../../hooks/useDownshiftMultiSelectProps";
import { useLazyLoading } from "../../hooks/useLazyLoading";
import { ComboboxWrapper } from "../ComboboxWrapper";
Expand Down Expand Up @@ -37,6 +37,36 @@ export function MultiSelection({
const inputRef = useRef<HTMLInputElement>(null);
const isSelectedItemsBoxStyle = selector.selectedItemsStyle === "boxes";
const isOptionsSelected = selector.isOptionsSelected();
const inputLabel = getInputLabel(options.inputId);
const hasLabel = useMemo(() => Boolean(inputLabel), [inputLabel]);
const inputProps = getInputProps({
...getDropdownProps(
{
preventKeyAction: isOpen
},
{ suppressRefError: true }
),
ref: inputRef,
onKeyDown: (event: KeyboardEvent) => {
if (
(event.key === "Backspace" && inputRef.current?.selectionStart === 0) ||
(event.key === "ArrowLeft" && isSelectedItemsBoxStyle && inputRef.current?.selectionStart === 0)
) {
setActiveIndex(selectedItems.length - 1);
}
if (event.key === " ") {
if (highlightedIndex >= 0) {
toggleSelectedItem(highlightedIndex);
event.preventDefault();
event.stopPropagation();
}
}
},
disabled: selector.readOnly,
readOnly: selector.options.filterType === "none",
"aria-required": ariaRequired.value,
"aria-label": !hasLabel && options.ariaLabel ? options.ariaLabel : undefined
});

const memoizedselectedCaptions = useMemo(
() => getSelectedCaptionsPlaceholder(selector, selectedItems),
Expand Down Expand Up @@ -106,35 +136,8 @@ export function MultiSelection({
})}
tabIndex={tabIndex}
placeholder=" "
{...getInputProps({
...getDropdownProps(
{
preventKeyAction: isOpen
},
{ suppressRefError: true }
),
ref: inputRef,
onKeyDown: (event: KeyboardEvent) => {
if (
(event.key === "Backspace" && inputRef.current?.selectionStart === 0) ||
(event.key === "ArrowLeft" &&
isSelectedItemsBoxStyle &&
inputRef.current?.selectionStart === 0)
) {
setActiveIndex(selectedItems.length - 1);
}
if (event.key === " ") {
if (highlightedIndex >= 0) {
toggleSelectedItem(highlightedIndex);
event.preventDefault();
event.stopPropagation();
}
}
},
disabled: selector.readOnly,
readOnly: selector.options.filterType === "none",
"aria-required": ariaRequired.value
})}
{...inputProps}
aria-labelledby={hasLabel ? inputProps["aria-labelledby"] : undefined}
/>
<InputPlaceholder isEmpty={selectedItems.length <= 0}>{memoizedselectedCaptions}</InputPlaceholder>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import classNames from "classnames";
import { Fragment, ReactElement, createElement, useMemo, useRef } from "react";
import { ClearButton } from "../../assets/icons";
import { SelectionBaseProps, SingleSelector } from "../../helpers/types";
import { getInputLabel } from "../../helpers/utils";
import { useDownshiftSingleSelectProps } from "../../hooks/useDownshiftSingleSelectProps";
import { useLazyLoading } from "../../hooks/useLazyLoading";
import { ComboboxWrapper } from "../ComboboxWrapper";
Expand Down Expand Up @@ -44,6 +45,7 @@ export function SingleSelection({

const selectedItemCaption = useMemo(
() => selector.caption.render(selectedItem, "label"),
// eslint-disable-next-line react-hooks/exhaustive-deps
[
selectedItem,
selector.status,
Expand All @@ -54,6 +56,20 @@ export function SingleSelection({
]
);

const inputLabel = getInputLabel(options.inputId);
const hasLabel = useMemo(() => Boolean(inputLabel), [inputLabel]);

const inputProps = getInputProps(
{
disabled: selector.readOnly,
readOnly: selector.options.filterType === "none",
ref: inputRef,
"aria-required": ariaRequired.value,
"aria-label": !hasLabel && options.ariaLabel ? options.ariaLabel : undefined
},
{ suppressRefError: true }
);

return (
<Fragment>
<ComboboxWrapper
Expand All @@ -74,16 +90,9 @@ export function SingleSelection({
"widget-combobox-input-nofilter": selector.options.filterType === "none"
})}
tabIndex={tabIndex}
{...getInputProps(
{
disabled: selector.readOnly,
readOnly: selector.options.filterType === "none",
ref: inputRef,
"aria-required": ariaRequired.value
},
{ suppressRefError: true }
)}
{...inputProps}
placeholder=" "
aria-labelledby={hasLabel ? inputProps["aria-labelledby"] : undefined}
/>
<InputPlaceholder
isEmpty={!selector.currentId || !selector.caption.render(selectedItem, "label")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface SelectionBaseProps<Selector> {
menuFooterContent?: ReactNode;
tabIndex: number;
ariaRequired: DynamicValue<boolean>;
ariaLabel?: string;
a11yConfig: {
ariaLabels: {
clearSelection: string;
Expand Down
4 changes: 4 additions & 0 deletions packages/pluggableWidgets/combobox-web/src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,7 @@ function sortSelections(
}
return newValueIds;
}

export function getInputLabel(inputId: string): Element | null {
return document.querySelector(`label[for="${inputId}"]`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface ComboboxContainerProps {
onEnterEvent?: ActionValue;
onLeaveEvent?: ActionValue;
ariaRequired: DynamicValue<boolean>;
ariaLabel?: DynamicValue<string>;
clearButtonAriaLabel?: DynamicValue<string>;
removeValueAriaLabel?: DynamicValue<string>;
a11ySelectedValue?: DynamicValue<string>;
Expand Down Expand Up @@ -145,6 +146,7 @@ export interface ComboboxPreviewProps {
onEnterEvent: {} | null;
onLeaveEvent: {} | null;
ariaRequired: string;
ariaLabel: string;
clearButtonAriaLabel: string;
removeValueAriaLabel: string;
a11ySelectedValue: string;
Expand Down
Loading