@aemforms/af-core 0.22.16 → 0.22.18

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.
package/lib/Field.d.ts CHANGED
@@ -2,9 +2,6 @@ import { ConstraintsMessages, ContainerModel, FieldJson, FieldModel, FormModel,
2
2
  import Scriptable from './Scriptable';
3
3
  import DataValue from './data/DataValue';
4
4
  import DataGroup from './data/DataGroup';
5
- /**
6
- * Defines a form object field which implements {@link FieldModel | field model} interface
7
- */
8
5
  declare class Field extends Scriptable<FieldJson> implements FieldModel {
9
6
  /**
10
7
  * @param params
@@ -61,8 +58,11 @@ declare class Field extends Scriptable<FieldJson> implements FieldModel {
61
58
  private isEmpty;
62
59
  get editValue(): any;
63
60
  get displayValue(): any;
61
+ protected getDataNodeValue(typedValue: any): any;
64
62
  get value(): any;
65
63
  set value(v: any);
64
+ protected _updateRuleNodeReference(value: any): void;
65
+ protected getInternalType(): string | undefined;
66
66
  valueOf(): any;
67
67
  toString(): any;
68
68
  /**
@@ -70,6 +70,8 @@ declare class Field extends Scriptable<FieldJson> implements FieldModel {
70
70
  * @param constraint
71
71
  */
72
72
  getErrorMessage(constraint: keyof (ConstraintsMessages)): string;
73
+ get errorMessage(): string | undefined;
74
+ get screenReaderText(): string | undefined;
73
75
  /**
74
76
  *
75
77
  * @private
@@ -105,10 +107,7 @@ declare class Field extends Scriptable<FieldJson> implements FieldModel {
105
107
  };
106
108
  maxItems: <T_1>(constraint: number, value: T_1[]) => {
107
109
  valid: boolean;
108
- value: T_1[]; /**
109
- * Returns the error message for a given constraint
110
- * @param constraint
111
- */
110
+ value: T_1[];
112
111
  };
113
112
  uniqueItems: <T_2>(constraint: boolean, value: T_2[]) => {
114
113
  valid: boolean;
@@ -134,6 +133,14 @@ declare class Field extends Scriptable<FieldJson> implements FieldModel {
134
133
  valid: boolean;
135
134
  value: any;
136
135
  };
136
+ accept: (constraint: string[], value: any) => {
137
+ valid: boolean;
138
+ value: any;
139
+ };
140
+ maxFileSize: (constraint: string | number, value: any) => {
141
+ valid: boolean;
142
+ value: any;
143
+ };
137
144
  };
138
145
  /**
139
146
  * returns whether the field is array type or not
@@ -168,20 +175,23 @@ declare class Field extends Scriptable<FieldJson> implements FieldModel {
168
175
  /**
169
176
  * returns the format constraint
170
177
  */
171
- get format(): string;
178
+ get format(): string | undefined;
179
+ get enforceEnum(): boolean | undefined;
180
+ get tooltip(): string | undefined;
181
+ get maxLength(): number | undefined;
182
+ get minLength(): number | undefined;
183
+ get pattern(): string | undefined;
184
+ get step(): number | undefined;
185
+ get exclusiveMinimum(): number | undefined;
186
+ set exclusiveMinimum(eM: number | undefined);
187
+ get exclusiveMaximum(): number | undefined;
188
+ set exclusiveMaximum(eM: number | undefined);
189
+ get default(): any;
172
190
  /**
173
191
  * @private
174
192
  */
175
193
  protected evaluateConstraints(): any;
176
194
  triggerValidationEvent(changes: any): void;
177
- /**
178
- * Checks whether there are any updates in the properties. If there are applies them to the
179
- * json model as well.
180
- * @param propNames
181
- * @param updates
182
- * @private
183
- */
184
- protected _applyUpdates(propNames: string[], updates: any): any;
185
195
  /**
186
196
  * Validates the current form object
187
197
  */
@@ -196,8 +206,8 @@ declare class Field extends Scriptable<FieldJson> implements FieldModel {
196
206
  editValue: any;
197
207
  displayValue: any;
198
208
  description?: string | undefined;
199
- rules?: import("./types").Items<string> | undefined;
200
- events?: import("./types").Items<string | string[] | undefined> | undefined;
209
+ rules: import("./types").Items<string> & {};
210
+ events: import("./types").Items<string | string[] | undefined> & {};
201
211
  enumNames?: string[] | undefined;
202
212
  enum?: any[] | undefined;
203
213
  accept?: string[] | undefined;
@@ -227,9 +237,12 @@ declare class Field extends Scriptable<FieldJson> implements FieldModel {
227
237
  constraintMessages?: ConstraintsMessages | undefined;
228
238
  fieldType?: string | undefined;
229
239
  errorMessage?: string | undefined;
230
- properties?: {
240
+ properties: {
231
241
  [key: string]: any;
232
- } | undefined;
242
+ };
243
+ screenReaderText?: string | undefined;
244
+ tooltip?: string | undefined;
245
+ altText?: string | undefined;
233
246
  placeholder?: string | undefined;
234
247
  readOnly?: boolean | undefined;
235
248
  valid?: boolean | undefined;
@@ -238,6 +251,9 @@ declare class Field extends Scriptable<FieldJson> implements FieldModel {
238
251
  displayFormat?: string | undefined;
239
252
  editFormat?: string | undefined;
240
253
  emptyValue?: "" | "undefined" | "null" | undefined;
254
+ index: number;
255
+ parent: undefined;
256
+ qualifiedName: any;
241
257
  id: string;
242
258
  };
243
259
  }
package/lib/Field.js CHANGED
@@ -28,6 +28,7 @@ const af_formatters_1 = require("@aemforms/af-formatters");
28
28
  /**
29
29
  * Defines a form object field which implements {@link FieldModel | field model} interface
30
30
  */
31
+ const validTypes = ['string', 'number', 'boolean', 'file', 'string[]', 'number[]', 'boolean[]', 'file[]', 'array', 'object'];
31
32
  class Field extends Scriptable_1.default {
32
33
  /**
33
34
  * @param params
@@ -73,10 +74,31 @@ class Field extends Scriptable_1.default {
73
74
  */
74
75
  _getFallbackType() {
75
76
  const type = this._jsonModel.type;
76
- if (typeof type !== 'string') {
77
+ let finalType = type;
78
+ if (typeof type !== 'string' || validTypes.indexOf(type) === -1) {
77
79
  const _enum = this.enum;
78
- return _enum && _enum.length > 0 ? typeof _enum[0] : 'string';
80
+ finalType = typeof (_enum === null || _enum === void 0 ? void 0 : _enum[0]);
81
+ if (finalType === 'undefined' && typeof this._jsonModel.default !== 'undefined') {
82
+ if (this._jsonModel.default instanceof Array && this._jsonModel.default.length > 0) {
83
+ finalType = `${typeof (this._jsonModel.default[0])}[]`;
84
+ }
85
+ else {
86
+ finalType = typeof (this._jsonModel.default);
87
+ }
88
+ }
89
+ if (finalType.indexOf('undefined') === 0) {
90
+ const typeMappings = {
91
+ 'text-input': 'string',
92
+ 'number-input': 'number',
93
+ 'date-input': 'string',
94
+ 'plain-text': 'string',
95
+ 'image': 'string',
96
+ 'checkbox': 'boolean'
97
+ };
98
+ finalType = typeMappings[this.fieldType];
99
+ }
79
100
  }
101
+ return finalType;
80
102
  }
81
103
  _applyDefaults() {
82
104
  Object.entries(this._getDefaults()).map(([key, value]) => {
@@ -86,11 +108,23 @@ class Field extends Scriptable_1.default {
86
108
  this._jsonModel[key] = value;
87
109
  }
88
110
  });
111
+ const type = this._jsonModel.type;
112
+ if (typeof type !== 'string' || validTypes.indexOf(type) === -1) {
113
+ this._jsonModel.type = this._getFallbackType();
114
+ }
115
+ if (['plain-text', 'image'].indexOf(this.fieldType) === -1) {
116
+ this._jsonModel.value = undefined;
117
+ }
89
118
  const value = this._jsonModel.value;
90
119
  if (value === undefined) {
91
- this._jsonModel.value = this._jsonModel.default;
120
+ const typedRes = ValidationUtils_1.Constraints.type(this.getInternalType() || 'string', this._jsonModel.default);
121
+ this._jsonModel.value = typedRes.value;
122
+ }
123
+ if (this._jsonModel.type !== 'string') {
124
+ this._jsonModel.emptyValue = undefined;
92
125
  }
93
126
  if (this._jsonModel.fieldType === undefined) {
127
+ this.form.logger.error('fieldType property is mandatory. Please ensure all the fields have a fieldType');
94
128
  //@ts-ignore
95
129
  if (this._jsonModel.viewType) {
96
130
  //@ts-ignore
@@ -113,7 +147,39 @@ class Field extends Scriptable_1.default {
113
147
  this._jsonModel.enum = [true, false];
114
148
  }
115
149
  }
116
- if (typeof this._jsonModel.step !== 'number' || this._jsonModel.type !== 'number') {
150
+ else {
151
+ if (typeof this._jsonModel.enumNames === 'undefined') {
152
+ this._jsonModel.enumNames = this._jsonModel.enum.map(_ => _.toString());
153
+ }
154
+ while (this._jsonModel.enumNames.length < this._jsonModel.enum.length) {
155
+ //@ts-ignore
156
+ this._jsonModel.enumNames.push(this._jsonModel.enum[this._jsonModel.enumNames.length].toString());
157
+ }
158
+ }
159
+ if (this._jsonModel.type !== 'string') {
160
+ this._jsonModel.format = undefined;
161
+ this._jsonModel.pattern = undefined;
162
+ this._jsonModel.minLength = undefined;
163
+ this._jsonModel.maxLength = undefined;
164
+ }
165
+ else if (this._jsonModel.fieldType === 'date-input') {
166
+ this._jsonModel.format = 'date';
167
+ }
168
+ if (this._jsonModel.type !== 'number' && this._jsonModel.format !== 'date') {
169
+ this._jsonModel.step = undefined;
170
+ this._jsonModel.minimum = undefined;
171
+ this._jsonModel.maximum = undefined;
172
+ this._jsonModel.exclusiveMinimum = undefined;
173
+ this._jsonModel.exclusiveMaximum = undefined;
174
+ }
175
+ ['minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum'].forEach(c => {
176
+ //@ts-ignore
177
+ if (typeof this._jsonModel[c] !== this._jsonModel.type) {
178
+ //@ts-ignore
179
+ this._jsonModel[c] = undefined;
180
+ }
181
+ });
182
+ if (typeof this._jsonModel.step !== 'number') {
117
183
  this._jsonModel.step = undefined;
118
184
  }
119
185
  }
@@ -175,16 +241,24 @@ class Field extends Scriptable_1.default {
175
241
  this._setProperty('required', r);
176
242
  }
177
243
  get maximum() {
178
- return this._jsonModel.maximum;
244
+ if (this.type === 'number' || this.format === 'date') {
245
+ return this._jsonModel.maximum;
246
+ }
179
247
  }
180
248
  set maximum(m) {
181
- this._setProperty('maximum', m);
249
+ if (this.type === 'number' || this.format === 'date') {
250
+ this._setProperty('maximum', m);
251
+ }
182
252
  }
183
253
  get minimum() {
184
- return this._jsonModel.minimum;
254
+ if (this.type === 'number' || this.format === 'date') {
255
+ return this._jsonModel.minimum;
256
+ }
185
257
  }
186
258
  set minimum(m) {
187
- this._setProperty('minimum', m);
259
+ if (this.type === 'number' || this.format === 'date') {
260
+ this._setProperty('minimum', m);
261
+ }
188
262
  }
189
263
  /**
190
264
  * returns whether the value is empty. Empty value is either a '', undefined or null
@@ -211,6 +285,9 @@ class Field extends Scriptable_1.default {
211
285
  return this.value;
212
286
  }
213
287
  }
288
+ getDataNodeValue(typedValue) {
289
+ return this.isEmpty() ? this.emptyValue : typedValue;
290
+ }
214
291
  get value() {
215
292
  if (this._jsonModel.value === undefined) {
216
293
  return null;
@@ -220,30 +297,19 @@ class Field extends Scriptable_1.default {
220
297
  }
221
298
  }
222
299
  set value(v) {
223
- var _a;
300
+ const dataNode = this.getDataNode();
301
+ if (BaseNode_1.staticFields.indexOf(this.fieldType) > -1 && typeof dataNode !== 'undefined') {
302
+ // if a static field is bound to data node it can not be updated.
303
+ return;
304
+ }
224
305
  const Constraints = this._getConstraintObject();
225
- const typeRes = Constraints.type(this._jsonModel.type || 'string', v);
306
+ const typeRes = Constraints.type(this.getInternalType() || 'string', v);
226
307
  const changes = this._setProperty('value', typeRes.value, false);
227
308
  let uniqueRes = { valid: true };
228
309
  if (changes.length > 0) {
229
- if ((_a = this.type) === null || _a === void 0 ? void 0 : _a.endsWith('[]')) {
230
- if (typeRes.value != null) {
231
- typeRes.value.forEach((val, index) => {
232
- this._ruleNodeReference[index] = val;
233
- });
234
- while (typeRes.value.length !== this._ruleNodeReference.length) {
235
- this._ruleNodeReference.pop();
236
- }
237
- }
238
- else {
239
- while (this._ruleNodeReference.length !== 0) {
240
- this._ruleNodeReference.pop();
241
- }
242
- }
243
- }
244
- const dataNode = this.getDataNode();
310
+ this._updateRuleNodeReference(typeRes.value);
245
311
  if (typeof dataNode !== 'undefined') {
246
- dataNode.setValue(this.isEmpty() ? this.emptyValue : this._jsonModel.value, this._jsonModel.value);
312
+ dataNode.setValue(this.getDataNodeValue(this._jsonModel.value), this._jsonModel.value, this);
247
313
  }
248
314
  if (this.parent.uniqueItems && this.parent.type === 'array') {
249
315
  // @ts-ignore
@@ -267,6 +333,27 @@ class Field extends Scriptable_1.default {
267
333
  this.dispatch(changeAction);
268
334
  }
269
335
  }
336
+ _updateRuleNodeReference(value) {
337
+ var _a;
338
+ if ((_a = this.type) === null || _a === void 0 ? void 0 : _a.endsWith('[]')) {
339
+ if (value != null) {
340
+ value.forEach((val, index) => {
341
+ this._ruleNodeReference[index] = val;
342
+ });
343
+ while (value.length !== this._ruleNodeReference.length) {
344
+ this._ruleNodeReference.pop();
345
+ }
346
+ }
347
+ else {
348
+ while (this._ruleNodeReference.length !== 0) {
349
+ this._ruleNodeReference.pop();
350
+ }
351
+ }
352
+ }
353
+ }
354
+ getInternalType() {
355
+ return this.type;
356
+ }
270
357
  valueOf() {
271
358
  // @ts-ignore
272
359
  const obj = this[BaseNode_1.target];
@@ -289,6 +376,12 @@ class Field extends Scriptable_1.default {
289
376
  var _a;
290
377
  return ((_a = this._jsonModel.constraintMessages) === null || _a === void 0 ? void 0 : _a[constraint]) || '';
291
378
  }
379
+ get errorMessage() {
380
+ return this._jsonModel.errorMessage;
381
+ }
382
+ get screenReaderText() {
383
+ return this._jsonModel.screenReaderText;
384
+ }
292
385
  /**
293
386
  *
294
387
  * @private
@@ -363,7 +456,10 @@ class Field extends Scriptable_1.default {
363
456
  default:
364
457
  return ValidationUtils_1.ValidConstraints.string;
365
458
  }
459
+ case 'file':
460
+ return ValidationUtils_1.ValidConstraints.file;
366
461
  case 'number':
462
+ case 'integer':
367
463
  return ValidationUtils_1.ValidConstraints.number;
368
464
  }
369
465
  if (this.isArrayType()) {
@@ -375,7 +471,62 @@ class Field extends Scriptable_1.default {
375
471
  * returns the format constraint
376
472
  */
377
473
  get format() {
378
- return this._jsonModel.format || '';
474
+ if (typeof this._jsonModel.format === 'undefined') {
475
+ if (this.type === 'string') {
476
+ switch (this.fieldType) {
477
+ case 'date-input':
478
+ this._jsonModel.format = 'date';
479
+ break;
480
+ case 'file-input':
481
+ this._jsonModel.format = 'data-url';
482
+ break;
483
+ }
484
+ }
485
+ }
486
+ return this._jsonModel.format;
487
+ }
488
+ get enforceEnum() {
489
+ return this._jsonModel.enforceEnum;
490
+ }
491
+ get tooltip() {
492
+ return this._jsonModel.tooltip;
493
+ }
494
+ get maxLength() {
495
+ return this._jsonModel.maxLength;
496
+ }
497
+ get minLength() {
498
+ return this._jsonModel.minLength;
499
+ }
500
+ get pattern() {
501
+ return this._jsonModel.pattern;
502
+ }
503
+ get step() {
504
+ if (this.type === 'number' || this.format === 'date') {
505
+ return this._jsonModel.step;
506
+ }
507
+ }
508
+ get exclusiveMinimum() {
509
+ if (this.type === 'number' || this.format === 'date') {
510
+ return this._jsonModel.exclusiveMinimum;
511
+ }
512
+ }
513
+ set exclusiveMinimum(eM) {
514
+ if (this.type === 'number' || this.format === 'date') {
515
+ this._jsonModel.exclusiveMinimum = eM;
516
+ }
517
+ }
518
+ get exclusiveMaximum() {
519
+ if (this.type === 'number' || this.format === 'date') {
520
+ return this._jsonModel.exclusiveMaximum;
521
+ }
522
+ }
523
+ set exclusiveMaximum(eM) {
524
+ if (this.type === 'number' || this.format === 'date') {
525
+ this._jsonModel.exclusiveMaximum = eM;
526
+ }
527
+ }
528
+ get default() {
529
+ return this._jsonModel.default;
379
530
  }
380
531
  /**
381
532
  * @private
@@ -394,7 +545,8 @@ class Field extends Scriptable_1.default {
394
545
  }
395
546
  if (valid && value != this.emptyValue) {
396
547
  const invalidConstraint = supportedConstraints.find(key => {
397
- if (key in elem) {
548
+ // @ts-ignore
549
+ if (key in elem && elem[key] !== undefined) {
398
550
  // @ts-ignore
399
551
  const restriction = elem[key];
400
552
  // @ts-ignore
@@ -455,24 +607,6 @@ class Field extends Scriptable_1.default {
455
607
  }
456
608
  }
457
609
  }
458
- /**
459
- * Checks whether there are any updates in the properties. If there are applies them to the
460
- * json model as well.
461
- * @param propNames
462
- * @param updates
463
- * @private
464
- */
465
- _applyUpdates(propNames, updates) {
466
- return propNames.reduce((acc, propertyName) => {
467
- //@ts-ignore
468
- const currentValue = updates[propertyName];
469
- const changes = this._setProperty(propertyName, currentValue, false);
470
- if (changes.length > 0) {
471
- acc[propertyName] = changes[0];
472
- }
473
- return acc;
474
- }, {});
475
- }
476
610
  /**
477
611
  * Validates the current form object
478
612
  */
@@ -499,17 +633,20 @@ class Field extends Scriptable_1.default {
499
633
  * @private
500
634
  */
501
635
  defaultDataModel(name) {
502
- return new DataValue_1.default(name, this.isEmpty() ? this.emptyValue : this._jsonModel.value, this.type || 'string');
636
+ const value = BaseNode_1.staticFields.indexOf(this.fieldType) > -1 ? undefined : this.getDataNodeValue(this._jsonModel.value);
637
+ return new DataValue_1.default(name, value, this.type || 'string');
503
638
  }
504
639
  getState() {
505
640
  return Object.assign(Object.assign({}, super.getState()), { editValue: this.editValue, displayValue: this.displayValue });
506
641
  }
507
642
  }
508
643
  __decorate([
509
- (0, BaseNode_1.dependencyTracked)()
644
+ (0, BaseNode_1.dependencyTracked)(),
645
+ (0, BaseNode_1.exclude)('button', 'image', 'plain-text')
510
646
  ], Field.prototype, "readOnly", null);
511
647
  __decorate([
512
- (0, BaseNode_1.dependencyTracked)()
648
+ (0, BaseNode_1.dependencyTracked)(),
649
+ (0, BaseNode_1.exclude)('button', 'image', 'plain-text')
513
650
  ], Field.prototype, "enabled", null);
514
651
  __decorate([
515
652
  (0, BaseNode_1.dependencyTracked)()
@@ -523,7 +660,31 @@ __decorate([
523
660
  __decorate([
524
661
  (0, BaseNode_1.dependencyTracked)()
525
662
  ], Field.prototype, "required", null);
663
+ __decorate([
664
+ (0, BaseNode_1.include)('date-input', 'number-input')
665
+ ], Field.prototype, "editValue", null);
666
+ __decorate([
667
+ (0, BaseNode_1.include)('date-input', 'number-input')
668
+ ], Field.prototype, "displayValue", null);
526
669
  __decorate([
527
670
  (0, BaseNode_1.dependencyTracked)()
528
671
  ], Field.prototype, "value", null);
672
+ __decorate([
673
+ (0, BaseNode_1.include)('text-input', 'date-input', 'file-input')
674
+ ], Field.prototype, "format", null);
675
+ __decorate([
676
+ (0, BaseNode_1.include)('text-input')
677
+ ], Field.prototype, "maxLength", null);
678
+ __decorate([
679
+ (0, BaseNode_1.include)('text-input')
680
+ ], Field.prototype, "minLength", null);
681
+ __decorate([
682
+ (0, BaseNode_1.include)('text-input')
683
+ ], Field.prototype, "pattern", null);
684
+ __decorate([
685
+ (0, BaseNode_1.dependencyTracked)()
686
+ ], Field.prototype, "exclusiveMinimum", null);
687
+ __decorate([
688
+ (0, BaseNode_1.dependencyTracked)()
689
+ ], Field.prototype, "exclusiveMaximum", null);
529
690
  exports.default = Field;
@@ -8,10 +8,12 @@ export declare class FileObject implements IFileObject {
8
8
  name: string;
9
9
  size: number;
10
10
  constructor(init?: Partial<FileObject>);
11
+ get type(): string;
11
12
  toJSON(): {
12
13
  name: string;
13
14
  size: number;
14
15
  mediaType: string;
15
16
  data: any;
16
17
  };
18
+ equals(obj: IFileObject): boolean;
17
19
  }
package/lib/FileObject.js CHANGED
@@ -18,6 +18,9 @@ class FileObject {
18
18
  this.size = 0;
19
19
  Object.assign(this, init);
20
20
  }
21
+ get type() {
22
+ return this.mediaType;
23
+ }
21
24
  toJSON() {
22
25
  return {
23
26
  'name': this.name,
@@ -26,5 +29,11 @@ class FileObject {
26
29
  'data': this.data.toString()
27
30
  };
28
31
  }
32
+ equals(obj) {
33
+ return (this.data === obj.data &&
34
+ this.mediaType === obj.mediaType &&
35
+ this.name === obj.name &&
36
+ this.size === obj.size);
37
+ }
29
38
  }
30
39
  exports.FileObject = FileObject;
@@ -8,11 +8,12 @@ declare class FileUpload extends Field implements FieldModel {
8
8
  protected _getDefaults(): {
9
9
  accept: string[];
10
10
  maxFileSize: string;
11
- type: string;
12
11
  readOnly: boolean;
13
12
  enabled: boolean;
14
13
  visible: boolean;
14
+ type: string | undefined;
15
15
  };
16
+ protected _getFallbackType(): string | undefined;
16
17
  /**
17
18
  * Returns the max file size in bytes as per IEC specification
18
19
  */
@@ -28,19 +29,9 @@ declare class FileUpload extends Field implements FieldModel {
28
29
  * @private
29
30
  */
30
31
  protected _applyUpdates(propNames: string[], updates: any): any;
31
- typeCheck(value: any): {
32
- valid: boolean;
33
- value: any;
34
- };
35
- checkFileType(type: any): boolean;
36
- acceptCheck(value: any): {
37
- valid: boolean;
38
- value: any;
39
- };
40
- get value(): any;
41
- set value(value: any);
32
+ protected getInternalType(): "file" | "file[]";
33
+ protected getDataNodeValue(typedValue: any): any;
42
34
  private _serialize;
43
- private coerce;
44
35
  importData(dataModel: DataGroup): void;
45
36
  }
46
37
  export default FileUpload;