Fix code generation for rt-template siblings. (#164)

* Clean up the list of propTemplates fixtures.

* Subtract the child index of a rt-template tag by the number of rt-template tags seen.

Fixes wix/react-templates#163.
This commit is contained in:
Marcus R. Brown 2016-07-21 00:26:18 -07:00 committed by Ido
parent fb3afc9a5b
commit 4a403dffbf
4 changed files with 43 additions and 2 deletions

View File

@ -116,6 +116,7 @@ function generateInjectedFunc(context, namePrefix, body, params) {
}
function generateTemplateProps(node, context) {
let templatePropCount = 0;
const propTemplateDefinition = context.options.propTemplates[node.name];
const propertiesTemplates = _(node.children)
.map((child, index) => {
@ -141,7 +142,7 @@ function generateTemplateProps(node, context) {
}
if (templateProp) {
_.assign(templateProp, {childIndex: index, content: _.find(child.children, {type: 'tag'})});
_.assign(templateProp, {childIndex: index - templatePropCount++, content: _.find(child.children, {type: 'tag'})});
}
return templateProp;

View File

@ -0,0 +1,12 @@
<div>
<rt-template prop="templateProp1" arguments="brother">
<div>Brother: {brother}</div>
</rt-template>
<rt-template prop="templateProp2" arguments="sister">
<div>Sister: {sister}</div>
</rt-template>
<div>Separator</div>
<rt-template prop="templateProp3" arguments="other">
<div>Other: {other}</div>
</rt-template>
</div>

View File

@ -0,0 +1,22 @@
define([
'react',
'lodash'
], function (React, _) {
'use strict';
function templateProp11(brother) {
return React.createElement('div', {}, 'Brother: ', brother);
}
function templateProp22(sister) {
return React.createElement('div', {}, 'Sister: ', sister);
}
function templateProp33(other) {
return React.createElement('div', {}, 'Other: ', other);
}
return function () {
return React.createElement('div', {
'templateProp1': templateProp11.bind(this),
'templateProp2': templateProp22.bind(this),
'templateProp3': templateProp33.bind(this)
}, React.createElement('div', {}, 'Separator'));
};
});

View File

@ -49,7 +49,13 @@ module.exports = {
},
modules: 'amd'
};
const files = ['propTemplates/simpleTemplate.rt', 'propTemplates/templateInScope.rt', 'propTemplates/implicitTemplate.rt', 'propTemplates/twoTemplates.rt'];
const files = [
'simpleTemplate.rt',
'templateInScope.rt',
'implicitTemplate.rt',
'twoTemplates.rt',
'siblingTemplates.rt'
].map(file => path.join('propTemplates', file));
testFiles(t, files, options);
});