Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

Support for attributes, type-extension CEs, and those loaded by HTML import #60

Closed
wants to merge 14 commits into from
Closed
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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
"dist",
"src"
],
"version": "2.0.2"
"version": "2.1.0",
"engines": {
"node": ">=6.0.0"
}
}
39 changes: 27 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,36 @@ export default function (CustomElement, opts) {
this.componentWillReceiveProps(this.props);
}
componentWillReceiveProps(props) {
const node = ReactDOM.findDOMNode(this);
Object.keys(props).forEach(name => {
if (name === 'children' || name === 'style') {
return;
}
const node = ReactDOM.findDOMNode(this);
Object.keys(props).forEach(name => {
if (name === 'children' || name === 'style') {
return;
}

if (name.indexOf('on') === 0 && name[2] === name[2].toUpperCase()) {
syncEvent(node, name.substring(2), props[name]);
} else {
node[name] = props[name];
}
});
const value = props[name];

if (name.indexOf('on') === 0 && name[2] === name[2].toUpperCase()) {
syncEvent(node, name.substring(2), value);
} else if (name.indexOf('attrs') === 0 && value && typeof value === 'object') {
Object.keys(value).forEach(attrName => {
const attrValue = value[attrName];

node.setAttribute(attrName, attrValue);
})
} else {
node[name] = props[name];
}
});
}
render() {
return React.createElement(tagName, { style: this.props.style }, this.props.children);
return React.createElement(
tagName,
{
is: this.props.is,
style: this.props.style
},
this.props.children
);
}
}

Expand Down
1 change: 1 addition & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './unit/attrs';
import './unit/children';
import './unit/events';
import './unit/display-name';
Expand Down
24 changes: 24 additions & 0 deletions test/unit/attrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import reactify from '../../src/index';
import React from 'react';
import ReactDOM from 'react-dom';

let x = 0;

function createComponent() {
const tagName = `x-attributes-${x++}`;

return document.registerElement(tagName, {
prototype: Object.create(HTMLElement.prototype),
})
}

describe('attrs', () => {
it('should pass on properties that start with "attr-" as attributes', () => {
const Comp = reactify(createComponent());
const attrs = {
'data-test': 'test-data'
};
const comp = ReactDOM.render(<Comp attrs={attrs}/>, window.fixture);
expect(ReactDOM.findDOMNode(comp).getAttribute('data-test')).to.equal('test-data');
});
});