Skip to content

Commit

Permalink
fix(Table): updated a11y for empty/nontext Th components
Browse files Browse the repository at this point in the history
  • Loading branch information
thatblindgeye committed Mar 11, 2024
1 parent 7b860c7 commit c0f926c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
22 changes: 20 additions & 2 deletions packages/react-table/src/components/Table/Th.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ export interface ThProps
stickyRightOffset?: string;
/** Indicates the <th> is part of a subheader of a nested header */
isSubheader?: boolean;
/** Visually hidden text accessible only via assistive technologies. This must be passed in if the
* th is intended to be visually empty, and must be conveyed as a column header text.
*/
screenReaderText?: string;
/** Prpvides an accessible name to the th. This should only be passed in when the th contains only non-text
* content, such as a "select all" checkbox or "expand all" toggle.
*/
'aria-label'?: string;
}

const ThBase: React.FunctionComponent<ThProps> = ({
Expand Down Expand Up @@ -83,8 +91,17 @@ const ThBase: React.FunctionComponent<ThProps> = ({
stickyLeftOffset,
stickyRightOffset,
isSubheader = false,
screenReaderText,
'aria-label': ariaLabel,
...props
}: ThProps) => {
if (!children && !screenReaderText && !ariaLabel) {
// eslint-disable-next-line no-console
console.warn(
'Th: Table headers must have an accessible name. If the Th is intended to be visually empty, pass in screenReaderText. If the Th contains only non-text, interactive content such as a checkbox or expand toggle, pass in an aria-label.'
);
}

const [showTooltip, setShowTooltip] = React.useState(false);
const [truncated, setTruncated] = React.useState(false);
const cellRef = innerRef ? innerRef : React.createRef();
Expand Down Expand Up @@ -188,8 +205,9 @@ const ThBase: React.FunctionComponent<ThProps> = ({
onBlur={() => setShowTooltip(false)}
data-label={dataLabel}
onMouseEnter={tooltip !== null ? onMouseEnter : onMouseEnterProp}
scope={component === 'th' && children ? scope : null}
scope={component === 'th' ? scope : null}
ref={cellRef}
aria-label={ariaLabel}
className={css(
styles.tableTh,
className,
Expand All @@ -212,7 +230,7 @@ const ThBase: React.FunctionComponent<ThProps> = ({
} as React.CSSProperties
})}
>
{transformedChildren}
{transformedChildren || (screenReaderText && <span className="pf-v5-screen-reader">{screenReaderText}</span>)}
</MergedComponent>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export const TableActions: React.FunctionComponent = () => {
<Th>{columnNames.prs}</Th>
<Th>{columnNames.workspaces}</Th>
<Th>{columnNames.lastCommit}</Th>
<Td></Td>
<Td></Td>
<Th screenReaderText="Primary action" />
<Th screenReaderText="Secondary action" />
</Tr>
</Thead>
<Tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const TableActions: React.FunctionComponent = () => {
<Th>{columnNames.prs}</Th>
<Th>{columnNames.workspaces}</Th>
<Th>{columnNames.lastCommit}</Th>
<Td></Td>
<Th />
</Tr>
</Thead>
<Tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ export const TableNestedHeaders: React.FunctionComponent = () => {
{columnNames.destination}
</Th>
</Tr>
{/* TODO: Remove the following Tr once Core updates towards https://github.com/patternfly/patternfly/issues/6272
are made for the v5 branch. For v6 branch the row can be removed immediately.
*/}
<Tr isBorderRow aria-hidden="true">
<Td colSpan={9}></Td>
</Tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const TableExpandable: React.FunctionComponent = () => {
<Table aria-label="Simple table">
<Thead>
<Tr>
<Td />
<Th />
<Th width={20}>{columnNames.name}</Th>
<Th>{columnNames.branches}</Th>
<Th>{columnNames.prs}</Th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const selectable: ITransform = (

return {
className: css(styles.tableCheck),
component: 'td',
component: rowId !== -1 ? 'td' : 'th',
isVisible: !rowData || !rowData.fullWidth,
children: (
<SelectColumn
Expand Down

0 comments on commit c0f926c

Please sign in to comment.