@builder.io/mitosis 0.3.15 → 0.3.17

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.
@@ -264,10 +264,10 @@ var handleNgOutletBindings = function (node, options) {
264
264
  return allProps;
265
265
  };
266
266
  var blockToAngular = function (_a) {
267
- var _b, _c, _d, _e, _f;
268
- var root = _a.root, json = _a.json, _g = _a.options, options = _g === void 0 ? {} : _g, _h = _a.blockOptions, blockOptions = _h === void 0 ? {
267
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k;
268
+ var root = _a.root, json = _a.json, _l = _a.options, options = _l === void 0 ? {} : _l, _m = _a.blockOptions, blockOptions = _m === void 0 ? {
269
269
  nativeAttributes: [],
270
- } : _h;
270
+ } : _m;
271
271
  var childComponents = (blockOptions === null || blockOptions === void 0 ? void 0 : blockOptions.childComponents) || [];
272
272
  if (mappers[json.name]) {
273
273
  return mappers[json.name](root, json, options);
@@ -292,14 +292,29 @@ var blockToAngular = function (_a) {
292
292
  var str = '';
293
293
  if ((0, mitosis_node_1.checkIsForNode)(json)) {
294
294
  var indexName = json.scope.indexName;
295
- str += "<ng-container *ngFor=\"let ".concat(json.scope.forName, " of ").concat((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code).concat(indexName ? "; let ".concat(indexName, " = index") : '', "\">");
295
+ var forName = json.scope.forName;
296
+ // Check if "key" is present for the first child of the for loop
297
+ if (json.children[0].bindings && ((_c = json.children[0].bindings.key) === null || _c === void 0 ? void 0 : _c.code)) {
298
+ var fnIndex = ((_d = root.meta) === null || _d === void 0 ? void 0 : _d._trackByForIndex) || 0;
299
+ var trackByFnName = "trackBy".concat(forName ? forName.charAt(0).toUpperCase() + forName.slice(1) : '').concat(fnIndex);
300
+ root.meta._trackByForIndex = fnIndex + 1;
301
+ var code = (_e = json.children[0].bindings.key) === null || _e === void 0 ? void 0 : _e.code;
302
+ root.state[trackByFnName] = {
303
+ code: "".concat(trackByFnName, "(").concat(indexName !== null && indexName !== void 0 ? indexName : '_', ", ").concat(forName, ") { return ").concat(code, "; }"),
304
+ type: 'method',
305
+ };
306
+ str += "<ng-container *ngFor=\"let ".concat(forName, " of ").concat((_f = json.bindings.each) === null || _f === void 0 ? void 0 : _f.code).concat(indexName ? "; let ".concat(indexName, " = index") : '', "; trackBy: ").concat(trackByFnName, "\">");
307
+ }
308
+ else {
309
+ str += "<ng-container *ngFor=\"let ".concat(forName, " of ").concat((_g = json.bindings.each) === null || _g === void 0 ? void 0 : _g.code).concat(indexName ? "; let ".concat(indexName, " = index") : '', "\">");
310
+ }
296
311
  str += json.children
297
312
  .map(function (item) { return (0, exports.blockToAngular)({ root: root, json: item, options: options, blockOptions: blockOptions }); })
298
313
  .join('\n');
299
314
  str += "</ng-container>";
300
315
  }
301
316
  else if (json.name === 'Show') {
302
- var condition = (_d = json.bindings.when) === null || _d === void 0 ? void 0 : _d.code;
317
+ var condition = (_h = json.bindings.when) === null || _h === void 0 ? void 0 : _h.code;
303
318
  if (options.state === 'inline-with-wrappers' && (condition === null || condition === void 0 ? void 0 : condition.includes('typeof'))) {
304
319
  var wordAfterTypeof = condition.split('typeof')[1].trim().split(' ')[0];
305
320
  condition = condition.replace("typeof ".concat(wordAfterTypeof), "useTypeOf(".concat(wordAfterTypeof, ")"));
@@ -310,7 +325,7 @@ var blockToAngular = function (_a) {
310
325
  .join('\n');
311
326
  str += "</ng-container>";
312
327
  // else condition
313
- if ((0, is_mitosis_node_1.isMitosisNode)((_e = json.meta) === null || _e === void 0 ? void 0 : _e.else)) {
328
+ if ((0, is_mitosis_node_1.isMitosisNode)((_j = json.meta) === null || _j === void 0 ? void 0 : _j.else)) {
314
329
  str += "<ng-container *ngIf=\"!(".concat(condition, ")\">");
315
330
  str += (0, exports.blockToAngular)({ root: root, json: json.meta.else, options: options, blockOptions: blockOptions });
316
331
  str += "</ng-container>";
@@ -327,7 +342,7 @@ var blockToAngular = function (_a) {
327
342
  code: 'null',
328
343
  type: 'property',
329
344
  };
330
- if (!((_f = root.hooks.onInit) === null || _f === void 0 ? void 0 : _f.code.includes(inputsPropsStateName))) {
345
+ if (!((_k = root.hooks.onInit) === null || _k === void 0 ? void 0 : _k.code.includes(inputsPropsStateName))) {
331
346
  if (!root.hooks.onInit) {
332
347
  root.hooks.onInit = { code: '' };
333
348
  }
@@ -437,6 +452,8 @@ var handleBindings = function (json, item, index, forName, indexName) {
437
452
  if (forName) {
438
453
  if (item.name === 'For')
439
454
  continue;
455
+ if (key === 'key')
456
+ continue;
440
457
  if (key.startsWith('on')) {
441
458
  var _j = item.bindings[key].arguments, cusArgs = _j === void 0 ? ['event'] : _j;
442
459
  var eventBindingName = "".concat(generateNewBindingName(index, item.name), "_event");
@@ -556,6 +573,24 @@ var classPropertiesPlugin = function () { return ({
556
573
  },
557
574
  },
558
575
  }); };
576
+ // if any state "property" is trying to access state.* or props.*
577
+ // then we need to move them to onInit where they can be accessed
578
+ var transformState = function (json) {
579
+ Object.entries(json.state).forEach(function (_a) {
580
+ var _b;
581
+ var key = _a[0], value = _a[1];
582
+ if ((value === null || value === void 0 ? void 0 : value.type) === 'property') {
583
+ if (value.code && (value.code.includes('state.') || value.code.includes('props.'))) {
584
+ var code = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value.code, { replaceWith: 'this' });
585
+ json.state[key].code = 'null';
586
+ if (!((_b = json.hooks.onInit) === null || _b === void 0 ? void 0 : _b.code)) {
587
+ json.hooks.onInit = { code: '' };
588
+ }
589
+ json.hooks.onInit.code += "\nthis.".concat(key, " = ").concat(code, ";\n");
590
+ }
591
+ }
592
+ });
593
+ };
559
594
  var componentToAngular = function (userOptions) {
560
595
  if (userOptions === void 0) { userOptions = {}; }
561
596
  return function (_a) {
@@ -713,6 +748,11 @@ var componentToAngular = function (userOptions) {
713
748
  template = tryFormat(template, 'html');
714
749
  }
715
750
  (0, strip_meta_properties_1.stripMetaProperties)(json);
751
+ var _p = traverseToGetAllDynamicComponents(json, options, {
752
+ childComponents: childComponents,
753
+ nativeAttributes: (_e = (_d = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _d === void 0 ? void 0 : _d.nativeAttributes) !== null && _e !== void 0 ? _e : [],
754
+ }), dynamicComponents = _p.components, dynamicTemplate = _p.dynamicTemplate;
755
+ transformState(json);
716
756
  var dataString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
717
757
  format: 'class',
718
758
  valueMapper: processAngularCode({
@@ -723,10 +763,6 @@ var componentToAngular = function (userOptions) {
723
763
  stateVars: stateVars,
724
764
  }),
725
765
  });
726
- var _p = traverseToGetAllDynamicComponents(json, options, {
727
- childComponents: childComponents,
728
- nativeAttributes: (_e = (_d = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _d === void 0 ? void 0 : _d.nativeAttributes) !== null && _e !== void 0 ? _e : [],
729
- }), dynamicComponents = _p.components, dynamicTemplate = _p.dynamicTemplate;
730
766
  var hostDisplayCss = options.visuallyIgnoreHostElement ? ':host { display: contents; }' : '';
731
767
  var styles = css.length ? [hostDisplayCss, css].join('\n') : hostDisplayCss;
732
768
  // Preparing built in component metadata parameters
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "name": "Builder.io",
23
23
  "url": "https://www.builder.io"
24
24
  },
25
- "version": "0.3.15",
25
+ "version": "0.3.17",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {