Skip to content

Commit

Permalink
Re-render when components prop changes
Browse files Browse the repository at this point in the history
  • Loading branch information
d3x7r0 committed Aug 5, 2019
1 parent 6f74274 commit e465fad
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,27 @@ export default class Markup extends Component {
customReviver = h;
}

shouldComponentUpdate({ wrap, type, markup }) {
let p = this.props;
return wrap!==p.wrap || type!==p.type || markup!==p.markup;
shouldComponentUpdate({ wrap, type, markup, components }) {
const p = this.props;

if (components) {
// we have a list of components, but there wasn't one before:
if (!p.components) {
return true;
}

for (let i in components) {
// one of the components changed:
if (p.components[i]!==components[i]) return true;
}

for (let i in p.components) {
// one of the components was removed:
if (!(i in components)) return true;
}
}

return wrap!==p.wrap || type!==p.type || markup!==p.markup || (!components && p.components);
}

setComponents(components) {
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,43 @@ describe('Markup', () => {
);
});

it('should update when markup changes', () => {
render(
<Markup type="html" markup='<p>hello foo</p>' />,
scratch
);

expect(scratch.textContent).to.eql('hello foo');

render(
<Markup type="html" markup='<p>hello bar</p>' />,
scratch
);

expect(scratch.textContent).to.eql('hello bar');
});

it('should update when mapped components change', () => {
const XFoo = () =>
(<div class="x-foo">hello foo</div>);
const XBar = () =>
(<div class="x-bar">hello bar</div>);

render(
<Markup type="html" markup='<x-component />' components={{ XComponent: XFoo }} />,
scratch
);

expect(scratch.textContent).to.eql('hello foo');

render(
<Markup type="html" markup='<x-component />' components={{ XComponent: XBar }} />,
scratch
);

expect(scratch.textContent).to.eql('hello bar');
});

describe('allow-scripts option', () => {
before( () => {
window.stub = sinon.stub();
Expand Down

0 comments on commit e465fad

Please sign in to comment.