Skip to content

Commit

Permalink
support submitting only via the submit button and not on enter
Browse files Browse the repository at this point in the history
  • Loading branch information
wouter-willems committed Jul 31, 2024
1 parent 7e1f4c1 commit dea3dde
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions projects/demo/src/app/demo/demo.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<klp-form [formGroup]="myForm" [warnings]="formWarnings" errorMessageLocation="rightOfCaption" [readOnly]="false">
<klp-form [formGroup]="myForm" [warnings]="formWarnings" errorMessageLocation="rightOfCaption" [readOnly]="false" allowSubmitOn="buttonOnly">
<div style="height: 520px; overflow: auto; display: flex; gap: 30px">
<app-deep-input *ngIf="false" >
</app-deep-input>
Expand Down Expand Up @@ -211,7 +211,7 @@

simple form with form level errors

<klp-form [formGroup]="simpleFormWithFormLevelErrors" [errors]="formErrors" errorMessageLocation="rightOfCaption">
<klp-form [formGroup]="simpleFormWithFormLevelErrors" [errors]="formErrors" errorMessageLocation="rightOfCaption" allowSubmitOn="buttonOnly">
<klp-form-element caption="First name" spaceDistribution="34-66" direction="vertical">
<klp-form-text-input formControlName="firstName" [clearable]="true">
</klp-form-text-input>
Expand Down
2 changes: 1 addition & 1 deletion projects/klippa/ngx-enhancy-forms/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@klippa/ngx-enhancy-forms",
"version": "14.22.13",
"version": "14.22.14",
"publishConfig": {
"access": "public"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[disabled]="isLoading || disabled"
[isLoading]="isLoading"
[fullWidth]="fullWidth"
type="submit"
[type]="buttonType"
[ngClass]="fullWidth ? 'fullWidth' : ''"
>
<ng-content></ng-content>
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,9 +15,10 @@ export type SubmitButtonVariant = Extract<ButtonVariant,
templateUrl: './form-submit-button.component.html',
styleUrls: ['./form-submit-button.component.scss'],
})
export class FormSubmitButtonComponent {
export class FormSubmitButtonComponent implements OnInit{
private parentForm = inject(FormComponent, {optional: true});
private handleError = inject(KLP_FORM_ERROR_HANDLER, {optional: true}) ?? DefaultErrorHandler;
public buttonType: 'submit' | 'button' = 'submit';

@Input() public isLoading = false;
@Input() @HostBinding('class._fullWidth') public fullWidth = false;
Expand All @@ -27,6 +28,14 @@ export class FormSubmitButtonComponent {
@Input() public after: () => Promise<any> = () => 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 }});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class FormComponent implements OnInit, OnDestroy, OnChanges {
@Input() public warnings: Map<AbstractControl, string | TemplateRef<any>> = new Map<AbstractControl, string | TemplateRef<any>>();
@Input() public errors: Map<AbstractControl, string> = new Map<AbstractControl, string>();
@Input() public patchValueInterceptor: (values: any) => Promise<any>;
@Input() public allowSubmitOn: 'buttonAndEnter' | 'buttonOnly' = 'buttonAndEnter';
@Output() public onInjected = new EventEmitter<Record<string, any>>();

// we keep track of what form controls are actually rendered. Only those count when looking at form validation
Expand Down

0 comments on commit dea3dde

Please sign in to comment.