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

Merged
merged 8 commits into from
Jul 8, 2025
Merged
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
6 changes: 6 additions & 0 deletions packages/pluggableWidgets/combobox-web/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed

- We fixed an issue where combobox would show aria-labelledby even when no label was added.

- We added the option to fill an aria-label for the combobox.

## [2.4.2] - 2025-06-10

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion packages/pluggableWidgets/combobox-web/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mendix/combobox-web",
"widgetName": "Combobox",
"version": "2.4.2",
"version": "2.4.3",
"description": "Configurable Combo box widget with suggestions and autocomplete.",
"copyright": "© Mendix Technology BV 2025. All rights reserved.",
"license": "Apache-2.0",
Expand Down
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 @@ -81,7 +81,7 @@
onLeaveEvent?: () => void;
}

export interface SingleSelector extends SelectorBase<"single", string> {}

Check warning on line 84 in packages/pluggableWidgets/combobox-web/src/helpers/types.ts

View workflow job for this annotation

GitHub Actions / Run code quality check

An interface declaring no members is equivalent to its supertype
export interface MultiSelector extends SelectorBase<"multi", string[]> {
selectedItemsStyle: SelectedItemsStyleEnum;
selectionMethod: SelectionMethodEnum;
Expand All @@ -100,6 +100,7 @@
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}"]`);
}
2 changes: 1 addition & 1 deletion packages/pluggableWidgets/combobox-web/src/package.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://www.mendix.com/package/1.0/">
<clientModule name="Combobox" version="2.4.2" xmlns="http://www.mendix.com/clientModule/1.0/">
<clientModule name="Combobox" version="2.4.3" xmlns="http://www.mendix.com/clientModule/1.0/">
<widgetFiles>
<widgetFile path="Combobox.xml" />
</widgetFiles>
Expand Down
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