@rxap/forms 14.0.0 → 14.1.0-dev.0

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.
@@ -14,7 +14,7 @@ import { ToggleSubject } from '@rxap/utilities/rxjs';
14
14
  import * as i2$1 from '@angular/router';
15
15
  import { Router } from '@angular/router';
16
16
  import { ConfirmClick } from '@rxap/directives';
17
- import { isMobilePhone, isEmail, isURL, isIP } from '@rxap/validator';
17
+ import { isMobilePhone, isEmail, isURL, isIP, isPort, isUUID } from '@rxap/validator';
18
18
 
19
19
  function getControlValue(control) {
20
20
  if (control.getRawValue) {
@@ -170,8 +170,9 @@ class RxapFormArray extends UntypedFormArray {
170
170
  *
171
171
  * @param index (optional) the index where the control should be created
172
172
  * @param state (optional) the initial state of the new control
173
+ * @param options (optional) ControlEventOptions
173
174
  */
174
- insertAt(index, state) {
175
+ insertAt(index, state, options) {
175
176
  const insertIndex = index ?? this.controls.length;
176
177
  const controlOrDefinition = this._builder(state, {
177
178
  controlId: insertIndex.toFixed(0),
@@ -186,10 +187,10 @@ class RxapFormArray extends UntypedFormArray {
186
187
  // call the super insert after the update, bc the insert method will
187
188
  // trigger a change detection
188
189
  if (controlOrDefinition instanceof AbstractControl) {
189
- super.insert(insertIndex, controlOrDefinition);
190
+ super.insert(insertIndex, controlOrDefinition, options);
190
191
  }
191
192
  else {
192
- super.insert(insertIndex, controlOrDefinition.rxapFormGroup);
193
+ super.insert(insertIndex, controlOrDefinition.rxapFormGroup, options);
193
194
  }
194
195
  }
195
196
  disabledWhile(observable, options) {
@@ -216,7 +217,9 @@ class RxapFormArray extends UntypedFormArray {
216
217
  }
217
218
  setValue(valueOrObservable, options) {
218
219
  if (isObservable(valueOrObservable)) {
219
- return valueOrObservable.subscribe((value) => super.setValue(value, options));
220
+ return valueOrObservable.subscribe((value) => {
221
+ super.setValue(value, options);
222
+ });
220
223
  }
221
224
  super.setValue(valueOrObservable, options);
222
225
  }
@@ -231,13 +234,37 @@ class RxapFormArray extends UntypedFormArray {
231
234
  hasErrorAndTouched(errorCode, path) {
232
235
  return hasErrorAndTouched(this, errorCode, path);
233
236
  }
237
+ _patchValue(value, options) {
238
+ // Even though the `value` argument type doesn't allow `null` and `undefined` values, the
239
+ // `patchValue` can be called recursively and inner data structures might have these values, so
240
+ // we just ignore such cases when a field containing FormArray instance receives `null` or
241
+ // `undefined` as a value.
242
+ if (value == null /* both `null` and `undefined` */)
243
+ return;
244
+ if (options?.strict) {
245
+ if (this.length > value.length) {
246
+ for (let index = this.length - 1; index >= value.length; index--) {
247
+ this.removeAt(index);
248
+ }
249
+ }
250
+ }
251
+ value.forEach((newValue, index) => {
252
+ if (this.at(index)) {
253
+ this.at(index).patchValue(newValue, { ...(options ?? {}), onlySelf: true });
254
+ }
255
+ else if (options?.coerce) {
256
+ this.insertAt(index, value[index]);
257
+ }
258
+ });
259
+ this.updateValueAndValidity(options);
260
+ }
234
261
  patchValue(valueOrObservable, options) {
235
262
  if (isObservable(valueOrObservable)) {
236
263
  return valueOrObservable.subscribe((value) => {
237
- super.patchValue(value, options);
264
+ this._patchValue(value, options);
238
265
  });
239
266
  }
240
- super.patchValue(valueOrObservable, options);
267
+ this._patchValue(valueOrObservable, options);
241
268
  }
242
269
  hasErrorAndDirty(errorCode, path) {
243
270
  return hasErrorAndDirty(this, errorCode, path);
@@ -252,20 +279,20 @@ class RxapFormArray extends UntypedFormArray {
252
279
  setEnable(enable = true, opts) {
253
280
  enableControl(this, enable, opts);
254
281
  }
255
- push(control) {
282
+ push(control, options) {
256
283
  if (isDevMode()) {
257
284
  console.warn('It is not recommend to use the FormArray.push method');
258
285
  }
259
- return super.push(control);
286
+ return super.push(control, options);
260
287
  }
261
288
  setDisable(disable = true, opts) {
262
289
  disableControl(this, disable, opts);
263
290
  }
264
- setControl(index, control) {
291
+ setControl(index, control, options) {
265
292
  if (isDevMode()) {
266
293
  console.warn('It is not recommend to use the FormArray.setControl method');
267
294
  }
268
- return super.setControl(index, control);
295
+ return super.setControl(index, control, options);
269
296
  }
270
297
  markAsTouched(opts) {
271
298
  super.markAsTouched(opts);
@@ -409,11 +436,25 @@ class RxapFormGroup extends UntypedFormGroup {
409
436
  }
410
437
  super.setValue(valueOrObservable, options);
411
438
  }
439
+ _patchValue(value, options) {
440
+ // Even though the `value` argument type doesn't allow `null` and `undefined` values, the
441
+ // `patchValue` can be called recursively and inner data structures might have these values, so
442
+ // we just ignore such cases when a field containing FormGroup instance receives `null` or
443
+ // `undefined` as a value.
444
+ if (value == null /* both `null` and `undefined` */)
445
+ return;
446
+ Object.keys(value).forEach(name => {
447
+ if (this.controls[name]) {
448
+ this.controls[name].patchValue(value[name], { ...(options ?? {}), onlySelf: true });
449
+ }
450
+ });
451
+ this.updateValueAndValidity(options);
452
+ }
412
453
  patchValue(valueOrObservable, options) {
413
454
  if (isObservable(valueOrObservable)) {
414
- return valueOrObservable.subscribe((value) => super.patchValue(value, options));
455
+ return valueOrObservable.subscribe((value) => this._patchValue(value, options));
415
456
  }
416
- super.patchValue(valueOrObservable, options);
457
+ this._patchValue(valueOrObservable, options);
417
458
  }
418
459
  disabledWhile(observable, options) {
419
460
  return controlDisabledWhile(this, observable, options);
@@ -1201,10 +1242,14 @@ class FormDirective extends FormGroupDirective {
1201
1242
  }
1202
1243
  }
1203
1244
  ngOnInit() {
1204
- if (!this.form) {
1245
+ if (!this._formDefinition) {
1205
1246
  // TODO : replace with rxap error
1206
1247
  throw new Error('The form definition instance is not defined');
1207
1248
  }
1249
+ if (!this.form) {
1250
+ // TODO : replace with rxap error
1251
+ throw new Error('The form instance is not defined');
1252
+ }
1208
1253
  this.loadInitialState(this.form);
1209
1254
  function HasNgOnInitMethod(obj) {
1210
1255
  return obj && typeof obj.ngOnInit === 'function';
@@ -1216,8 +1261,14 @@ class FormDirective extends FormGroupDirective {
1216
1261
  const debounce = typeof this._formDefinition.rxapMetadata.autoSubmit === 'number'
1217
1262
  ? this._formDefinition.rxapMetadata.autoSubmit
1218
1263
  : 5000;
1219
- this._autoSubmitSubscription = this.form.valueChanges
1220
- .pipe(debounceTime(debounce), filter(() => this.form.valid), tap((value) => console.debug(`Auto submit form '${this._formDefinition.rxapMetadata.id}'`, value)), tap(() => this.submit()))
1264
+ this._autoSubmitSubscription = this
1265
+ .form
1266
+ .valueChanges
1267
+ .pipe(debounceTime(debounce), filter(() => this.form.valid), tap((value) => {
1268
+ if (isDevMode()) {
1269
+ console.debug(`Auto submit form '${this._formDefinition.rxapMetadata.id}'`, value);
1270
+ }
1271
+ }), tap(() => this.submit()))
1221
1272
  .subscribe();
1222
1273
  }
1223
1274
  }
@@ -2681,6 +2732,42 @@ function IsIP({ message, version } = {}) {
2681
2732
  };
2682
2733
  }
2683
2734
 
2735
+ function IsPort({ message } = {}) {
2736
+ return (control) => {
2737
+ if (control.value === null) {
2738
+ return null;
2739
+ }
2740
+ if (!(typeof control.value === 'string' && isPort(control.value))) {
2741
+ return {
2742
+ isPort: {
2743
+ expected: `A valid port number between 1 and 65535`,
2744
+ actual: control.value,
2745
+ message
2746
+ }
2747
+ };
2748
+ }
2749
+ return null;
2750
+ };
2751
+ }
2752
+
2753
+ function IsUUID({ message, version = 'all' } = {}) {
2754
+ return (control) => {
2755
+ if (control.value === null) {
2756
+ return null;
2757
+ }
2758
+ if (!(typeof control.value === 'string' && isUUID(control.value, version))) {
2759
+ return {
2760
+ isUuid: {
2761
+ expected: version === 'all' ? `A valid uuid` : `A valid uuid version ${version}`,
2762
+ actual: control.value,
2763
+ message
2764
+ }
2765
+ };
2766
+ }
2767
+ return null;
2768
+ };
2769
+ }
2770
+
2684
2771
  const RxapValidators = {
2685
2772
  IsNumber: _IsNumber,
2686
2773
  IsBoolean,
@@ -2694,7 +2781,9 @@ const RxapValidators = {
2694
2781
  IsPhoneNumber,
2695
2782
  IsEmail,
2696
2783
  IsUrl,
2697
- IsIP
2784
+ IsIP,
2785
+ IsPort,
2786
+ IsUUID,
2698
2787
  };
2699
2788
 
2700
2789
  // endregion