@klippa/ngx-enhancy-forms 14.7.16 → 14.8.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.
@@ -1,5 +1,5 @@
1
1
  import * as i0 from '@angular/core';
2
- import { Directive, Input, Component, SkipSelf, Optional, InjectionToken, Inject, ViewChild, EventEmitter, Host, Output, HostBinding, TemplateRef, ContentChild, NgModule } from '@angular/core';
2
+ import { Directive, Input, Component, SkipSelf, Optional, InjectionToken, Inject, ViewChild, EventEmitter, Host, Output, HostBinding, TemplateRef, ContentChild, inject, NgModule } from '@angular/core';
3
3
  import * as i1 from '@angular/common';
4
4
  import { CommonModule } from '@angular/common';
5
5
  import * as i2 from '@angular/forms';
@@ -85,6 +85,9 @@ function splitArrayByCondition(value, condition) {
85
85
  return acc;
86
86
  }, [[]]);
87
87
  }
88
+ function isArrayOf(arr, kind) {
89
+ return arr.reduce((acc, val) => acc && val instanceof kind, true);
90
+ }
88
91
 
89
92
  function mergeArray(arrA, arrB) {
90
93
  const arr = new Array(Math.max(arrA.length, arrB.length));
@@ -409,7 +412,7 @@ class WithTooltipDirective {
409
412
  this.triangle = document.createElement('div');
410
413
  this.triangle.style.zIndex = '1';
411
414
  this.triangle.style.position = 'fixed';
412
- this.triangle.style.left = `${el.nativeElement.getBoundingClientRect().x + (el.nativeElement.getBoundingClientRect().width / 2)}px`;
415
+ this.triangle.style.left = `calc(${el.nativeElement.getBoundingClientRect().x + el.nativeElement.getBoundingClientRect().width}px - 3rem)`;
413
416
  this.triangle.style.top = `${el.nativeElement.getBoundingClientRect().y}px`;
414
417
  this.triangle.style.transform = `translate(-50%, calc(-100% + 0.1rem))`;
415
418
  this.triangle.style.width = '0';
@@ -1260,71 +1263,77 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.8", ngImpor
1260
1263
  args: ['contentRef']
1261
1264
  }] } });
1262
1265
 
1266
+ class FormValidationError extends Error {
1267
+ constructor(path, message) {
1268
+ super(message);
1269
+ this.name = 'FormValidationError';
1270
+ this.path = path;
1271
+ }
1272
+ }
1273
+ const KLP_FORM_ERROR_HANDLER = new InjectionToken('KLP_FORM_ERROR_HANDLER');
1274
+ const DefaultErrorHandler = (error) => {
1275
+ if (Array.isArray(error) && isArrayOf(error, FormValidationError)) {
1276
+ // If the error is an array of FormValidationErrors, then pass it along.
1277
+ return error;
1278
+ }
1279
+ else if (error instanceof FormValidationError) {
1280
+ // If the error is a FormValidationError, then wrap it and pass it on.
1281
+ return [error];
1282
+ }
1283
+ throw error;
1284
+ };
1285
+
1263
1286
  class FormSubmitButtonComponent {
1264
- constructor(parentForm) {
1265
- this.parentForm = parentForm;
1287
+ constructor() {
1288
+ this.parentForm = inject(FormComponent, { host: true, optional: true });
1289
+ this.handleError = inject(KLP_FORM_ERROR_HANDLER, { optional: true }) ?? DefaultErrorHandler;
1266
1290
  this.isLoading = false;
1267
1291
  this.fullWidth = false;
1268
1292
  this.variant = 'greenFilled';
1269
1293
  this.before = () => Promise.resolve();
1270
1294
  this.after = () => Promise.resolve();
1271
- }
1272
- get _() {
1273
- return this.fullWidth;
1295
+ this.setValidationError = (e) => {
1296
+ this.parentForm.formGroup.get(e.path)?.setErrors({ message: { value: e.message } });
1297
+ };
1274
1298
  }
1275
1299
  async submitForm() {
1300
+ await this.before().catch(() => null);
1276
1301
  try {
1277
- await this.before();
1302
+ const [renderedAndEnabledValues, renderedValues] = await this.parentForm.trySubmit();
1303
+ this.isLoading = true;
1304
+ await this.submitCallback(renderedAndEnabledValues, renderedValues)
1305
+ .finally(() => this.isLoading = false);
1278
1306
  }
1279
1307
  catch (e) {
1280
- return;
1281
- }
1282
- this.parentForm
1283
- .trySubmit()
1284
- .then(([renderedAndEnabledValues, renderedValues]) => {
1285
- this.isLoading = true;
1286
- const submitCallbackResult = this.submitCallback(renderedAndEnabledValues, renderedValues);
1287
- if (isNullOrUndefined(submitCallbackResult)) {
1288
- throw new Error('No promise is returned in your submit function.');
1289
- }
1290
- return submitCallbackResult.then(() => (this.isLoading = false)).catch((e) => {
1291
- this.isLoading = false;
1292
- throw e;
1293
- });
1294
- })
1295
- .catch((e) => {
1296
1308
  if (e === invalidFieldsSymbol) {
1297
1309
  return; // swallow the error, the framework will scroll to the field that needs attention
1298
1310
  }
1299
- throw e;
1300
- });
1311
+ this.handleError(e).forEach(this.setValidationError);
1312
+ return;
1313
+ }
1301
1314
  await this.after();
1302
1315
  }
1303
1316
  }
1304
- FormSubmitButtonComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.8", ngImport: i0, type: FormSubmitButtonComponent, deps: [{ token: FormComponent, host: true, optional: true }], target: i0.ɵɵFactoryTarget.Component });
1305
- FormSubmitButtonComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.2.8", type: FormSubmitButtonComponent, selector: "klp-form-submit-button", inputs: { isLoading: "isLoading", fullWidth: "fullWidth", variant: "variant", before: "before", after: "after", submitCallback: "submitCallback" }, host: { properties: { "class._fullWidth": "this._" } }, ngImport: i0, template: "<klp-form-button\n\t\t[variant]=\"variant\"\n\t\t(click)=\"submitForm()\"\n\t\t[disabled]=\"isLoading\"\n\t\t[isLoading]=\"isLoading\"\n\t\t[fullWidth]=\"fullWidth\"\n\t\ttype=\"submit\"\n\t\t[ngClass]=\"fullWidth ? 'fullWidth' : ''\"\n\t>\n\t<ng-content></ng-content>\n</klp-form-button>\n", styles: [":host{display:inline-block}:host._fullWidth{display:block}.fullWidth{width:100%}\n"], dependencies: [{ kind: "directive", type: i1.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "component", type: ButtonComponent, selector: "klp-form-button", inputs: ["variant", "size", "fullWidth", "hasBorder", "disabled", "isLoading", "type", "clickCallback"] }] });
1317
+ FormSubmitButtonComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.8", ngImport: i0, type: FormSubmitButtonComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
1318
+ FormSubmitButtonComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.2.8", type: FormSubmitButtonComponent, selector: "klp-form-submit-button", inputs: { isLoading: "isLoading", fullWidth: "fullWidth", variant: "variant", submitCallback: "submitCallback", before: "before", after: "after" }, host: { properties: { "class._fullWidth": "this.fullWidth" } }, ngImport: i0, template: "<klp-form-button\n\t\t[variant]=\"variant\"\n\t\t(click)=\"submitForm()\"\n\t\t[disabled]=\"isLoading\"\n\t\t[isLoading]=\"isLoading\"\n\t\t[fullWidth]=\"fullWidth\"\n\t\ttype=\"submit\"\n\t\t[ngClass]=\"fullWidth ? 'fullWidth' : ''\"\n\t>\n\t<ng-content></ng-content>\n</klp-form-button>\n", styles: [":host{display:inline-block}:host._fullWidth{display:block}.fullWidth{width:100%}\n"], dependencies: [{ kind: "directive", type: i1.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "component", type: ButtonComponent, selector: "klp-form-button", inputs: ["variant", "size", "fullWidth", "hasBorder", "disabled", "isLoading", "type", "clickCallback"] }] });
1306
1319
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.8", ngImport: i0, type: FormSubmitButtonComponent, decorators: [{
1307
1320
  type: Component,
1308
1321
  args: [{ selector: 'klp-form-submit-button', template: "<klp-form-button\n\t\t[variant]=\"variant\"\n\t\t(click)=\"submitForm()\"\n\t\t[disabled]=\"isLoading\"\n\t\t[isLoading]=\"isLoading\"\n\t\t[fullWidth]=\"fullWidth\"\n\t\ttype=\"submit\"\n\t\t[ngClass]=\"fullWidth ? 'fullWidth' : ''\"\n\t>\n\t<ng-content></ng-content>\n</klp-form-button>\n", styles: [":host{display:inline-block}:host._fullWidth{display:block}.fullWidth{width:100%}\n"] }]
1309
- }], ctorParameters: function () { return [{ type: FormComponent, decorators: [{
1310
- type: Host
1311
- }, {
1312
- type: Optional
1313
- }] }]; }, propDecorators: { isLoading: [{
1322
+ }], propDecorators: { isLoading: [{
1314
1323
  type: Input
1315
1324
  }], fullWidth: [{
1316
1325
  type: Input
1326
+ }, {
1327
+ type: HostBinding,
1328
+ args: ['class._fullWidth']
1317
1329
  }], variant: [{
1318
1330
  type: Input
1331
+ }], submitCallback: [{
1332
+ type: Input
1319
1333
  }], before: [{
1320
1334
  type: Input
1321
1335
  }], after: [{
1322
1336
  type: Input
1323
- }], submitCallback: [{
1324
- type: Input
1325
- }], _: [{
1326
- type: HostBinding,
1327
- args: ['class._fullWidth']
1328
1337
  }] } });
1329
1338
 
1330
1339
  class MultipleValueAccessorBase extends ValueAccessorBase {
@@ -2208,5 +2217,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.8", ngImpor
2208
2217
  * Generated bundle index. Do not edit.
2209
2218
  */
2210
2219
 
2211
- export { ButtonComponent, CheckboxComponent, DATE_PICKER_LOCALE, DATE_PICKER_TRANSLATIONS, DATE_TIME_PICKER_TRANSLATIONS, DEFAULT_ERROR_MESSAGES, DatePickerComponent, DateTimePickerComponent, EmailInputComponent, FORM_ERROR_MESSAGES, FileInputComponent, FormCaptionComponent, FormComponent, FormElementComponent, FormErrorComponent, FormSubmitButtonComponent, HourMinuteInputComponent, KLP_DATE_FORMATS, KlpSelectOptionTemplateDirective, LoadingIndicatorComponent, MultipleValueAccessorBase, NgxEnhancyFormsModule, NumberInputComponent, Orientation, PasswordFieldComponent, RadioComponent, SELECT_TRANSLATIONS, SelectComponent, SelectFooterComponent, SortableGroupedItemsComponent, SortableItemsComponent, SubFormDirective, TextInputComponent, ToggleComponent, ValueAccessorBase, WithTooltipDirective, dateValidator, invalidDateKey, invalidFieldsSymbol, matDateFormatsFactory };
2220
+ export { ButtonComponent, CheckboxComponent, DATE_PICKER_LOCALE, DATE_PICKER_TRANSLATIONS, DATE_TIME_PICKER_TRANSLATIONS, DEFAULT_ERROR_MESSAGES, DatePickerComponent, DateTimePickerComponent, DefaultErrorHandler, EmailInputComponent, FORM_ERROR_MESSAGES, FileInputComponent, FormCaptionComponent, FormComponent, FormElementComponent, FormErrorComponent, FormSubmitButtonComponent, FormValidationError, HourMinuteInputComponent, KLP_DATE_FORMATS, KLP_FORM_ERROR_HANDLER, KlpSelectOptionTemplateDirective, LoadingIndicatorComponent, MultipleValueAccessorBase, NgxEnhancyFormsModule, NumberInputComponent, Orientation, PasswordFieldComponent, RadioComponent, SELECT_TRANSLATIONS, SelectComponent, SelectFooterComponent, SortableGroupedItemsComponent, SortableItemsComponent, SubFormDirective, TextInputComponent, ToggleComponent, ValueAccessorBase, WithTooltipDirective, dateValidator, invalidDateKey, invalidFieldsSymbol, matDateFormatsFactory };
2212
2221
  //# sourceMappingURL=klippa-ngx-enhancy-forms.mjs.map