Skip to content

Commit

Permalink
Render data- and aria- props (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Klinsing authored and yesmeck committed Oct 15, 2018
1 parent 78783cf commit fe68e44
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Provider, create } from 'mini-store';
import merge from 'lodash/merge';
import classes from 'component-classes';
import { polyfill } from 'react-lifecycles-compat';
import { debounce, warningOnce } from './utils';
import { debounce, warningOnce, getDataAndAriaProps } from './utils';
import ColumnManager from './ColumnManager';
import HeadTable from './HeadTable';
import BodyTable from './BodyTable';
Expand Down Expand Up @@ -502,6 +502,7 @@ class Table extends React.Component {
}
const hasLeftFixed = this.columnManager.isAnyColumnsLeftFixed();
const hasRightFixed = this.columnManager.isAnyColumnsRightFixed();
const dataAndAriaProps = getDataAndAriaProps(props);

return (
<Provider store={this.store}>
Expand All @@ -514,6 +515,7 @@ class Table extends React.Component {
className={className}
style={props.style}
id={props.id}
{...dataAndAriaProps}
>
{this.renderTitle()}
<div className={`${prefixCls}-content`}>
Expand Down
13 changes: 13 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,16 @@ export function remove(array, item) {
const last = array.slice(index + 1, array.length);
return front.concat(last);
}

/**
* Returns only data- and aria- key/value pairs
* @param {object} props
*/
export function getDataAndAriaProps(props) {
return Object.keys(props).reduce((memo, key) => {
if (key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-') {
memo[key] = props[key];
}
return memo;
}, {});
}
10 changes: 10 additions & 0 deletions tests/Table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ describe('Table', () => {
expect(wrapper.find(`div#${testId}`)).toHaveLength(1);
});

it('renders data- attributes', () => {
const wrapper = render(createTable({ 'data-test': 'names-table' }));
expect(wrapper).toMatchSnapshot();
});

it('renders aria- attributes', () => {
const wrapper = render(createTable({ 'aria-label': 'names-table-aria' }));
expect(wrapper).toMatchSnapshot();
});

xit('sets row refs', () => {
const wrapper = mount(createTable({ rowRef: record => record.key }));
expect(wrapper.instance().refs.key0).toBe(
Expand Down
132 changes: 132 additions & 0 deletions tests/__snapshots__/Table.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,72 @@ exports[`Table dataIndex render text by path 1`] = `
</div>
`;

exports[`Table renders aria- attributes 1`] = `
<div
aria-label="names-table-aria"
class="rc-table rc-table-scroll-position-left"
>
<div
class="rc-table-content"
>
<div
class="rc-table-body"
>
<table
class=""
>
<colgroup>
<col />
</colgroup>
<thead
class="rc-table-thead"
>
<tr>
<th
class=""
>
Name
</th>
</tr>
</thead>
<tbody
class="rc-table-tbody"
>
<tr
class="rc-table-row rc-table-row-level-0"
data-row-key="key0"
>
<td
class=""
>
<span
class="rc-table-row-indent indent-level-0"
style="padding-left:0px"
/>
Lucy
</td>
</tr>
<tr
class="rc-table-row rc-table-row-level-0"
data-row-key="key1"
>
<td
class=""
>
<span
class="rc-table-row-indent indent-level-0"
style="padding-left:0px"
/>
Jack
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
`;

exports[`Table renders colSpan correctly 1`] = `
<div
class="rc-table rc-table-scroll-position-left"
Expand Down Expand Up @@ -991,6 +1057,72 @@ exports[`Table renders custom cell correctly 1`] = `
</div>
`;

exports[`Table renders data- attributes 1`] = `
<div
class="rc-table rc-table-scroll-position-left"
data-test="names-table"
>
<div
class="rc-table-content"
>
<div
class="rc-table-body"
>
<table
class=""
>
<colgroup>
<col />
</colgroup>
<thead
class="rc-table-thead"
>
<tr>
<th
class=""
>
Name
</th>
</tr>
</thead>
<tbody
class="rc-table-tbody"
>
<tr
class="rc-table-row rc-table-row-level-0"
data-row-key="key0"
>
<td
class=""
>
<span
class="rc-table-row-indent indent-level-0"
style="padding-left:0px"
/>
Lucy
</td>
</tr>
<tr
class="rc-table-row rc-table-row-level-0"
data-row-key="key1"
>
<td
class=""
>
<span
class="rc-table-row-indent indent-level-0"
style="padding-left:0px"
/>
Jack
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
`;

exports[`Table renders empty text correctly 1`] = `
<div
class="rc-table rc-table-scroll-position-left"
Expand Down
18 changes: 18 additions & 0 deletions tests/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getDataAndAriaProps } from '../src/utils';

describe('utils', () => {
describe('getDataAndAria', () => {
it('should return only data- and aria- properties', () => {
const props = {
'data-test': 'name',
'aria-label': 'name',
dataSource: '/api',
ariaLabel: 'some-label',
};
expect(getDataAndAriaProps(props)).toEqual({
'data-test': 'name',
'aria-label': 'name',
});
});
});
});

0 comments on commit fe68e44

Please sign in to comment.