diff --git a/dist/index.js b/dist/index.js index 3def8fc..e8be2ac 100644 --- a/dist/index.js +++ b/dist/index.js @@ -247,8 +247,8 @@ function functionalWrapper(Component) { // invoked by React var vNodes = Component(normalizeProps(props, context, { instance: context.__parent }), true); if (isArray$1(vNodes)) { - return vNodes.map(function (vNode) { - return normalizeIntactVNodeToReactVNode(vNode); + return vNodes.map(function (vNode, index) { + return normalizeIntactVNodeToReactVNode(vNode, index); }); } return normalizeIntactVNodeToReactVNode(vNodes); @@ -274,11 +274,11 @@ function functionalWrapper(Component) { return ret; } -function normalizeIntactVNodeToReactVNode(vNode) { +function normalizeIntactVNodeToReactVNode(vNode, key) { if (isStringOrNumber$1(vNode)) { return vNode; } else if (vNode) { - return createElement(vNode.tag, vNode.props, vNode.props.children || vNode.children); + return createElement(vNode.tag, _extends({ key: key }, vNode.props), vNode.props.children || vNode.children); } } @@ -382,8 +382,15 @@ var Wrapper = function () { // if the parentVNode is a Intact component, it indicates that // the Wrapper node is returned by parent component directly // in this case we must fix the element property of parent component. - // 3 is textNode - var dom = this && this.nodeType === 3 ? this : ReactDOM.findDOMNode(this); + var dom = void 0; + if (this) { + dom = this.nodeType === 3 /* TextNode */ ? this : ReactDOM.findDOMNode(this); + } else { + // maybe this element is wrapped by Provider + // and we can not get the instance + // but the real element is inserted before the placeholder by React + dom = placeholder.previousSibling; + } placeholder._realElement = dom; resolve(); }); diff --git a/dist/intact.react.js b/dist/intact.react.js index 82982be..3252fd6 100644 --- a/dist/intact.react.js +++ b/dist/intact.react.js @@ -249,8 +249,8 @@ function functionalWrapper(Component) { // invoked by React var vNodes = Component(normalizeProps(props, context, { instance: context.__parent }), true); if (isArray$1(vNodes)) { - return vNodes.map(function (vNode) { - return normalizeIntactVNodeToReactVNode(vNode); + return vNodes.map(function (vNode, index) { + return normalizeIntactVNodeToReactVNode(vNode, index); }); } return normalizeIntactVNodeToReactVNode(vNodes); @@ -276,11 +276,11 @@ function functionalWrapper(Component) { return ret; } -function normalizeIntactVNodeToReactVNode(vNode) { +function normalizeIntactVNodeToReactVNode(vNode, key) { if (isStringOrNumber$1(vNode)) { return vNode; } else if (vNode) { - return createElement(vNode.tag, vNode.props, vNode.props.children || vNode.children); + return createElement(vNode.tag, _extends({ key: key }, vNode.props), vNode.props.children || vNode.children); } } @@ -384,8 +384,15 @@ var Wrapper = function () { // if the parentVNode is a Intact component, it indicates that // the Wrapper node is returned by parent component directly // in this case we must fix the element property of parent component. - // 3 is textNode - var dom = this && this.nodeType === 3 ? this : ReactDOM.findDOMNode(this); + var dom = void 0; + if (this) { + dom = this.nodeType === 3 /* TextNode */ ? this : ReactDOM.findDOMNode(this); + } else { + // maybe this element is wrapped by Provider + // and we can not get the instance + // but the real element is inserted before the placeholder by React + dom = placeholder.previousSibling; + } placeholder._realElement = dom; resolve(); }); diff --git a/dist/intact.react.min.js b/dist/intact.react.min.js index 7fe7752..71ff1a3 100644 --- a/dist/intact.react.min.js +++ b/dist/intact.react.min.js @@ -1 +1 @@ -(function(t,e){typeof exports==="object"&&typeof module!=="undefined"?module.exports=e(require("react"),require("intact/dist"),require("react-dom")):typeof define==="function"&&define.amd?define(["react","intact/dist","react-dom"],e):t.Intact=e(t.React,t.Intact,t.ReactDOM)})(this,function(t,e,r){"use strict";t=t&&t.hasOwnProperty("default")?t["default"]:t;e=e&&e.hasOwnProperty("default")?e["default"]:e;r=r&&r.hasOwnProperty("default")?r["default"]:r;var n=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(t){return typeof t}:function(t){return t&&typeof Symbol==="function"&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};var i=function(t,e){if(!(t instanceof e)){throw new TypeError("Cannot call a class as a function")}};var o=function(){function t(t,e){for(var r=0;r1?r-1:0),i=1;i1&&arguments[1]!==undefined?arguments[1]:{};if(Q(t)){var r=[];t.forEach(function(t){if(Q(t)){r.push.apply(r,P(t,e))}else{r.push(I(t,e))}});return r}return I(t,e)}function S(t,e,r,n){if(!t)return;var i={};var o=i._blocks={};var a=void 0;for(var s in t){if(s==="children"){i.children=P(t.children,r)}else if(a=D(s)){i[a]=t[s]}else if(s.substring(0,2)==="b-"){o[s.substring(2)]=A(t[s])}else if(s==="forwardRef"){i.ref=t[s]}else{i[s]=t[s]}}i._context=U(e);if(n!=null){i.key=n}return i}function F(t){return(typeof t==="undefined"?"undefined":n(t))==="object"&&t!==null?function(e){t.current=e}:t}function U(t){var e=t._context;return{data:{get:function t(r){if(r!=null){return j(e.state||{},r)}else{return e.state||{}}},set:function t(r,n){var i=a({},e.state);$(i,r,n);e.setState(i)}}}}function A(t){if(V(t)){return function(e){for(var r=arguments.length,n=Array(r>1?r-1:0),i=1;i=65&&i<=90){return"ev-"+t.substring(2).toLowerCase().replace(/\-/g,":")}}}var G=e.utils;var q=G.isObject;var B=G.extend;var L=e.Vdt.miss.h;var W=void 0;var z=void 0;var Z=[];var H=function(e){s(r,e);function r(t,n){i(this,r);if(n){var o={};var a=S(t,n,o);var s=u(this,e.call(this,a));o.instance=s;s.__promises=n.__promises||Z;s.mountedQueue=n.__parent&&n.__parent.mountedQueue;s.vNode=L(s.constructor,a);s.vNode.children=s;s._props=s.props;delete s.props;s._isReact=true}else{var s=u(this,e.call(this,t))}s._triggerFlagStack=[];var p=void 0;Object.defineProperty(s,"element",{get:function t(){if(!this.__isUpdating&&p&&p.nodeType===8&&p.nodeValue===x){return p._realElement||p}return p},set:function t(e){p=e},configurable:true,enumerable:true});return u(s)}r.prototype.getChildContext=function t(){return{__parent:this,__promises:this.__promises}};r.prototype.get=function t(){for(var r=arguments.length,n=Array(r),i=0;i1?r-1:0),i=1;i1&&arguments[1]!==undefined?arguments[1]:{};if(Q(t)){var r=[];t.forEach(function(t){if(Q(t)){r.push.apply(r,S(t,e))}else{r.push(I(t,e))}});return r}return I(t,e)}function P(t,e,r,n){if(!t)return;var i={};var o=i._blocks={};var a=void 0;for(var s in t){if(s==="children"){i.children=S(t.children,r)}else if(a=D(s)){i[a]=t[s]}else if(s.substring(0,2)==="b-"){o[s.substring(2)]=A(t[s])}else if(s==="forwardRef"){i.ref=t[s]}else{i[s]=t[s]}}i._context=U(e);if(n!=null){i.key=n}return i}function F(t){return(typeof t==="undefined"?"undefined":n(t))==="object"&&t!==null?function(e){t.current=e}:t}function U(t){var e=t._context;return{data:{get:function t(r){if(r!=null){return j(e.state||{},r)}else{return e.state||{}}},set:function t(r,n){var i=a({},e.state);$(i,r,n);e.setState(i)}}}}function A(t){if(V(t)){return function(e){for(var r=arguments.length,n=Array(r>1?r-1:0),i=1;i=65&&i<=90){return"ev-"+t.substring(2).toLowerCase().replace(/\-/g,":")}}}var G=e.utils;var q=G.isObject;var B=G.extend;var L=e.Vdt.miss.h;var W=void 0;var z=void 0;var Z=[];var H=function(e){s(r,e);function r(t,n){i(this,r);if(n){var o={};var a=P(t,n,o);var s=u(this,e.call(this,a));o.instance=s;s.__promises=n.__promises||Z;s.mountedQueue=n.__parent&&n.__parent.mountedQueue;s.vNode=L(s.constructor,a);s.vNode.children=s;s._props=s.props;delete s.props;s._isReact=true}else{var s=u(this,e.call(this,t))}s._triggerFlagStack=[];var p=void 0;Object.defineProperty(s,"element",{get:function t(){if(!this.__isUpdating&&p&&p.nodeType===8&&p.nodeValue===x){return p._realElement||p}return p},set:function t(e){p=e},configurable:true,enumerable:true});return u(s)}r.prototype.getChildContext=function t(){return{__parent:this,__promises:this.__promises}};r.prototype.get=function t(){for(var r=arguments.length,n=Array(r),i=0;i { - return normalizeIntactVNodeToReactVNode(vNode); + return vNodes.map((vNode, index) => { + return normalizeIntactVNodeToReactVNode(vNode, index); }); } return normalizeIntactVNodeToReactVNode(vNodes); @@ -40,13 +40,13 @@ export default function functionalWrapper(Component) { return ret; } -export function normalizeIntactVNodeToReactVNode(vNode) { +export function normalizeIntactVNodeToReactVNode(vNode, key) { if (isStringOrNumber(vNode)) { return vNode; } else if (vNode) { return createElement( vNode.tag, - vNode.props, + {key, ...vNode.props}, vNode.props.children || vNode.children ); } diff --git a/test/index.js b/test/index.js index 81dff8e..2425bc2 100644 --- a/test/index.js +++ b/test/index.js @@ -122,7 +122,7 @@ describe('Unit test', function() { it('render nested array children', () => { render( - {[1, 2].map(item =>
{item}
)} + {[1, 2].map(item =>
{item}
)}
3
); @@ -206,6 +206,9 @@ describe('Unit test', function() { }); render(testtest); expect(container.innerHTML).to.eql('
testtest
text
test'); + + render(
test1test2
); + expect(container.innerHTML).to.eql('
test1
text
test
test2
text
test
'); }); it('render intact functional component with forwardRef', () => { @@ -425,8 +428,8 @@ describe('Unit test', function() { const instance = renderApp(function() { return (
- {this.state.list.map(item => { - return {item} + {this.state.list.map((item, index) => { + return {item} })}
); @@ -442,8 +445,8 @@ describe('Unit test', function() { const instance = renderApp(function() { return ( - {this.state.list.map(item => { - return
{item}
+ {this.state.list.map((item, index) => { + return
{item}
})}
) @@ -1238,4 +1241,23 @@ describe('Unit test', function() { ); expect(container.innerHTML).to.eq('
c
'); }); + + it('should get element that element nested new context api', () => { + const Context = React.createContext(); + const Component = createIntactComponent( + ``, + { + _mount() { + expect(this.element.outerHTML).to.eql('
test
'); + }, + } + ); + render( + + +
test
+
+
+ ); + }); });