From dea3dde994ab6643a3c3fbbc5206df52deb86c4c Mon Sep 17 00:00:00 2001 From: Wouter Willems Date: Wed, 31 Jul 2024 14:04:22 +0200 Subject: [PATCH] support submitting only via the submit button and not on enter --- projects/demo/src/app/demo/demo.component.html | 4 ++-- projects/klippa/ngx-enhancy-forms/package.json | 2 +- .../form-submit-button.component.html | 2 +- .../form-submit-button.component.ts | 13 +++++++++++-- .../src/lib/form/form.component.ts | 1 + 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/projects/demo/src/app/demo/demo.component.html b/projects/demo/src/app/demo/demo.component.html index 6c5f9df..07e383c 100644 --- a/projects/demo/src/app/demo/demo.component.html +++ b/projects/demo/src/app/demo/demo.component.html @@ -1,5 +1,5 @@ - +
@@ -211,7 +211,7 @@ simple form with form level errors - + diff --git a/projects/klippa/ngx-enhancy-forms/package.json b/projects/klippa/ngx-enhancy-forms/package.json index 34fd9dd..d439c06 100644 --- a/projects/klippa/ngx-enhancy-forms/package.json +++ b/projects/klippa/ngx-enhancy-forms/package.json @@ -1,6 +1,6 @@ { "name": "@klippa/ngx-enhancy-forms", - "version": "14.22.13", + "version": "14.22.14", "publishConfig": { "access": "public" }, diff --git a/projects/klippa/ngx-enhancy-forms/src/lib/form/form-submit-button/form-submit-button.component.html b/projects/klippa/ngx-enhancy-forms/src/lib/form/form-submit-button/form-submit-button.component.html index 31fe17a..cbe6e53 100644 --- a/projects/klippa/ngx-enhancy-forms/src/lib/form/form-submit-button/form-submit-button.component.html +++ b/projects/klippa/ngx-enhancy-forms/src/lib/form/form-submit-button/form-submit-button.component.html @@ -4,7 +4,7 @@ [disabled]="isLoading || disabled" [isLoading]="isLoading" [fullWidth]="fullWidth" - type="submit" + [type]="buttonType" [ngClass]="fullWidth ? 'fullWidth' : ''" > diff --git a/projects/klippa/ngx-enhancy-forms/src/lib/form/form-submit-button/form-submit-button.component.ts b/projects/klippa/ngx-enhancy-forms/src/lib/form/form-submit-button/form-submit-button.component.ts index 575efab..0d3ae23 100644 --- a/projects/klippa/ngx-enhancy-forms/src/lib/form/form-submit-button/form-submit-button.component.ts +++ b/projects/klippa/ngx-enhancy-forms/src/lib/form/form-submit-button/form-submit-button.component.ts @@ -1,4 +1,4 @@ -import { Component, HostBinding, inject, Input } from '@angular/core'; +import {Component, HostBinding, inject, Input, OnInit} from '@angular/core'; import {FormComponent, invalidFieldsSymbol} from '../form.component'; import { ButtonVariant } from '../../elements/button/button.component'; import { DefaultErrorHandler, FormValidationError, KLP_FORM_ERROR_HANDLER } from '../form-validation-error/form-validation-error'; @@ -15,9 +15,10 @@ export type SubmitButtonVariant = Extract Promise = () => Promise.resolve(); @Input() public disabled: boolean = false; + ngOnInit(): void { + if (this.parentForm.allowSubmitOn === 'buttonOnly') { + this.buttonType = 'button'; + } else { + this.buttonType = 'submit'; + } + } + private setValidationError = (e: FormValidationError) => { this.parentForm.formGroup.get(e.path)?.setErrors({ message: { value: e.message }}); } diff --git a/projects/klippa/ngx-enhancy-forms/src/lib/form/form.component.ts b/projects/klippa/ngx-enhancy-forms/src/lib/form/form.component.ts index e2c86d5..49be4dd 100644 --- a/projects/klippa/ngx-enhancy-forms/src/lib/form/form.component.ts +++ b/projects/klippa/ngx-enhancy-forms/src/lib/form/form.component.ts @@ -39,6 +39,7 @@ export class FormComponent implements OnInit, OnDestroy, OnChanges { @Input() public warnings: Map> = new Map>(); @Input() public errors: Map = new Map(); @Input() public patchValueInterceptor: (values: any) => Promise; + @Input() public allowSubmitOn: 'buttonAndEnter' | 'buttonOnly' = 'buttonAndEnter'; @Output() public onInjected = new EventEmitter>(); // we keep track of what form controls are actually rendered. Only those count when looking at form validation