Skip to content

Commit

Permalink
fix(tag): fix ability to pass data attributes to the tags
Browse files Browse the repository at this point in the history
  • Loading branch information
zouxuoz committed Jul 20, 2018
1 parent d6b6c26 commit 3495dce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/common/Tag/Tag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import pick from 'lodash/pick';
import pickBy from 'lodash/pickBy';

type TagProps = {|
activeClassName?: string,
Expand Down Expand Up @@ -66,9 +66,11 @@ const COLLECTED_PROPS = [
'value',
];

const collectProps = (props) => pickBy(props, (value, name) => COLLECTED_PROPS.indexOf(name) !== -1 || /^data-/.test(name));

function Tag({ tagName, ...props }: TagProps) {
const TagComponent = tagName;
const collectedProps = pick(props, COLLECTED_PROPS);
const collectedProps = collectProps(props);

return <TagComponent { ...collectedProps } />;
}
Expand Down
6 changes: 6 additions & 0 deletions src/common/Tag/__tests__/Tag-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ it('As developer, I can use tag for collect common props.', () => {

expect(tree.toJSON()).toMatchSnapshot();
});

it('As developer, I can pass data attributes to the tag.', () => {
const tree = renderer.create(<Tag tagName="div" data-attr={ 10 } />);

expect(tree.toJSON()).toMatchSnapshot();
});
6 changes: 6 additions & 0 deletions src/common/Tag/__tests__/__snapshots__/Tag-test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`As developer, I can pass data attributes to the tag. 1`] = `
<div
data-attr={10}
/>
`;

exports[`As developer, I can use tag for collect common props. 1`] = `
<div
onClick={[Function]}
Expand Down

0 comments on commit 3495dce

Please sign in to comment.