1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-10 13:40:06 +00:00
Files
browser/libs/components/src/form-field/error-summary.component.ts
Oscar Hinton 9efc31534b [PM-28231] Enable component-class-suffix (#17384)
* Enable component-class-suffix

* Rename file
2025-11-18 13:26:38 +01:00

49 lines
1.3 KiB
TypeScript

import { Component, input } from "@angular/core";
import { AbstractControl, UntypedFormGroup } from "@angular/forms";
import { I18nPipe } from "@bitwarden/ui-common";
// FIXME(https://bitwarden.atlassian.net/browse/CL-764): Migrate to OnPush
// eslint-disable-next-line @angular-eslint/prefer-on-push-component-change-detection
@Component({
selector: "bit-error-summary",
template: ` @if (errorCount > 0) {
<i class="bwi bwi-error"></i> {{ "fieldsNeedAttention" | i18n: errorString }}
}`,
host: {
class: "tw-block tw-text-danger tw-mt-2",
"aria-live": "assertive",
},
imports: [I18nPipe],
})
export class BitErrorSummaryComponent {
readonly formGroup = input<UntypedFormGroup>();
get errorCount(): number {
const form = this.formGroup();
return form ? this.getErrorCount(form) : 0;
}
get errorString() {
return this.errorCount.toString();
}
private getErrorCount(form: UntypedFormGroup): number {
return Object.values(form.controls).reduce((acc: number, control: AbstractControl) => {
if (control instanceof UntypedFormGroup) {
return acc + this.getErrorCount(control);
}
if (control.errors == null) {
return acc;
}
if (!control.dirty && control.untouched) {
return acc;
}
return acc + Object.keys(control.errors).length;
}, 0);
}
}