Skip to content

Commit 0504dc6

Browse files
authored
Corrects the element that aria- props are applied to (#855)
* Corrects element aria- props are applied to * Removes test attribute * Adds headers to demo
1 parent 0f8d2b8 commit 0504dc6

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

docs/demo/aria.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## aria-label
2+
3+
<code src="../examples/aria.tsx">

docs/examples/aria.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import Table from 'rc-table';
3+
import '../../assets/index.less';
4+
5+
const columns = [
6+
{
7+
title: 'Name',
8+
dataIndex: 'name',
9+
key: 'name',
10+
},
11+
{
12+
title: 'Age',
13+
dataIndex: 'age',
14+
key: 'age',
15+
},
16+
{
17+
title: 'Address',
18+
dataIndex: 'address',
19+
key: 'address',
20+
},
21+
];
22+
23+
const data = [
24+
{ name: 'John', age: '25', address: '1 A Street' },
25+
{ name: 'Fred', age: '38', address: '2 B Street' },
26+
{ name: 'Anne', age: '47', address: '3 C Street' },
27+
];
28+
29+
const Demo = () => (
30+
<div>
31+
<h2>Table with aria-label</h2>
32+
<Table columns={columns} data={data} aria-label="Users" data-testid="blah" />
33+
<br />
34+
<h2>Table with aria-labelledby</h2>
35+
<label id="lblPeopleTable">People</label>
36+
<Table columns={columns} data={data} aria-labelledby="lblPeopleTable" />
37+
</div>
38+
);
39+
40+
export default Demo;

src/Table.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,9 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
642642
warning(false, '`components.body` with render props is only work on `scroll.y`.');
643643
}
644644

645+
const dataProps = pickAttrs(props, { data: true });
646+
const ariaProps = pickAttrs(props, { aria: true });
647+
645648
if (fixHeader || isSticky) {
646649
// >>>>>> Fixed Header
647650
let bodyContent: React.ReactNode;
@@ -681,6 +684,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
681684
...scrollTableStyle,
682685
tableLayout: mergedTableLayout,
683686
}}
687+
{...ariaProps}
684688
>
685689
{captionElement}
686690
{bodyColGroup}
@@ -763,7 +767,10 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
763767
onScroll={onScroll}
764768
ref={scrollBodyRef}
765769
>
766-
<TableComponent style={{ ...scrollTableStyle, tableLayout: mergedTableLayout }}>
770+
<TableComponent
771+
style={{ ...scrollTableStyle, tableLayout: mergedTableLayout }}
772+
{...ariaProps}
773+
>
767774
{captionElement}
768775
{bodyColGroup}
769776
{showHeader !== false && <Header {...headerProps} {...columnContext} />}
@@ -778,8 +785,6 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
778785
);
779786
}
780787

781-
const ariaProps = pickAttrs(props, { aria: true, data: true });
782-
783788
let fullTable = (
784789
<div
785790
className={classNames(prefixCls, className, {
@@ -799,7 +804,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
799804
style={style}
800805
id={id}
801806
ref={fullTableRef}
802-
{...ariaProps}
807+
{...dataProps}
803808
>
804809
<MemoTableContent
805810
pingLeft={pingedLeft}

tests/Table.spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,20 @@ describe('Table.Basic', () => {
135135
expect(wrapper.find(`div#${testId}`).length).toBeTruthy();
136136
});
137137

138-
it('renders data- & aria- attributes', () => {
139-
const miscProps = { 'data-test': 'names-table', 'aria-label': 'names-table-aria' };
138+
it('renders data- attributes', () => {
139+
const miscProps = { 'data-test': 'names-table' };
140140
const wrapper = mount(createTable(miscProps));
141141
const props = wrapper.find('div.rc-table').props();
142142
expect(props).toEqual(expect.objectContaining(miscProps));
143143
});
144144

145+
it('renders aria- attributes', () => {
146+
const miscProps = { 'aria-label': 'names-table-aria' };
147+
const wrapper = mount(createTable(miscProps));
148+
const props = wrapper.find('table').props();
149+
expect(props).toEqual(expect.objectContaining(miscProps));
150+
});
151+
145152
describe('rowKey', () => {
146153
it('uses record.key', () => {
147154
const wrapper = mount(createTable());

0 commit comments

Comments
 (0)