Skip to content
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

feat(Th): include by default aria-sort for assistive technology #1789

Merged
merged 1 commit into from
Dec 8, 2022
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 @@ -27,12 +27,12 @@ showTabs: true

### Table Header `<Th>`

| Properties | Description |
| ---------- | ----------------------------------------------------------------------------------------------- |
| `sortable` | _(optional)_ defines the table header as sortable if set to `true`. Defaults to `false`. |
| `active` | _(optional)_ defines the sortable column as the current active. Defaults to `false`. |
| `reversed` | _(optional)_ defines the sortable column as in reversed order. Defaults to `false`. |
| `noWrap` | _(optional)_ if set to `true`, the header text will not wrap to new lines. Defaults to `false`. |
| Properties | Description |
| ---------- | ---------------------------------------------------------------------------------------------------- |
| `sortable` | _(optional)_ defines the table header as sortable if set to `true` (ascending). Defaults to `false`. |
| `active` | _(optional)_ defines the sortable column as the current active (ascending). Defaults to `false`. |
| `reversed` | _(optional)_ defines the sortable column as in reversed order (descending). Defaults to `false`. |
| `noWrap` | _(optional)_ if set to `true`, the header text will not wrap to new lines. Defaults to `false`. |

### Table Data `<Td>`

Expand Down
12 changes: 9 additions & 3 deletions packages/dnb-eufemia/src/components/table/TableTh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ export type TableThChildren =

export type TableThProps = {
/**
* Defines the table header as sortable
* Defines the table header as sortable (ascending)
* Default: false
*/
sortable?: boolean

/**
* Defines the sortable column as the current active
* Defines the sortable column as the current active (ascending)
* Default: false
*/
active?: boolean

/**
* Defines the sortable column as in reversed order
* Defines the sortable column as in reversed order (descending)
* Default: false
*/
reversed?: boolean
Expand Down Expand Up @@ -55,11 +55,17 @@ export default function Th(

const role = props.scope === 'row' ? 'rowheader' : 'columnheader'
const scope = props.scope === 'row' ? 'row' : 'col'
const ariaSort = sortable
? reversed
? 'descending'
: 'ascending'
: undefined

return (
<th
role={role}
scope={scope}
aria-sort={ariaSort}
className={classnames(
'dnb-table__th',
sortable && 'dnb-table--sortable',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,56 @@ describe('TableTh', () => {
expect(element.getAttribute('scope')).toBe('row')
})

it('should set correct sortable class', () => {
render(
<table>
<thead>
<tr>
<TableTh sortable>th content</TableTh>
</tr>
</thead>
</table>
)

const element = document.querySelector('th')
expect(Array.from(element.classList)).toContain('dnb-table--sortable')
expect(element.getAttribute('aria-sort')).toBe('ascending')
})

it('should set correct active class', () => {
render(
<table>
<thead>
<tr>
<TableTh active>th content</TableTh>
<TableTh sortable active>
th content
</TableTh>
</tr>
</thead>
</table>
)

const element = document.querySelector('th')
expect(Array.from(element.classList)).toContain('dnb-table--active')
expect(element.getAttribute('aria-sort')).toBe('ascending')
})

it('should set correct reversed class', () => {
render(
<table>
<thead>
<tr>
<TableTh reversed>th content</TableTh>
<TableTh sortable reversed>
th content
</TableTh>
</tr>
</thead>
</table>
)

const element = document.querySelector('th')
expect(Array.from(element.classList)).toContain('dnb-table--reversed')
expect(element.getAttribute('aria-sort')).toBe('descending')
})

it('should set correct noWrap class', () => {
Expand Down