Skip to content

fix(AnalyticalTable): announce grouped, filtered and sorted columns with screen readers #4392

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 3 commits into from
Mar 22, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,51 @@ describe('AnalyticalTable', () => {
cy.findByText('Selected: {"0":true,"1":true,"2":true,"3":true}').should('be.visible');
});

it('a11y: grouped, filtered, sorted', () => {
cy.mount(<AnalyticalTable columns={columns} data={data} groupable filterable sortable />);
cy.findByText('Name').click();
cy.findByText('Sort Ascending').shadow().findByRole('listitem').click({ force: true });
cy.get('[data-column-id="name"]').should('have.attr', 'aria-sort', 'ascending');
cy.findByText('Name').click();
cy.findByText('Clear Sorting').shadow().findByRole('listitem').click({ force: true });
cy.get('[data-column-id="name"]').should('not.have.attr', 'aria-sort');
cy.findByText('Name').click();
cy.findByText('Sort Descending').shadow().findByRole('listitem').click({ force: true });
cy.get('[data-column-id="name"]').should('have.attr', 'aria-sort', 'descending');
cy.findByText('Name').click();
cy.findByText('Sort Ascending').shadow().get('[ui5-input]').typeIntoUi5Input('A{enter}');
cy.get('[data-column-id="name"]')
.should('have.attr', 'aria-sort', 'descending')
.and('have.attr', 'aria-label', 'Filtered');

cy.findByText('Name').click();
cy.findByText('Group').shadow().findByRole('listitem').click({ force: true });
cy.get('[data-column-id="name"]')
.should('have.attr', 'aria-sort', 'descending')
.and('have.attr', 'aria-label', 'Filtered Grouped');
cy.get('[data-visible-row-index="1"][data-visible-column-index="0"]').should(
'have.attr',
'aria-label',
'Grouped, To expand the row, press the spacebar'
);
cy.get('[name="navigation-right-arrow"]').click();
cy.get('[data-visible-row-index="1"][data-visible-column-index="0"]').should(
'have.attr',
'aria-label',
'Grouped, To collapse the row, press the spacebar'
);
cy.findByText('Name').click();
cy.findByText('Ungroup').shadow().findByRole('listitem').click({ force: true });
cy.get('[data-visible-row-index="1"][data-visible-column-index="0"]').should('not.have.attr', 'aria-label');
cy.get('[data-column-id="name"]')
.should('have.attr', 'aria-sort', 'descending')
.and('have.attr', 'aria-label', 'Filtered');

cy.findByText('Name').click();
cy.findByText('Sort Ascending').shadow().get('[ui5-input]').typeIntoUi5Input('{selectall}{backspace}{enter}');
cy.get('[data-column-id="name"]').should('have.attr', 'aria-sort', 'descending').and('not.have.attr', 'aria-label');
});

cypressPassThroughTestsFactory(AnalyticalTable, { data, columns });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export const ColumnHeaderModal = (props: ColumnHeaderModalProperties) => {
onKeyDown={handleCustomLiKeyDown}
>
<FlexBox alignItems={FlexBoxAlignItems.Center} className={classes.filter}>
<Icon name={iconFilter} className={classes.filterIcon} />
<Icon name={iconFilter} className={classes.filterIcon} aria-hidden />
<Filter column={column} popoverRef={ref} />
</FlexBox>
</CustomListItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import iconSortDescending from '@ui5/webcomponents-icons/dist/sort-descending.js
import { ThemingParameters } from '@ui5/webcomponents-react-base';
import { clsx } from 'clsx';
import React, {
AriaAttributes,
CSSProperties,
DragEventHandler,
FC,
Expand Down Expand Up @@ -49,6 +50,9 @@ export interface ColumnHeaderProps {
style: CSSProperties;
column: ColumnType;
role: string;
isFiltered?: boolean;
['aria-sort']?: AriaAttributes['aria-sort'];
['aria-label']?: AriaAttributes['aria-label'];
}

const styles = {
Expand Down Expand Up @@ -116,10 +120,12 @@ export const ColumnHeader: FC<ColumnHeaderProps> = (props: ColumnHeaderProps) =>
onClick,
onKeyDown,
portalContainer,
scaleXFactor
scaleXFactor,
isFiltered,
'aria-label': ariaLabel,
'aria-sort': ariaSort
} = props;

const isFiltered = column.filterValue && column.filterValue.length > 0;
const [popoverOpen, setPopoverOpen] = useState(false);
const columnHeaderRef = useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -190,6 +196,7 @@ export const ColumnHeader: FC<ColumnHeaderProps> = (props: ColumnHeaderProps) =>
setPopoverOpen(true);
}
};

if (!column) return null;
return (
<div
Expand Down Expand Up @@ -235,6 +242,8 @@ export const ColumnHeader: FC<ColumnHeaderProps> = (props: ColumnHeaderProps) =>
onClick={handleHeaderCellClick}
onKeyDown={handleHeaderCellKeyDown}
onKeyUp={handleHeaderCellKeyUp}
aria-label={ariaLabel}
aria-sort={ariaSort}
>
<div className={classes.header} data-h-align={column.hAlign}>
<Text
Expand All @@ -254,9 +263,11 @@ export const ColumnHeader: FC<ColumnHeaderProps> = (props: ColumnHeaderProps) =>
style={iconContainerDirectionStyles}
data-component-name={`AnalyticalTableHeaderIconsContainer-${id}`}
>
{isFiltered && <Icon name={iconFilter} />}
{column.isSorted && <Icon name={column.isSortedDesc ? iconSortDescending : iconSortAscending} />}
{column.isGrouped && <Icon name={iconGroup} />}
{isFiltered && <Icon name={iconFilter} aria-hidden />}
{column.isSorted && (
<Icon name={column.isSortedDesc ? iconSortDescending : iconSortAscending} aria-hidden />
)}
{column.isGrouped && <Icon name={iconGroup} aria-hidden />}
</div>
</div>
{hasPopover && popoverOpen && (
Expand Down
40 changes: 36 additions & 4 deletions packages/main/src/components/AnalyticalTable/hooks/useA11y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface UpdatedCellProptypes {
'aria-colindex'?: number;
}

const getCellProps = (cellProps, { cell: { column, row, value }, instance }) => {
const setCellProps = (cellProps, { cell: { column, row, value }, instance }) => {
const columnIndex = instance.visibleColumns.findIndex(({ id }) => id === column.id);
const { alwaysShowSubComponent, renderRowSubComponent, translatableTexts, selectionMode, selectionBehavior } =
instance.webComponentsReactProperties;
Expand All @@ -29,13 +29,18 @@ const getCellProps = (cellProps, { cell: { column, row, value }, instance }) =>

if ((isFirstUserCol && rowIsExpandable) || (row.isGrouped && row.canExpand)) {
updatedCellProps.onKeyDown = row.getToggleRowExpandedProps?.()?.onKeyDown;
let ariaLabel = '';
if (row.isGrouped) {
ariaLabel += translatableTexts.groupedA11yText + ',';
}
if (row.isExpanded) {
updatedCellProps['aria-expanded'] = 'true';
updatedCellProps['aria-label'] = translatableTexts.collapseA11yText;
ariaLabel += ` ${translatableTexts.collapseA11yText}`;
} else {
updatedCellProps['aria-expanded'] = 'false';
updatedCellProps['aria-label'] = translatableTexts.expandA11yText;
ariaLabel += ` ${translatableTexts.expandA11yText}`;
}
updatedCellProps['aria-label'] = ariaLabel;
} else if (
(selectionMode !== AnalyticalTableSelectionMode.None &&
selectionBehavior !== AnalyticalTableSelectionBehavior.RowSelector &&
Expand All @@ -54,7 +59,34 @@ const getCellProps = (cellProps, { cell: { column, row, value }, instance }) =>
return [cellProps, updatedCellProps];
};

const setHeaderProps = (headerProps, { column, instance }) => {
const { translatableTexts } = instance.webComponentsReactProperties;

if (!column) {
return headerProps;
}
const isFiltered = column?.filterValue && column?.filterValue.length > 0;

const updatedProps = {};
if (column.isSorted) {
updatedProps['aria-sort'] = column.isSortedDesc ? 'descending' : 'ascending';
}
if (isFiltered) {
updatedProps['aria-label'] = translatableTexts.filteredA11yText;
}
if (column.isGrouped) {
if (updatedProps['aria-label']) {
updatedProps['aria-label'] += ` ${translatableTexts.groupedA11yText}`;
} else {
updatedProps['aria-label'] = translatableTexts.groupedA11yText;
}
}

return [headerProps, { isFiltered, ...updatedProps }];
};

export const useA11y = (hooks) => {
hooks.getCellProps.push(getCellProps);
hooks.getCellProps.push(setCellProps);
hooks.getHeaderProps.push(setHeaderProps);
};
useA11y.pluginName = 'useA11y';
6 changes: 5 additions & 1 deletion packages/main/src/components/AnalyticalTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import {
COLLAPSE_PRESS_SPACE,
EXPAND_NODE,
EXPAND_PRESS_SPACE,
FILTERED,
GROUPED,
INVALID_TABLE,
SELECT_PRESS_SPACE,
UNSELECT_PRESS_SPACE
Expand Down Expand Up @@ -671,7 +673,9 @@ const AnalyticalTable = forwardRef<AnalyticalTableDomRef, AnalyticalTablePropTyp
selectA11yText: i18nBundle.getText(SELECT_PRESS_SPACE),
unselectA11yText: i18nBundle.getText(UNSELECT_PRESS_SPACE),
expandNodeA11yText: i18nBundle.getText(EXPAND_NODE),
collapseNodeA11yText: i18nBundle.getText(COLLAPSE_NODE)
collapseNodeA11yText: i18nBundle.getText(COLLAPSE_NODE),
filteredA11yText: i18nBundle.getText(FILTERED),
groupedA11yText: i18nBundle.getText(GROUPED)
},
tagNamesWhichShouldNotSelectARow,
tableRef,
Expand Down
6 changes: 6 additions & 0 deletions packages/main/src/i18n/messagebundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ SORT_DESCENDING=Sort Descending
#XTXT
GROUP=Group

#XACT: Aria announcement for grouped table rows
GROUPED=Grouped

#XTXT
UNGROUP=Ungroup

Expand Down Expand Up @@ -246,6 +249,9 @@ UNSELECT_PRESS_SPACE=To unselect the row, press the spacebar
#XACT: Aria label text for an invalid table with overlay
INVALID_TABLE=Invalid Table

#XACT: Aria announcement for filtered table columns
FILTERED=Filtered

#XACT: Aria Role description for Card Headers
ARIA_DESC_CARD_HEADER=Card Header

Expand Down