Skip to content

Commit

Permalink
fix: can not replace react element with intact element in intact comp…
Browse files Browse the repository at this point in the history
…onent, close ksc-fe/kpc#196
  • Loading branch information
Javey committed Jan 17, 2019
1 parent 53c5f04 commit cf38a64
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
22 changes: 13 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,16 @@ var Wrapper = function () {
// let the component destroy by itself
this.destroyed = true;
// react can use comment node as parent so long as its text like bellow
this.placeholder = document.createComment(commentNodeValue);
var placeholder = this.placeholder = document.createComment(commentNodeValue);
// we should append the placholder advanced,
// because when a intact component update itself
// the _render will update react element sync
if (this.parentDom) {
this.parentDom.appendChild(this.placeholder);
var parentDom = this.parentDom || this.parentVNode && this.parentVNode.dom;
if (parentDom) {
parentDom.appendChild(placeholder);
}
// if the _render is sync, return the result directly
this._render(nextVNode);
return this.placeholder;
return placeholder;
};

Wrapper.prototype.update = function update(lastVNode, nextVNode) {
Expand All @@ -291,7 +291,13 @@ var Wrapper = function () {
var placeholder = this.placeholder;
// let react remove it, intact never remove it
ReactDOM.render(null, placeholder, function () {
placeholder.parentNode.removeChild(placeholder);
var parentDom = placeholder.parentNode;
// get parentNode even if it has been removed
// hack for intact replace child
Object.defineProperty(placeholder, 'parentNode', {
value: parentDom
});
parentDom.removeChild(placeholder);
});
placeholder._unmount = noop;
if (placeholder._realElement) {
Expand Down Expand Up @@ -716,9 +722,7 @@ var IntactReact = function (_Intact) {
dom[internalInstanceKey] = placeholder[internalInstanceKey];
dom[internalEventHandlersKey] = placeholder[internalEventHandlersKey];
Object.defineProperty(placeholder, 'parentNode', {
get: function get$$1() {
return parentElement;
}
value: parentElement
});

parentElement.replaceChild(dom, placeholder);
Expand Down
22 changes: 13 additions & 9 deletions dist/intact.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,16 @@ var Wrapper = function () {
// let the component destroy by itself
this.destroyed = true;
// react can use comment node as parent so long as its text like bellow
this.placeholder = document.createComment(commentNodeValue);
var placeholder = this.placeholder = document.createComment(commentNodeValue);
// we should append the placholder advanced,
// because when a intact component update itself
// the _render will update react element sync
if (this.parentDom) {
this.parentDom.appendChild(this.placeholder);
var parentDom = this.parentDom || this.parentVNode && this.parentVNode.dom;
if (parentDom) {
parentDom.appendChild(placeholder);
}
// if the _render is sync, return the result directly
this._render(nextVNode);
return this.placeholder;
return placeholder;
};

Wrapper.prototype.update = function update(lastVNode, nextVNode) {
Expand All @@ -293,7 +293,13 @@ var Wrapper = function () {
var placeholder = this.placeholder;
// let react remove it, intact never remove it
ReactDOM.render(null, placeholder, function () {
placeholder.parentNode.removeChild(placeholder);
var parentDom = placeholder.parentNode;
// get parentNode even if it has been removed
// hack for intact replace child
Object.defineProperty(placeholder, 'parentNode', {
value: parentDom
});
parentDom.removeChild(placeholder);
});
placeholder._unmount = noop;
if (placeholder._realElement) {
Expand Down Expand Up @@ -718,9 +724,7 @@ var IntactReact = function (_Intact) {
dom[internalInstanceKey] = placeholder[internalInstanceKey];
dom[internalEventHandlersKey] = placeholder[internalEventHandlersKey];
Object.defineProperty(placeholder, 'parentNode', {
get: function get$$1() {
return parentElement;
}
value: parentElement
});

parentElement.replaceChild(dom, placeholder);
Expand Down
2 changes: 1 addition & 1 deletion dist/intact.react.min.js

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/IntactReact.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ class IntactReact extends Intact {
dom[internalInstanceKey] = placeholder[internalInstanceKey];
dom[internalEventHandlersKey] = placeholder[internalEventHandlersKey];
Object.defineProperty(placeholder, 'parentNode', {
get() {
return parentElement;
}
value: parentElement,
});

parentElement.replaceChild(dom, placeholder);
Expand Down
18 changes: 12 additions & 6 deletions src/Wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ export default class Wrapper {
// let the component destroy by itself
this.destroyed = true;
// react can use comment node as parent so long as its text like bellow
this.placeholder = document.createComment(commentNodeValue);
const placeholder = this.placeholder = document.createComment(commentNodeValue);
// we should append the placholder advanced,
// because when a intact component update itself
// the _render will update react element sync
if (this.parentDom) {
this.parentDom.appendChild(this.placeholder);
const parentDom = this.parentDom || this.parentVNode && this.parentVNode.dom;
if (parentDom) {
parentDom.appendChild(placeholder);
}
// if the _render is sync, return the result directly
this._render(nextVNode);
return this.placeholder;
return placeholder;
}

update(lastVNode, nextVNode) {
Expand All @@ -33,7 +33,13 @@ export default class Wrapper {
const placeholder = this.placeholder;
// let react remove it, intact never remove it
ReactDOM.render(null, placeholder, () => {
placeholder.parentNode.removeChild(placeholder);
const parentDom = placeholder.parentNode;
// get parentNode even if it has been removed
// hack for intact replace child
Object.defineProperty(placeholder, 'parentNode', {
value: parentDom
});
parentDom.removeChild(placeholder);
});
placeholder._unmount = noop;
if (placeholder._realElement) {
Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,23 @@ describe('Unit test', function() {
instance.setState({count: 2});
expect(container.innerHTML).to.eql('<div><div>2</div></div>');
});

it('update react children with wrapper element', () => {
const C = createIntactComponent(`
<div>{self.get('wrapper') ?
<div>{self.get('children')}</div> :
self.get('children')
}</div>
`, {defaults() { return {wrapper: true}}});
let c;
const instance = renderApp(function() {
return <C ref={i => c = i}><div>test</div></C>
});
c.set('wrapper', false);
expect(container.innerHTML).to.eql('<div><div>test</div></div>');
c.set('wrapper', true);
expect(container.innerHTML).to.eql('<div><div><div>test</div></div></div>');
});
});

describe('Destroy', () => {
Expand Down

0 comments on commit cf38a64

Please sign in to comment.