@builder.io/mitosis 0.0.56-3 → 0.0.56-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,15 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
13
22
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
24
  };
@@ -28,7 +37,6 @@ var get_state_object_string_1 = require("../helpers/get-state-object-string");
28
37
  var has_component_1 = require("../helpers/has-component");
29
38
  var is_component_1 = require("../helpers/is-component");
30
39
  var is_mitosis_node_1 = require("../helpers/is-mitosis-node");
31
- var replace_idenifiers_1 = require("../helpers/replace-idenifiers");
32
40
  var jsx_1 = require("../parsers/jsx");
33
41
  var strip_state_and_props_refs_1 = require("../helpers/strip-state-and-props-refs");
34
42
  var plugins_1 = require("../modules/plugins");
@@ -49,7 +57,7 @@ var needsSetAttribute = function (key) {
49
57
  var generateSetElementAttributeCode = function (key, useValue) {
50
58
  return needsSetAttribute(key)
51
59
  ? ";el.setAttribute(\"".concat(key, "\", ").concat(useValue, ");")
52
- : ";el.".concat(updateKeyIfException(key), " = ").concat(useValue);
60
+ : ";el.".concat(updateKeyIfException(key), " = ").concat(useValue, ";");
53
61
  };
54
62
  var addUpdateAfterSet = function (json, options) {
55
63
  (0, traverse_1.default)(json).forEach(function (item) {
@@ -64,39 +72,21 @@ var addUpdateAfterSet = function (json, options) {
64
72
  }
65
73
  });
66
74
  };
67
- var getForNames = function (json) {
68
- var names = [];
69
- (0, traverse_1.default)(json).forEach(function (item) {
70
- if ((0, is_mitosis_node_1.isMitosisNode)(item)) {
71
- if (item.name === 'For') {
72
- names.push(item.properties._forName);
73
- }
74
- }
75
- });
76
- return names;
77
- };
78
- var replaceForNameIdentifiers = function (json, options) {
79
- // TODO: cache this. by reference? lru?
80
- var forNames = getForNames(json);
81
- (0, traverse_1.default)(json).forEach(function (item) {
82
- if ((0, is_mitosis_node_1.isMitosisNode)(item)) {
83
- for (var key in item.bindings) {
84
- if (key === 'css' || key === '_forName') {
85
- continue;
86
- }
87
- var value = item.bindings[key];
88
- if (typeof value === 'string') {
89
- item.bindings[key] = (0, replace_idenifiers_1.replaceIdentifiers)(value, forNames, function (name) {
90
- return "".concat(options.format === 'class' ? 'this.' : '', "getContext(el, \"").concat(name, "\")");
91
- });
92
- }
93
- }
94
- }
95
- });
75
+ var addScopeVars = function (parentScopeVars, value, fn) {
76
+ return "".concat(parentScopeVars
77
+ .filter(function (scopeVar) {
78
+ return new RegExp(scopeVar).test(value);
79
+ })
80
+ .map(function (scopeVar) {
81
+ return fn(scopeVar);
82
+ })
83
+ .join('\n'));
96
84
  };
97
85
  var mappers = {
98
- Fragment: function (json, options) {
99
- return json.children.map(function (item) { return blockToHtml(item, options); }).join('\n');
86
+ Fragment: function (json, options, parentScopeVars) {
87
+ return json.children
88
+ .map(function (item) { return blockToHtml(item, options, parentScopeVars); })
89
+ .join('\n');
100
90
  },
101
91
  };
102
92
  var getId = function (json, options) {
@@ -131,7 +121,8 @@ var addOnChangeJs = function (id, options, code) {
131
121
  options.onChangeJsById[id] += code;
132
122
  };
133
123
  // TODO: spread support
134
- var blockToHtml = function (json, options) {
124
+ var blockToHtml = function (json, options, parentScopeVars) {
125
+ if (parentScopeVars === void 0) { parentScopeVars = []; }
135
126
  var hasData = Object.keys(json.bindings).length;
136
127
  var elId = '';
137
128
  if (hasData) {
@@ -139,7 +130,7 @@ var blockToHtml = function (json, options) {
139
130
  json.properties['data-name'] = elId;
140
131
  }
141
132
  if (mappers[json.name]) {
142
- return mappers[json.name](json, options);
133
+ return mappers[json.name](json, options, parentScopeVars);
143
134
  }
144
135
  if ((0, is_children_1.default)(json)) {
145
136
  return "<slot></slot>";
@@ -149,28 +140,41 @@ var blockToHtml = function (json, options) {
149
140
  }
150
141
  if (json.bindings._text) {
151
142
  // TO-DO: textContent might be better performance-wise
152
- addOnChangeJs(elId, options, "el.innerText = ".concat(json.bindings._text, ";"));
153
- return "<span data-name=\"".concat(elId, "\"><!-- ").concat(json.bindings._text.replace(/getContext\(el, "([^"]+)"\)/g, '$1'), " --></span>");
143
+ addOnChangeJs(elId, options, "\n ".concat(addScopeVars(parentScopeVars, json.bindings._text, function (scopeVar) {
144
+ return "const ".concat(scopeVar, " = ").concat(options.format === 'class' ? 'this.' : '', "getContext(el, \"").concat(scopeVar, "\");");
145
+ }), "\n ;el.innerText = ").concat(json.bindings._text, ";"));
146
+ return "<span data-name=\"".concat(elId, "\"><!-- ").concat(json.bindings._text, " --></span>");
154
147
  }
155
148
  var str = '';
156
149
  if (json.name === 'For') {
157
150
  var itemName = json.properties._forName;
151
+ var indexName = json.properties._indexName;
152
+ var collectionName = json.properties._collectionName;
153
+ var scopedVars_1 = __spreadArray(__spreadArray([], parentScopeVars, true), [
154
+ itemName,
155
+ indexName,
156
+ collectionName,
157
+ ], false).filter(Boolean);
158
158
  addOnChangeJs(elId, options,
159
159
  // TODO: be smarter about rendering, deleting old items and adding new ones by
160
160
  // querying dom potentially
161
- "\n let array = ".concat(json.bindings.each, ";\n let template = ").concat(options.format === 'class' ? 'this._root' : 'document', ".querySelector('[data-template-for=\"").concat(elId, "\"]');\n ").concat(options.format === 'class' ? 'this.' : '', "renderLoop(el, array, template, \"").concat(itemName, "\");\n "));
161
+ "\n let array = ".concat(json.bindings.each, ";\n let template = ").concat(options.format === 'class' ? 'this._root' : 'document', ".querySelector('[data-template-for=\"").concat(elId, "\"]');\n ").concat(options.format === 'class' ? 'this.' : '', "renderLoop(el, array, template, ").concat(itemName ? "\"".concat(itemName, "\"") : 'undefined', ", ").concat(indexName ? "\"".concat(indexName, "\"") : 'undefined', ", ").concat(collectionName ? "\"".concat(collectionName, "\"") : 'undefined', ");\n "));
162
162
  // TODO: decide on how to handle this...
163
163
  str += "\n <span data-name=\"".concat(elId, "\"></span>\n <template data-template-for=\"").concat(elId, "\">");
164
164
  if (json.children) {
165
- str += json.children.map(function (item) { return blockToHtml(item, options); }).join('\n');
165
+ str += json.children
166
+ .map(function (item) { return blockToHtml(item, options, scopedVars_1); })
167
+ .join('\n');
166
168
  }
167
169
  str += '</template>';
168
170
  }
169
171
  else if (json.name === 'Show') {
170
- addOnChangeJs(elId, options, "\n if(".concat(json.bindings.when.replace(/;$/, ''), ") {\n \tconst clonedElement = el.content.cloneNode(true);\n const endTag = document.createElement('template');\n endTag.setAttribute('data-show', '").concat(elId, "-end');\n\t\t\t\t\n el.after(endTag);\n el.after(clonedElement)\n } else {\n if (this.querySelector(\"[data-show='").concat(elId, "-end']\") === null) {\n \treturn;\n }\n\n let sibling = el.nextSibling;\n const toBeRemoved = [];\n while (sibling) {\n toBeRemoved.push(sibling);\n if (sibling?.dataset?.show === '").concat(elId, "-end') {\n \ttoBeRemoved.forEach(sib => sib.remove());\n return;\n } \n sibling = sibling.nextSibling;\n }\n } \n "));
172
+ addOnChangeJs(elId, options, "\n const whenCondition = ".concat(json.bindings.when.replace(/;$/, ''), ";\n if (whenCondition) {\n ").concat(options.format === 'class' ? 'this.' : '', "showContent(el)\n }\n "));
171
173
  str += "<template data-name=\"".concat(elId, "\">");
172
174
  if (json.children) {
173
- str += json.children.map(function (item) { return blockToHtml(item, options); }).join('\n');
175
+ str += json.children
176
+ .map(function (item) { return blockToHtml(item, options, parentScopeVars); })
177
+ .join('\n');
174
178
  }
175
179
  str += '</template>';
176
180
  }
@@ -209,18 +213,26 @@ var blockToHtml = function (json, options) {
209
213
  event_1 = 'input';
210
214
  }
211
215
  var fnName = (0, lodash_1.camelCase)("on-".concat(elId, "-").concat(event_1));
216
+ var codeContent = (0, remove_surrounding_block_1.removeSurroundingBlock)(updateReferencesInCode(useValue, options));
212
217
  options.js += "\n // Event handler for '".concat(event_1, "' event on ").concat(elId, "\n ").concat(options.format === 'class'
213
218
  ? "this.".concat(fnName, " = (event) => {")
214
- : "function ".concat(fnName, " (event) {"), "\n ").concat((0, remove_surrounding_block_1.removeSurroundingBlock)(updateReferencesInCode(useValue, options)), "\n }\n ");
219
+ : "function ".concat(fnName, " (event) {"), "\n ").concat(addScopeVars(parentScopeVars, codeContent, function (scopeVar) {
220
+ return "const ".concat(scopeVar, " = ").concat(options.format === 'class' ? 'this.' : '', "getContext(event.currentTarget, \"").concat(scopeVar, "\");");
221
+ }), "\n ").concat(codeContent, "\n }\n ");
215
222
  var fnIdentifier = "".concat(options.format === 'class' ? 'this.' : '').concat(fnName);
216
223
  addOnChangeJs(elId, options, "\n ;el.removeEventListener('".concat(event_1, "', ").concat(fnIdentifier, ");\n ;el.addEventListener('").concat(event_1, "', ").concat(fnIdentifier, ");\n "));
217
224
  }
218
225
  else {
219
226
  if (key === 'style') {
220
- addOnChangeJs(elId, options, ";Object.assign(el.style, ".concat(useValue, ");"));
227
+ addOnChangeJs(elId, options, "\n ".concat(addScopeVars(parentScopeVars, useValue, function (scopeVar) {
228
+ return "const ".concat(scopeVar, " = ").concat(options.format === 'class' ? 'this.' : '', "getContext(el, \"").concat(scopeVar, "\");");
229
+ }), "\n ;Object.assign(el.style, ").concat(useValue, ");"));
221
230
  }
222
231
  else {
223
- addOnChangeJs(elId, options, generateSetElementAttributeCode(key, useValue));
232
+ addOnChangeJs(elId, options, "\n ".concat(addScopeVars(parentScopeVars, useValue, function (scopeVar) {
233
+ // TODO: multiple loops may duplicate variable declarations
234
+ return ";var ".concat(scopeVar, " = ").concat(options.format === 'class' ? 'this.' : '', "getContext(el, \"").concat(scopeVar, "\");");
235
+ }), "\n ").concat(generateSetElementAttributeCode(key, useValue), "\n "));
224
236
  }
225
237
  }
226
238
  }
@@ -229,7 +241,9 @@ var blockToHtml = function (json, options) {
229
241
  }
230
242
  str += '>';
231
243
  if (json.children) {
232
- str += json.children.map(function (item) { return blockToHtml(item, options); }).join('\n');
244
+ str += json.children
245
+ .map(function (item) { return blockToHtml(item, options, parentScopeVars); })
246
+ .join('\n');
233
247
  }
234
248
  if (json.properties.innerHTML) {
235
249
  // Maybe put some kind of safety here for broken HTML such as no close tag
@@ -286,10 +300,10 @@ var componentToHtml = function (options) {
286
300
  if (options.plugins) {
287
301
  json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
288
302
  }
289
- replaceForNameIdentifiers(json, useOptions);
290
303
  addUpdateAfterSet(json, useOptions);
291
304
  var componentHasProps = (0, has_props_1.hasProps)(json);
292
305
  var hasLoop = (0, has_component_1.hasComponent)('For', json);
306
+ var hasShow = (0, has_component_1.hasComponent)('Show', json);
293
307
  if (options.plugins) {
294
308
  json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
295
309
  }
@@ -313,7 +327,7 @@ var componentToHtml = function (options) {
313
327
  valueMapper: function (value) {
314
328
  return addUpdateAfterSetInCode(updateReferencesInCode(value, useOptions), useOptions);
315
329
  },
316
- }), ";\n ").concat(componentHasProps ? "let props = {};" : '', "\n\n ").concat(!hasChangeListeners
330
+ }), ";\n ").concat(componentHasProps ? "let props = {};" : '', "\n let nodesToDestroy = [];\n\n function destroyAnyNodes() {\n // destroy current view template refs before rendering again\n nodesToDestroy.forEach(el => el.remove());\n nodesToDestroy = [];\n }\n ").concat(!hasChangeListeners
317
331
  ? ''
318
332
  : "\n \n // Function to update data bindings and loops\n // call update() when you mutate state and need the updates to reflect\n // in the dom\n function update() {\n ".concat(Object.keys(useOptions.onChangeJsById)
319
333
  .map(function (key) {
@@ -323,16 +337,18 @@ var componentToHtml = function (options) {
323
337
  }
324
338
  return "\n document.querySelectorAll(\"[data-name='".concat(key, "']\").forEach((el, index) => {\n ").concat(value, "\n })\n ");
325
339
  })
326
- .join('\n\n'), "\n\n ").concat(!((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length)
340
+ .join('\n\n'), "\n\n destroyAnyNodes();\n\n ").concat(!((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length)
327
341
  ? ''
328
342
  : "\n ".concat(json.hooks.onUpdate.map(function (hook) {
329
343
  return updateReferencesInCode(hook.code, useOptions);
330
344
  }), " \n "), "\n }\n\n ").concat(useOptions.js, "\n\n // Update with initial state on first load\n update();\n "), "\n\n ").concat(!((_d = json.hooks.onMount) === null || _d === void 0 ? void 0 : _d.code)
331
345
  ? ''
332
346
  : // TODO: make prettier by grabbing only the function body
333
- "\n // onMount\n ".concat(updateReferencesInCode(addUpdateAfterSetInCode(json.hooks.onMount.code, useOptions), useOptions), " \n "), "\n\n ").concat(!hasLoop
347
+ "\n // onMount\n ".concat(updateReferencesInCode(addUpdateAfterSetInCode(json.hooks.onMount.code, useOptions), useOptions), " \n "), "\n ").concat(!hasShow
348
+ ? ''
349
+ : "\n function showContent(el) {\n // https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement/content\n // grabs the content of a node that is between <template> tags\n // iterates through child nodes to register all content including text elements\n // attaches the content after the template\n \n \n const elementFragment = el.content.cloneNode(true);\n const children = Array.from(elementFragment.childNodes)\n children.forEach(child => {\n ".concat(options.format === 'class' ? 'this.' : '', "nodesToDestroy.push(child);\n });\n el.after(elementFragment);\n }\n \n "), "\n ").concat(!hasLoop
334
350
  ? ''
335
- : "\n\n // Helper to render loops\n function renderLoop(el, array, template, itemName) {\n el.innerHTML = '';\n for (let value of array) {\n let tmp = document.createElement('span');\n tmp.innerHTML = template.innerHTML;\n Array.from(tmp.children).forEach(function (child) {\n contextMap.set(child, {\n ...contextMap.get(child),\n [itemName]: value\n });\n el.appendChild(child);\n });\n }\n }\n\n // Helper to pass context down for loops\n let contextMap = new WeakMap();\n function getContext(el, name) {\n let parent = el;\n do {\n let context = contextMap.get(parent);\n if (context && name in context) {\n return context[name];\n }\n } while (parent = parent.parentNode)\n }\n ", "\n })()\n </script>\n ");
351
+ : "\n // Helper to render loops\n function renderLoop(el, array, template, itemName, itemIndex, collectionName) {\n el.innerHTML = \"\";\n for (let [index, value] of array.entries()) {\n let tmp = document.createElement(\"span\");\n tmp.innerHTML = template.innerHTML;\n Array.from(tmp.children).forEach((child) => {\n if (itemName !== undefined) {\n child['__' + itemName] = value;\n }\n if (itemIndex !== undefined) {\n child['__' + itemIndex] = index;\n }\n if (collectionName !== undefined) {\n child['__' + collectionName] = array;\n }\n el.appendChild(child);\n });\n }\n }\n\n function getContext(el, name) {\n do {\n let value = el['__' + name]\n if (value !== undefined) {\n return value\n }\n } while ((el = el.parentNode));\n }\n ", "\n })()\n </script>\n ");
336
352
  }
337
353
  if (options.plugins) {
338
354
  str = (0, plugins_1.runPreCodePlugins)(str, options.plugins);
@@ -373,9 +389,9 @@ var componentToCustomElement = function (options) {
373
389
  json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
374
390
  }
375
391
  var componentHasProps = (0, has_props_1.hasProps)(json);
376
- replaceForNameIdentifiers(json, useOptions);
377
392
  addUpdateAfterSet(json, useOptions);
378
393
  var hasLoop = (0, has_component_1.hasComponent)('For', json);
394
+ var hasShow = (0, has_component_1.hasComponent)('Show', json);
379
395
  if (options.plugins) {
380
396
  json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
381
397
  }
@@ -425,18 +441,18 @@ var componentToCustomElement = function (options) {
425
441
  },
426
442
  }), ";\n ").concat(componentHasProps /* TODO: accept these as attributes/properties on the custom element */
427
443
  ? "this.props = {};"
428
- : '', "\n\n ").concat(!hasLoop
444
+ : '', "\n\n\n // used to keep track of all nodes created by show/for\n this.nodesToDestroy = [];\n\n ").concat(useOptions.js, "\n\n if (").concat((_b = json.meta.useMetadata) === null || _b === void 0 ? void 0 : _b.isAttachedToShadowDom, ") {\n this.attachShadow({ mode: 'open' })\n }\n }\n\n\n ").concat(!((_c = json.hooks.onUnMount) === null || _c === void 0 ? void 0 : _c.code)
429
445
  ? ''
430
- : "\n // Helper to pass context down for loops\n this.contextMap = new WeakMap();\n ", "\n\n ").concat(useOptions.js, "\n\n if (").concat((_b = json.meta.useMetadata) === null || _b === void 0 ? void 0 : _b.isAttachedToShadowDom, ") {\n this.attachShadow({ mode: 'open' })\n }\n }\n\n\n ").concat(!((_c = json.hooks.onUnMount) === null || _c === void 0 ? void 0 : _c.code)
446
+ : "\n disconnectedCallback() {\n // onUnMount\n ".concat(updateReferencesInCode(addUpdateAfterSetInCode(json.hooks.onUnMount.code, useOptions), useOptions), "\n this.destroyAnyNodes(); // clean up nodes when component is destroyed\n }\n "), "\n\n destroyAnyNodes() {\n // destroy current view template refs before rendering again\n this.nodesToDestroy.forEach(el => el.remove());\n this.nodesToDestroy = [];\n }\n\n get _root() {\n return this.shadowRoot || this;\n }\n\n connectedCallback() {\n this._root.innerHTML = `\n ").concat(html, "`;\n this.onMount();\n this.update();\n }\n\n\n ").concat(!hasShow
431
447
  ? ''
432
- : "\n disconnectedCallback() {\n // onUnMount\n ".concat(updateReferencesInCode(addUpdateAfterSetInCode(json.hooks.onUnMount.code, useOptions), useOptions), "\n }\n "), "\n\n get _root() {\n return this.shadowRoot || this;\n }\n\n connectedCallback() {\n this._root.innerHTML = `\n ").concat(html, "`;\n this.onMount();\n this.update();\n }\n\n onMount() {\n ").concat(!((_d = json.hooks.onMount) === null || _d === void 0 ? void 0 : _d.code)
448
+ : "\n showContent(el) {\n // https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement/content\n // grabs the content of a node that is between <template> tags\n // iterates through child nodes to register all content including text elements\n // attaches the content after the template\n \n \n const elementFragment = el.content.cloneNode(true);\n const children = Array.from(elementFragment.childNodes)\n children.forEach(child => {\n this.nodesToDestroy.push(child);\n });\n el.after(elementFragment);\n }", "\n\n onMount() {\n ").concat(!((_d = json.hooks.onMount) === null || _d === void 0 ? void 0 : _d.code)
433
449
  ? ''
434
450
  : // TODO: make prettier by grabbing only the function body
435
451
  "\n // onMount\n ".concat(updateReferencesInCode(addUpdateAfterSetInCode(json.hooks.onMount.code, useOptions), useOptions), "\n "), "\n }\n\n onUpdate() {\n ").concat(!((_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.length)
436
452
  ? ''
437
453
  : "\n ".concat(json.hooks.onUpdate.map(function (hook) {
438
454
  return updateReferencesInCode(hook.code, useOptions);
439
- }), " \n "), " \n }\n\n update() {\n this.onUpdate();\n this.updateBindings();\n }\n\n updateBindings() {\n ").concat(Object.keys(useOptions.onChangeJsById)
455
+ }), " \n "), " \n }\n\n update() {\n this.onUpdate();\n // re-rendering needs to ensure that all nodes generated by for/show are refreshed\n this.destroyAnyNodes();\n this.updateBindings();\n }\n\n updateBindings() {\n ").concat(Object.keys(useOptions.onChangeJsById)
440
456
  .map(function (key) {
441
457
  var value = useOptions.onChangeJsById[key];
442
458
  if (!value) {
@@ -446,7 +462,7 @@ var componentToCustomElement = function (options) {
446
462
  })
447
463
  .join('\n\n'), "\n }\n\n ").concat(!hasLoop
448
464
  ? ''
449
- : "\n\n // Helper to render loops\n renderLoop(el, array, template, itemName) {\n el.innerHTML = '';\n for (let value of array) {\n let tmp = document.createElement('span');\n tmp.innerHTML = template.innerHTML;\n Array.from(tmp.children).forEach((child) => {\n this.contextMap.set(child, {\n ...this.contextMap.get(child),\n [itemName]: value\n });\n el.appendChild(child);\n });\n }\n }\n\n getContext(el, name) {\n let parent = el;\n do {\n let context = this.contextMap.get(parent);\n if (context && name in context) {\n return context[name];\n }\n } while (parent = parent.parentNode || parent.host)\n }\n ", "\n }\n\n customElements.define('").concat(kebabName, "', ").concat(component.name, ");\n ");
465
+ : "\n\n // Helper to render loops\n renderLoop(el, array, template, itemName, itemIndex, collectionName) {\n el.innerHTML = \"\";\n for (let [index, value] of array.entries()) {\n let tmp = document.createElement(\"span\");\n tmp.innerHTML = template.innerHTML;\n Array.from(tmp.children).forEach((child) => {\n if (itemName !== undefined) {\n child['__' + itemName] = value;\n }\n if (itemIndex !== undefined) {\n child['__' + itemIndex] = index;\n }\n if (collectionName !== undefined) {\n child['__' + collectionName] = array;\n }\n el.appendChild(child);\n });\n }\n }\n \n getContext(el, name) {\n do {\n let value = el['__' + name]\n if (value !== undefined) {\n return value\n }\n } while ((el = el.parentNode));\n }\n ", "\n }\n\n customElements.define('").concat(kebabName, "', ").concat(component.name, ");\n ");
450
466
  if (options.plugins) {
451
467
  str = (0, plugins_1.runPreCodePlugins)(str, options.plugins);
452
468
  }
@@ -69,7 +69,9 @@ var NODE_MAPPERS = {
69
69
  },
70
70
  For: function (json, options) {
71
71
  var wrap = wrapInFragment(json);
72
- return "{".concat(processBinding(json.bindings.each, options), "?.map((").concat(json.properties._forName, ", index) => (\n ").concat(wrap ? '<>' : '').concat(json.children
72
+ return "{".concat(processBinding(json.bindings.each, options), "?.map((").concat(json.properties._forName).concat(json.properties._indexName ? ',' + json.properties._indexName : '').concat(json.properties._collectionName
73
+ ? ',' + json.properties._collectionName
74
+ : '', ") => (\n ").concat(wrap ? '<>' : '').concat(json.children
73
75
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
74
76
  .map(function (item) { return (0, exports.blockToReact)(item, options); })
75
77
  .join('\n')).concat(wrap ? '</>' : '', "\n ))}");
@@ -1,2 +1,2 @@
1
1
  import { MitosisComponent } from '../types/mitosis-component';
2
- export declare const renderPreComponent: (component: MitosisComponent, target?: "react" | "template" | "angular" | "builder" | "customElement" | "html" | "mitosis" | "liquid" | "reactNative" | "solid" | "svelte" | "swift" | "webcomponent" | "vue" | undefined) => string;
2
+ export declare const renderPreComponent: (component: MitosisComponent, target?: "react" | "angular" | "builder" | "customElement" | "html" | "mitosis" | "liquid" | "reactNative" | "solid" | "svelte" | "swift" | "template" | "webcomponent" | "vue" | undefined) => string;
@@ -327,6 +327,7 @@ var componentFunctionToJson = function (node, context) {
327
327
  } }));
328
328
  };
329
329
  var jsxElementToJson = function (node) {
330
+ var _a, _b, _c, _d, _e, _f, _g, _h;
330
331
  if (types.isJSXText(node)) {
331
332
  return (0, create_mitosis_node_1.createMitosisNode)({
332
333
  properties: {
@@ -345,6 +346,8 @@ var jsxElementToJson = function (node) {
345
346
  if (types.isArrowFunctionExpression(callback)) {
346
347
  if (types.isIdentifier(callback.params[0])) {
347
348
  var forName = callback.params[0].name;
349
+ var indexName = (_b = (_a = callback.params) === null || _a === void 0 ? void 0 : _a[1]) === null || _b === void 0 ? void 0 : _b.name;
350
+ var collectionName = (_d = (_c = callback.params) === null || _c === void 0 ? void 0 : _c[2]) === null || _d === void 0 ? void 0 : _d.name;
348
351
  return (0, create_mitosis_node_1.createMitosisNode)({
349
352
  name: 'For',
350
353
  bindings: {
@@ -354,6 +357,8 @@ var jsxElementToJson = function (node) {
354
357
  },
355
358
  properties: {
356
359
  _forName: forName,
360
+ _indexName: indexName,
361
+ _collectionName: collectionName,
357
362
  },
358
363
  children: [jsxElementToJson(callback.body)],
359
364
  });
@@ -434,8 +439,10 @@ var jsxElementToJson = function (node) {
434
439
  if (types.isJSXExpressionContainer(child)) {
435
440
  var childExpression = child.expression;
436
441
  if (types.isArrowFunctionExpression(childExpression)) {
437
- var argName = childExpression.params[0]
442
+ var forName = childExpression.params[0]
438
443
  .name;
444
+ var indexName = (_f = (_e = childExpression.params) === null || _e === void 0 ? void 0 : _e[1]) === null || _f === void 0 ? void 0 : _f.name;
445
+ var collectionName = (_h = (_g = childExpression.params) === null || _g === void 0 ? void 0 : _g[2]) === null || _h === void 0 ? void 0 : _h.name;
439
446
  return (0, create_mitosis_node_1.createMitosisNode)({
440
447
  name: 'For',
441
448
  bindings: {
@@ -443,7 +450,9 @@ var jsxElementToJson = function (node) {
443
450
  .value.expression).code,
444
451
  },
445
452
  properties: {
446
- _forName: argName,
453
+ _forName: forName,
454
+ _indexName: indexName,
455
+ _collectionName: collectionName,
447
456
  },
448
457
  children: [jsxElementToJson(childExpression.body)],
449
458
  });