Permalink
Browse files

get data-*, aria-*, and role attributes

  • Loading branch information...
Matt Lein authored and afc163 committed Jun 14, 2018
1 parent 2034861 commit f0b684de6a7c422f0de56e1ef72aeb35ab25a858
@@ -1,5 +1,5 @@
import throttleByAnimationFrame from '../throttleByAnimationFrame';
import getDataAttributes from '../getDataAttributes';
import getDataOrAriaProps from '../getDataOrAriaProps';
describe('Test utils function', () => {
beforeAll(() => {
@@ -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' });
});
});
});

This file was deleted.

Oops, something went wrong.
@@ -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.