@deepkit/desktop-ui
Version:
Library for desktop UI widgets in Angular 10+
137 lines (118 loc) • 4.12 kB
text/typescript
/*
* Deepkit Framework
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License.
*
* You should have received a copy of the MIT License along with this program.
*/
import { booleanAttribute, ChangeDetectorRef, Component, ContentChild, HostListener, input, output, signal, SkipSelf } from '@angular/core';
import { FormGroup, FormsModule, NgControl, ReactiveFormsModule } from '@angular/forms';
import { KeyValuePipe } from '@angular/common';
export class FormRowComponent {
label = input<string>('');
description = input<string>('');
labelWidth = input<number>();
left = input(false, { transform: booleanAttribute });
ngControl?: NgControl;
isString(v: any) {
return 'string' === typeof v;
}
}
export class FormComponent {
formGroup = input<FormGroup>(new FormGroup({}));
disabled = input<boolean>(false);
submit = input<() => Promise<any> | any>();
success = output();
error = output();
errorText = signal('');
submitting = signal(false);
constructor(
protected cd: ChangeDetectorRef,
protected cdParent: ChangeDetectorRef,
) {
}
onEnter(event: KeyboardEvent) {
if (this.submit() && event.key.toLowerCase() === 'enter'
&& event.target && (event.target as HTMLElement).tagName.toLowerCase() === 'input') {
this.submitForm();
}
}
get invalid() {
return this.formGroup().invalid;
}
async submitForm() {
if (this.disabled()) return;
if (this.submitting()) return;
const formGroup = this.formGroup();
if (formGroup.invalid) return;
this.errorText.set('');
this.submitting.set(true);
try {
const submit = this.submit();
if (submit) {
try {
await submit();
this.success.emit();
} catch (error: any) {
this.error.emit(error);
if (error.errors && error.errors[0]) {
//we got a validation-like error object
for (const item of error.errors) {
const control = formGroup.get(item.path);
if (control) {
control.setErrors({
...control.errors,
[item.code]: item.message,
});
}
}
} else {
this.errorText.set(error.message || error);
}
}
}
} finally {
this.submitting.set(false);
}
}
}