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

Re-render markup when components prop changes #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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, or a new one was added:
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