@aemforms/af-core 0.22.101 → 0.22.102

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.
@@ -2043,7 +2043,19 @@ class Container extends Scriptable {
2043
2043
  if ((action.type === 'addItem' || action.type == 'addInstance') && this._itemTemplate != null) {
2044
2044
  if ((this._jsonModel.maxItems === -1) || (this._children.length < this._jsonModel.maxItems)) {
2045
2045
  const dataNode = this.getDataNode();
2046
- let instanceIndex = action.payload;
2046
+ let instanceIndex = null;
2047
+ let instanceData = null;
2048
+ if (typeof action.payload === 'number') {
2049
+ instanceIndex = action.payload;
2050
+ }
2051
+ else if (typeof action.payload === 'object') {
2052
+ if ('index' in action.payload) {
2053
+ instanceIndex = action.payload.index;
2054
+ }
2055
+ if ('data' in action.payload) {
2056
+ instanceData = action.payload.data;
2057
+ }
2058
+ }
2047
2059
  const retVal = this._addChild(this._itemTemplate, action.payload, true);
2048
2060
  if (typeof instanceIndex !== 'number' || instanceIndex > this._children.length) {
2049
2061
  instanceIndex = this._children.length;
@@ -2059,6 +2071,9 @@ class Container extends Scriptable {
2059
2071
  for (let i = instanceIndex + 1; i < this._children.length; i++) {
2060
2072
  this._children[i].dispatch(new ExecuteRule());
2061
2073
  }
2074
+ if (instanceData) {
2075
+ retVal.importData(instanceData);
2076
+ }
2062
2077
  }
2063
2078
  }
2064
2079
  }
@@ -2112,14 +2127,21 @@ class Container extends Scriptable {
2112
2127
  dispatch(action) {
2113
2128
  super.dispatch(action);
2114
2129
  }
2130
+ createDataGroup(dataModel) {
2131
+ const dataGroup = new DataGroup(this._data.$name, dataModel, this._data.$type, this._data.parent);
2132
+ try {
2133
+ this._data.parent?.$addDataNode(dataGroup.$name, dataGroup, true);
2134
+ }
2135
+ catch (e) {
2136
+ this.form.logger.error(`Unable to set items for container '${this.qualifiedName}' with data model '${dataModel}': ${e.message}`);
2137
+ return null;
2138
+ }
2139
+ return dataGroup;
2140
+ }
2115
2141
  importData(dataModel) {
2116
2142
  if (typeof this._data !== 'undefined' && this.type === 'array' && Array.isArray(dataModel)) {
2117
- const dataGroup = new DataGroup(this._data.$name, dataModel, this._data.$type, this._data.parent);
2118
- try {
2119
- this._data.parent?.$addDataNode(dataGroup.$name, dataGroup, true);
2120
- }
2121
- catch (e) {
2122
- this.form.logger.error(`unable to setItems for ${this.qualifiedName} : ${e}`);
2143
+ const dataGroup = this.createDataGroup(dataModel);
2144
+ if (!dataGroup) {
2123
2145
  return;
2124
2146
  }
2125
2147
  this._data = dataGroup;
@@ -2136,6 +2158,13 @@ class Container extends Scriptable {
2136
2158
  this.notifyDependents(propertyChange('items', null, item.getState()));
2137
2159
  });
2138
2160
  }
2161
+ if (typeof this._data !== 'undefined' && this.type === 'object') {
2162
+ const dataGroup = this.createDataGroup(dataModel);
2163
+ if (!dataGroup) {
2164
+ return;
2165
+ }
2166
+ this.syncDataAndFormModel(dataGroup);
2167
+ }
2139
2168
  }
2140
2169
  syncDataAndFormModel(contextualDataModel) {
2141
2170
  const result = {
@@ -64,6 +64,7 @@ declare abstract class Container<T extends ContainerJson & RulesJson> extends Sc
64
64
  reset(): void;
65
65
  validate(): import("./types/Model").ValidationError[];
66
66
  dispatch(action: Action): void;
67
+ createDataGroup(dataModel: any): DataGroup | null;
67
68
  importData(dataModel: any): void;
68
69
  syncDataAndFormModel(contextualDataModel?: DataGroup): any;
69
70
  get activeChild(): BaseModel | null;
@@ -32,6 +32,10 @@ export type UIChangePayload = {
32
32
  value?: any;
33
33
  checked?: boolean;
34
34
  };
35
+ export type AddItemPayload = {
36
+ index: number;
37
+ data: Record<string, any>;
38
+ };
35
39
  export declare class Change extends ActionImpl {
36
40
  constructor(payload: ChangePayload, dispatch?: boolean);
37
41
  withAdditionalChange(change: Change): Change;
@@ -93,13 +97,13 @@ export declare class CustomEvent extends ActionImpl {
93
97
  get isCustomEvent(): boolean;
94
98
  }
95
99
  export declare class AddItem extends ActionImpl {
96
- constructor(payload?: number);
100
+ constructor(payload?: number | AddItemPayload);
97
101
  }
98
102
  export declare class RemoveItem extends ActionImpl {
99
103
  constructor(payload?: number);
100
104
  }
101
105
  export declare class AddInstance extends ActionImpl {
102
- constructor(payload?: number);
106
+ constructor(payload?: number | AddItemPayload);
103
107
  }
104
108
  export declare class RemoveInstance extends ActionImpl {
105
109
  constructor(payload?: number);
@@ -64,6 +64,7 @@ declare abstract class Container<T extends ContainerJson & RulesJson> extends Sc
64
64
  reset(): void;
65
65
  validate(): import("./types/Model").ValidationError[];
66
66
  dispatch(action: Action): void;
67
+ createDataGroup(dataModel: any): DataGroup | null;
67
68
  importData(dataModel: any): void;
68
69
  syncDataAndFormModel(contextualDataModel?: DataGroup): any;
69
70
  get activeChild(): BaseModel | null;
package/lib/Container.js CHANGED
@@ -275,7 +275,19 @@ class Container extends Scriptable_1.default {
275
275
  if ((action.type === 'addItem' || action.type == 'addInstance') && this._itemTemplate != null) {
276
276
  if ((this._jsonModel.maxItems === -1) || (this._children.length < this._jsonModel.maxItems)) {
277
277
  const dataNode = this.getDataNode();
278
- let instanceIndex = action.payload;
278
+ let instanceIndex = null;
279
+ let instanceData = null;
280
+ if (typeof action.payload === 'number') {
281
+ instanceIndex = action.payload;
282
+ }
283
+ else if (typeof action.payload === 'object') {
284
+ if ('index' in action.payload) {
285
+ instanceIndex = action.payload.index;
286
+ }
287
+ if ('data' in action.payload) {
288
+ instanceData = action.payload.data;
289
+ }
290
+ }
279
291
  const retVal = this._addChild(this._itemTemplate, action.payload, true);
280
292
  if (typeof instanceIndex !== 'number' || instanceIndex > this._children.length) {
281
293
  instanceIndex = this._children.length;
@@ -291,6 +303,9 @@ class Container extends Scriptable_1.default {
291
303
  for (let i = instanceIndex + 1; i < this._children.length; i++) {
292
304
  this._children[i].dispatch(new Events_1.ExecuteRule());
293
305
  }
306
+ if (instanceData) {
307
+ retVal.importData(instanceData);
308
+ }
294
309
  }
295
310
  }
296
311
  }
@@ -345,15 +360,22 @@ class Container extends Scriptable_1.default {
345
360
  dispatch(action) {
346
361
  super.dispatch(action);
347
362
  }
348
- importData(dataModel) {
363
+ createDataGroup(dataModel) {
349
364
  var _a;
365
+ const dataGroup = new DataGroup_1.default(this._data.$name, dataModel, this._data.$type, this._data.parent);
366
+ try {
367
+ (_a = this._data.parent) === null || _a === void 0 ? void 0 : _a.$addDataNode(dataGroup.$name, dataGroup, true);
368
+ }
369
+ catch (e) {
370
+ this.form.logger.error(`Unable to set items for container '${this.qualifiedName}' with data model '${dataModel}': ${e.message}`);
371
+ return null;
372
+ }
373
+ return dataGroup;
374
+ }
375
+ importData(dataModel) {
350
376
  if (typeof this._data !== 'undefined' && this.type === 'array' && Array.isArray(dataModel)) {
351
- const dataGroup = new DataGroup_1.default(this._data.$name, dataModel, this._data.$type, this._data.parent);
352
- try {
353
- (_a = this._data.parent) === null || _a === void 0 ? void 0 : _a.$addDataNode(dataGroup.$name, dataGroup, true);
354
- }
355
- catch (e) {
356
- this.form.logger.error(`unable to setItems for ${this.qualifiedName} : ${e}`);
377
+ const dataGroup = this.createDataGroup(dataModel);
378
+ if (!dataGroup) {
357
379
  return;
358
380
  }
359
381
  this._data = dataGroup;
@@ -370,6 +392,13 @@ class Container extends Scriptable_1.default {
370
392
  this.notifyDependents((0, Events_1.propertyChange)('items', null, item.getState()));
371
393
  });
372
394
  }
395
+ if (typeof this._data !== 'undefined' && this.type === 'object') {
396
+ const dataGroup = this.createDataGroup(dataModel);
397
+ if (!dataGroup) {
398
+ return;
399
+ }
400
+ this.syncDataAndFormModel(dataGroup);
401
+ }
373
402
  }
374
403
  syncDataAndFormModel(contextualDataModel) {
375
404
  const result = {
@@ -32,6 +32,10 @@ export declare type UIChangePayload = {
32
32
  value?: any;
33
33
  checked?: boolean;
34
34
  };
35
+ export declare type AddItemPayload = {
36
+ index: number;
37
+ data: Record<string, any>;
38
+ };
35
39
  export declare class Change extends ActionImpl {
36
40
  constructor(payload: ChangePayload, dispatch?: boolean);
37
41
  withAdditionalChange(change: Change): Change;
@@ -93,13 +97,13 @@ export declare class CustomEvent extends ActionImpl {
93
97
  get isCustomEvent(): boolean;
94
98
  }
95
99
  export declare class AddItem extends ActionImpl {
96
- constructor(payload?: number);
100
+ constructor(payload?: number | AddItemPayload);
97
101
  }
98
102
  export declare class RemoveItem extends ActionImpl {
99
103
  constructor(payload?: number);
100
104
  }
101
105
  export declare class AddInstance extends ActionImpl {
102
- constructor(payload?: number);
106
+ constructor(payload?: number | AddItemPayload);
103
107
  }
104
108
  export declare class RemoveInstance extends ActionImpl {
105
109
  constructor(payload?: number);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aemforms/af-core",
3
- "version": "0.22.101",
3
+ "version": "0.22.102",
4
4
  "description": "Core Module for Forms Runtime",
5
5
  "author": "Adobe Systems",
6
6
  "license": "Adobe Proprietary",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@adobe/json-formula": "0.1.50",
40
- "@aemforms/af-formatters": "^0.22.101"
40
+ "@aemforms/af-formatters": "^0.22.102"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@babel/preset-env": "^7.20.2",