Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
adds the ability to change the table role (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinkertrain authored Jun 30, 2022
1 parent 54b0eab commit e815a30
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 87 deletions.
2 changes: 2 additions & 0 deletions src/components/Table/Table.Head.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function TableHead({
selected,
selectionKey,
sortedInfo,
tableRole,
withSelectableRows,
}) {
function handleChange(_, checked) {
Expand Down Expand Up @@ -51,6 +52,7 @@ function TableHead({
column={column}
isLoading={isLoading}
sortedInfo={sortedInfo}
tableRole={tableRole}
/>
))}
</tr>
Expand Down
24 changes: 19 additions & 5 deletions src/components/Table/Table.HeaderCell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,28 @@ import {
generateCustomHeaderCell,
} from './Table.utils'

export default function HeaderCell({ column, isLoading, sortedInfo }) {
export default function HeaderCell({
column,
isLoading,
sortedInfo,
tableRole,
}) {
const sorterBtnId = Array.isArray(column.columnKey)
? `${column.columnKey.join('_')}_${column.sortKey}_sorter`
: `${column.columnKey}_${column.sortKey}_sorter`

function getColumnSortStatus() {
function isTableSortedByThisColumn() {
const colKey = Array.isArray(column.columnKey)
? column.sortKey
: column.columnKey

const isTableSortedByThisColumn =
return (
!isNil(sortedInfo) && sortedInfo.order && sortedInfo.columnKey === colKey
)
}

if (isTableSortedByThisColumn) {
function getColumnSortStatus() {
if (isTableSortedByThisColumn()) {
return sortedInfo.order
}

Expand Down Expand Up @@ -81,6 +89,7 @@ export default function HeaderCell({ column, isLoading, sortedInfo }) {

if (isSortable) {
const columnSortStatus = getColumnSortStatus()
const isSorted = isTableSortedByThisColumn()

return (
<SortableCellUI
Expand All @@ -91,6 +100,11 @@ export default function HeaderCell({ column, isLoading, sortedInfo }) {
)}
>
<SortableCellContentUI
aria-label={
isSorted &&
tableRole !== 'table' &&
`Sorted by ${sortedInfo.columnKey} ${columnSortStatus}`
}
align={column.align}
className={`${TABLE_CLASSNAME}__SortableHeaderCell__title`}
onClick={handleClick}
Expand Down Expand Up @@ -122,7 +136,7 @@ export default function HeaderCell({ column, isLoading, sortedInfo }) {
className={generateCellClassNames(column, 'HeaderCell')}
align={column.align}
cellWidth={column.width}
aria-sort={getColumnSortStatus()}
aria-sort={tableRole === 'table' ? getColumnSortStatus() : null}
>
{renderCellContents()}
</HeaderCellUI>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Table/Table.css.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export const TableUI = styled('table')`
background: linear-gradient(
to right,
${props => props.theme.bgFocusIndicator},
${props => props.theme.bgFocusIndicator} 3px,
transparent 3px,
${props => props.theme.bgFocusIndicator} 2px,
transparent 2px,
transparent 100%
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/components/Table/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function Table({
},
tableClassName,
tableDescription,
tableRole = 'table',
tableWidth = { min: '700px' },
withColumnChooser = false,
withFocusableRows = false,
Expand Down Expand Up @@ -145,6 +146,7 @@ export function Table({
withSelectableRows && 'selection-enabled',
tableClassName
)}
role={tableRole}
tableWidth={tableWidth}
withTallRows={withTallRows}
>
Expand All @@ -160,6 +162,7 @@ export function Table({
state.selectedRows.length === state.currentTableData.length
}
sortedInfo={sortedInfo}
tableRole={tableRole}
withSelectableRows={withSelectableRows}
/>
<TableBody
Expand Down Expand Up @@ -258,6 +261,8 @@ Table.propTypes = {
tableClassName: PropTypes.string,
/** Description of the table contents for accessibility */
tableDescription: PropTypes.string.isRequired,
/** Change the default role of the table */
tableRole: PropTypes.string,
/** The `<table>` width */
tableWidth: PropTypes.shape({ min: PropTypes.string, max: PropTypes.string }),
/** An object to customize the visual appearance of the table. See [Skins.md](/src/components/Table/docs/Skins.md) */
Expand Down
Loading

0 comments on commit e815a30

Please sign in to comment.