Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6ff611

Browse files
committed
Added conditional checks to when , , and get attached to headers and rows.
These a11y properties are only added when on-click or sort handlers are present.
1 parent 7ae12ea commit e6ff611

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

source/FlexTable/FlexTable.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ export default class FlexTable extends Component {
122122
disableHeader: false,
123123
headerHeight: 0,
124124
noRowsRenderer: () => null,
125-
onHeaderClick: () => null,
126-
onRowClick: () => null,
127125
onRowsRendered: () => null,
128126
onScroll: () => null,
129127
overscanRowsCount: 10
@@ -285,7 +283,7 @@ export default class FlexTable extends Component {
285283
: SortDirection.DESC
286284
const onClick = () => {
287285
sortEnabled && sort(dataKey, newSortDirection)
288-
onHeaderClick(dataKey, columnData)
286+
onHeaderClick && onHeaderClick(dataKey, columnData)
289287
}
290288

291289
const renderedHeader = headerRenderer({
@@ -297,15 +295,21 @@ export default class FlexTable extends Component {
297295
sortDirection
298296
})
299297

298+
const a11yProps = {}
299+
300+
if (sortEnabled || onHeaderClick) {
301+
a11yProps['aria-label'] = column.props['aria-label'] || label || dataKey
302+
a11yProps.role = 'rowheader'
303+
a11yProps.tabIndex = 0
304+
a11yProps.onClick = onClick
305+
}
306+
300307
return (
301308
<div
302-
aria-label={column.props['aria-label'] || label || dataKey}
309+
{...a11yProps}
303310
key={`Header-Col${columnIndex}`}
304311
className={classNames}
305312
style={style}
306-
onClick={onClick}
307-
role='rowheader'
308-
tabIndex='0'
309313
>
310314
{renderedHeader}
311315
</div>
@@ -334,18 +338,24 @@ export default class FlexTable extends Component {
334338
)
335339
)
336340

341+
const a11yProps = {}
342+
343+
if (onRowClick) {
344+
a11yProps['aria-label'] = 'row'
345+
a11yProps.role = 'row'
346+
a11yProps.tabIndex = 0
347+
a11yProps.onClick = () => onRowClick(rowIndex)
348+
}
349+
337350
return (
338351
<div
339-
aria-label='row'
352+
{...a11yProps}
340353
key={rowIndex}
341354
className={cn('FlexTable__row', rowClass)}
342-
onClick={() => onRowClick(rowIndex)}
343355
style={{
344356
height: this._getRowHeight(rowIndex),
345357
paddingRight: scrollbarWidth
346358
}}
347-
role='row'
348-
tabIndex='0'
349359
>
350360
{renderedRow}
351361
</div>

source/FlexTable/FlexTable.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,4 +669,47 @@ describe('FlexTable', () => {
669669
})
670670
})
671671
})
672+
673+
describe('a11y properties', () => {
674+
it('should attach a11y properties to a row if :onRowClick is specified', () => {
675+
const rendered = findDOMNode(render(getMarkup({
676+
onRowClick: () => {}
677+
})))
678+
const row = rendered.querySelector('.FlexTable__row')
679+
expect(row.getAttribute('aria-label')).toEqual('row')
680+
expect(row.getAttribute('role')).toEqual('row')
681+
expect(row.tabIndex).toEqual(0)
682+
})
683+
684+
it('should not attach a11y properties to a row if no :onRowClick is specified', () => {
685+
const rendered = findDOMNode(render(getMarkup({
686+
onRowClick: null
687+
})))
688+
const row = rendered.querySelector('.FlexTable__row')
689+
expect(row.getAttribute('aria-label')).toEqual(null)
690+
expect(row.getAttribute('role')).toEqual(null)
691+
expect(row.tabIndex).toEqual(-1)
692+
})
693+
694+
it('should attach a11y properties to a header column if sort is enabled', () => {
695+
const rendered = findDOMNode(render(getMarkup({
696+
disableSort: false,
697+
sort: () => {}
698+
})))
699+
const row = rendered.querySelector('.FlexTable__headerColumn')
700+
expect(row.getAttribute('aria-label')).toEqual('Name')
701+
expect(row.getAttribute('role')).toEqual('rowheader')
702+
expect(row.tabIndex).toEqual(0)
703+
})
704+
705+
it('should not attach a11y properties to a header column if sort is not enabled', () => {
706+
const rendered = findDOMNode(render(getMarkup({
707+
disableSort: true
708+
})))
709+
const row = rendered.querySelector('.FlexTable__headerColumn')
710+
expect(row.getAttribute('aria-label')).toEqual(null)
711+
expect(row.getAttribute('role')).toEqual(null)
712+
expect(row.tabIndex).toEqual(-1)
713+
})
714+
})
672715
})

0 commit comments

Comments
 (0)