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

Corrects the element that aria- props are applied to #855

Merged
merged 4 commits into from Dec 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/demo/aria.md
@@ -0,0 +1,3 @@
## aria-label

<code src="../examples/aria.tsx">
40 changes: 40 additions & 0 deletions docs/examples/aria.tsx
@@ -0,0 +1,40 @@
import React from 'react';
import Table from 'rc-table';
import '../../assets/index.less';

const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
},
{
title: 'Address',
dataIndex: 'address',
key: 'address',
},
];

const data = [
{ name: 'John', age: '25', address: '1 A Street' },
{ name: 'Fred', age: '38', address: '2 B Street' },
{ name: 'Anne', age: '47', address: '3 C Street' },
];

const Demo = () => (
<div>
<h2>Table with aria-label</h2>
<Table columns={columns} data={data} aria-label="Users" data-testid="blah" />
<br />
<h2>Table with aria-labelledby</h2>
<label id="lblPeopleTable">People</label>
<Table columns={columns} data={data} aria-labelledby="lblPeopleTable" />
</div>
);

export default Demo;
13 changes: 9 additions & 4 deletions src/Table.tsx
Expand Up @@ -635,6 +635,9 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
warning(false, '`components.body` with render props is only work on `scroll.y`.');
}

const dataProps = pickAttrs(props, { data: true });
const ariaProps = pickAttrs(props, { aria: true });

if (fixHeader || isSticky) {
// >>>>>> Fixed Header
let bodyContent: React.ReactNode;
Expand Down Expand Up @@ -674,6 +677,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
...scrollTableStyle,
tableLayout: mergedTableLayout,
}}
{...ariaProps}
>
{captionElement}
{bodyColGroup}
Expand Down Expand Up @@ -756,7 +760,10 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
onScroll={onScroll}
ref={scrollBodyRef}
>
<TableComponent style={{ ...scrollTableStyle, tableLayout: mergedTableLayout }}>
<TableComponent
style={{ ...scrollTableStyle, tableLayout: mergedTableLayout }}
{...ariaProps}
>
{captionElement}
{bodyColGroup}
{showHeader !== false && <Header {...headerProps} {...columnContext} />}
Expand All @@ -771,8 +778,6 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
);
}

const ariaProps = pickAttrs(props, { aria: true, data: true });

let fullTable = (
<div
className={classNames(prefixCls, className, {
Expand All @@ -792,7 +797,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
style={style}
id={id}
ref={fullTableRef}
{...ariaProps}
{...dataProps}
>
<MemoTableContent
pingLeft={pingedLeft}
Expand Down
11 changes: 9 additions & 2 deletions tests/Table.spec.js
Expand Up @@ -135,13 +135,20 @@ describe('Table.Basic', () => {
expect(wrapper.find(`div#${testId}`).length).toBeTruthy();
});

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

it('renders aria- attributes', () => {
const miscProps = { 'aria-label': 'names-table-aria' };
const wrapper = mount(createTable(miscProps));
const props = wrapper.find('table').props();
expect(props).toEqual(expect.objectContaining(miscProps));
});

describe('rowKey', () => {
it('uses record.key', () => {
const wrapper = mount(createTable());
Expand Down