Skip to content

Commit

Permalink
get data-*, aria-*, and role attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Lein authored and afc163 committed Jun 25, 2018
1 parent 2034861 commit f0b684d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
41 changes: 38 additions & 3 deletions components/_util/__tests__/util.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import throttleByAnimationFrame from '../throttleByAnimationFrame';
import getDataAttributes from '../getDataAttributes';
import getDataOrAriaProps from '../getDataOrAriaProps';

describe('Test utils function', () => {
beforeAll(() => {
Expand Down Expand Up @@ -34,19 +34,54 @@ describe('Test utils function', () => {
expect(callback).not.toBeCalled();
});

describe('getDataAttributes', () => {
describe('getDataOrAriaProps', () => {
it('returns all data-* properties from an object', () => {
const props = {
onClick: () => {},
isOpen: true,
'data-test': 'test-id',
'data-id': 1234,
};
const results = getDataAttributes(props);
const results = getDataOrAriaProps(props);
expect(results).toEqual({
'data-test': 'test-id',
'data-id': 1234,
});
});

it('does not return data-__ properties from an object', () => {
const props = {
onClick: () => {},
isOpen: true,
'data-__test': 'test-id',
'data-__id': 1234,
};
const results = getDataOrAriaProps(props);
expect(results).toEqual({});
});

it('returns all aria-* properties from an object', () => {
const props = {
onClick: () => {},
isOpen: true,
'aria-labelledby': 'label-id',
'aria-label': 'some-label',
};
const results = getDataOrAriaProps(props);
expect(results).toEqual({
'aria-labelledby': 'label-id',
'aria-label': 'some-label',
});
});

it('returns role property from an object', () => {
const props = {
onClick: () => {},
isOpen: true,
role: 'search',
};
const results = getDataOrAriaProps(props);
expect(results).toEqual({ role: 'search' });
});
});
});
8 changes: 0 additions & 8 deletions components/_util/getDataAttributes.ts

This file was deleted.

11 changes: 11 additions & 0 deletions components/_util/getDataOrAriaProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function getDataOrAriaProps(props: any) {
return Object.keys(props).reduce((prev: any, key: string) => {
if (
(key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-' || key === 'role') &&
key.substr(0, 7) !== 'data-__'
) {
prev[key] = props[key];
}
return prev;
}, {});
}

0 comments on commit f0b684d

Please sign in to comment.