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

Commit 44b867f

Browse files
authored
Merge pull request #27 from skatejs/26
#26 - Fix style prop
2 parents 621f01c + 709673e commit 44b867f

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export default function (CustomElement, opts) {
4141
static get propTypes() {
4242
return {
4343
children: React.PropTypes.any,
44+
style: React.PropTypes.any,
4445
};
4546
}
4647
componentDidMount() {
@@ -49,7 +50,7 @@ export default function (CustomElement, opts) {
4950
componentWillReceiveProps(props) {
5051
const node = ReactDOM.findDOMNode(this);
5152
Object.keys(props).forEach(name => {
52-
if (name === 'children') {
53+
if (name === 'children' || name === 'style') {
5354
return;
5455
}
5556

@@ -61,7 +62,7 @@ export default function (CustomElement, opts) {
6162
});
6263
}
6364
render() {
64-
return React.createElement(tagName, null, this.props.children);
65+
return React.createElement(tagName, { style: this.props.style }, this.props.children);
6566
}
6667
};
6768
}

test/unit/props.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,69 @@ import React from 'react';
33
import ReactDOM from 'react-dom';
44

55
let x = 0;
6-
function createComponent(name, done) {
6+
function createComponentWithOpts(opts) {
77
return reactify(document.registerElement(`x-props-${x++}`, {
8-
prototype: Object.create(HTMLElement.prototype, {
9-
[name]: {
10-
get() {
11-
return 'test';
12-
},
13-
set(value) {
14-
if (done) {
15-
done(this, value);
16-
}
17-
},
18-
},
19-
}),
8+
prototype: Object.create(HTMLElement.prototype, opts),
209
}), { React, ReactDOM });
2110
}
11+
function createComponentWithProp(name, done) {
12+
return createComponentWithOpts({
13+
[name]: {
14+
get() {
15+
return 'test';
16+
},
17+
set(value) {
18+
if (done) {
19+
done(this, value);
20+
}
21+
},
22+
},
23+
});
24+
}
2225

2326
describe('props', () => {
27+
it('should set style (object)', () => {
28+
const Comp = createComponentWithOpts({});
29+
ReactDOM.render(<Comp style={{ backgroundColor: 'black', width: 1 }} />, window.fixture);
30+
const elem = window.fixture.firstChild;
31+
expect(elem.style.backgroundColor).to.equal('black');
32+
expect(elem.style.width).to.equal('1px');
33+
});
34+
35+
it('should set className', () => {
36+
const Comp = createComponentWithOpts({});
37+
ReactDOM.render(<Comp className="test" />, window.fixture);
38+
const elem = window.fixture.firstChild;
39+
expect(elem.getAttribute('class')).to.equal('test');
40+
});
41+
2442
it('should not set children', () => {
25-
const Comp = createComponent('children', () => { throw new Error('set children'); });
43+
const Comp = createComponentWithProp('children', () => { throw new Error('set children'); });
2644
ReactDOM.render(<Comp><div /></Comp>, window.fixture);
2745
});
2846

2947
it('should not set events', () => {
30-
const Comp = createComponent('oncustomevent', () => { throw new Error('set oncustomevent'); });
48+
const Comp = createComponentWithProp('oncustomevent', () => { throw new Error('set oncustomevent'); });
3149
ReactDOM.render(<Comp oncustomevent="test" />, window.fixture);
3250
});
3351

3452
it('should not set attributes', () => {
35-
const Comp = createComponent('test', elem => expect(elem.hasAttribute('test')).to.equal(false));
53+
const Comp = createComponentWithProp('test', elem => expect(elem.hasAttribute('test')).to.equal(false));
3654
ReactDOM.render(<Comp test="test" />, window.fixture);
3755
});
3856

3957
it('should set properties for anything else', () => {
40-
const Comp = createComponent('test', (elem, value) => expect(value).to.equal('test'));
58+
const Comp = createComponentWithProp('test', (elem, value) => expect(value).to.equal('test'));
4159
ReactDOM.render(<Comp test="test" />, window.fixture);
4260
});
61+
62+
it('should work with rest/spread properties', () => {
63+
const Comp = createComponentWithProp('test');
64+
const rest = { style: { color: 'white' }, test: 'test' };
65+
ReactDOM.render(<Comp className="test" {...rest} />, window.fixture);
66+
const elem = window.fixture.firstChild;
67+
expect(elem.getAttribute('class')).to.equal('test');
68+
expect(elem.style.color).to.equal('white');
69+
expect(elem.test).to.equal('test');
70+
});
4371
});

0 commit comments

Comments
 (0)