Skip to content

Commit

Permalink
feat(RowList, BoxedRowList, Inline): support list a11y role (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
atabel authored Apr 8, 2024
1 parent 814c297 commit 7e2fe37
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
36 changes: 35 additions & 1 deletion src/__tests__/list-test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import {RowList, Row} from '../list';
import {RowList, Row, BoxedRowList, BoxedRow} from '../list';
import {RadioGroup} from '../radio-button';
import {screen, render, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
Expand All @@ -13,6 +13,40 @@ import {
} from '..';
import {makeTheme} from './test-utils';

test('RowList has a list role by default', () => {
render(
<ThemeContextProvider theme={makeTheme()}>
<RowList>
<Row title="Title" to="/somewhere" />
<Row title="Title 2" />
</RowList>
</ThemeContextProvider>
);

const list = screen.getByRole('list');
expect(list).toBeInTheDocument();

const items = screen.getAllByRole('listitem');
expect(items).toHaveLength(2);
});

test('BoxedRowList has a list role by default', () => {
render(
<ThemeContextProvider theme={makeTheme()}>
<BoxedRowList>
<BoxedRow title="Title" to="/somewhere" />
<BoxedRow title="Title 2" />
</BoxedRowList>
</ThemeContextProvider>
);

const list = screen.getByRole('list');
expect(list).toBeInTheDocument();

const items = screen.getAllByRole('listitem');
expect(items).toHaveLength(2);
});

test('Row which navigates', () => {
render(
<ThemeContextProvider theme={makeTheme()}>
Expand Down
1 change: 1 addition & 0 deletions src/inline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const Inline: React.FC<Props> = ({
{React.Children.map(children, (child) =>
!!child || child === 0 ? (
<div
role={role === 'list' ? 'listitem' : undefined}
style={{
// Hack to fix https://jira.tid.es/browse/WEB-1683
// In iOS the inline component sometimes cuts the last line of the content
Expand Down
19 changes: 14 additions & 5 deletions src/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,13 @@ const RowContent = React.forwardRef<TouchableElement, RowContentProps>((props, r
);
});

export const Row = React.forwardRef<TouchableElement, RowContentProps>(({dataAttributes, ...props}, ref) => (
<RowContent {...props} ref={ref} dataAttributes={{'component-name': 'Row', ...dataAttributes}} />
));
export const Row = React.forwardRef<TouchableElement, RowContentProps>(
({dataAttributes, role = 'listitem', ...props}, ref) => (
<div role={role}>
<RowContent {...props} ref={ref} dataAttributes={{'component-name': 'Row', ...dataAttributes}} />
</div>
)
);

type RowListProps = {
children: React.ReactNode;
Expand All @@ -666,7 +670,12 @@ type RowListProps = {
dataAttributes?: DataAttributes;
};

export const RowList: React.FC<RowListProps> = ({children, ariaLabelledby, role, dataAttributes}) => {
export const RowList: React.FC<RowListProps> = ({
children,
ariaLabelledby,
role = 'list',
dataAttributes,
}) => {
const lastIndex = React.Children.count(children) - 1;
return (
<div
Expand Down Expand Up @@ -733,7 +742,7 @@ type BoxedRowListProps = {
export const BoxedRowList: React.FC<BoxedRowListProps> = ({
children,
ariaLabelledby,
role,
role = 'list',
dataAttributes,
}) => (
<Stack
Expand Down

0 comments on commit 7e2fe37

Please sign in to comment.