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

Add data-attributes to DOM | Fixes #700 #702

Merged
merged 4 commits into from
Mar 7, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/clay-component/src/ClayComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ class ClayComponent extends Component {
}
}
}

/**
* @inheritDoc
*/
rendered(...args) {
super.rendered(...args);

for (let dataKey in this.data) {
if (Object.prototype.hasOwnProperty.call(this.data, dataKey)) {
this.element.setAttribute(
'data-' + dataKey,
this.data[dataKey]
);
}
}
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/clay-component/src/__tests__/ClayComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('ClayComponent', function() {
}
});

it('should create a ClayComponent with `virtual` data attributes', () => {
it('should create a ClayComponent with data attributes', () => {
component = new ClayComponent({
data: {
'my-attribute': 'Luke Skywalker',
Expand All @@ -28,6 +28,8 @@ describe('ClayComponent', function() {
expect(component.element.getAttribute('data-my-attribute')).toEqual(
'Luke Skywalker'
);

expect(component).toMatchSnapshot();
});

it('should create a ClayComponent with accesible data attributes from dom', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ClayComponent should create a ClayComponent with data attributes 1`] = `<div data-my-attribute="Luke Skywalker"></div>`;