1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-12 14:23:32 +00:00
Files
browser/libs/components/src/form-field/error-summary.component.ts
Will Martin 827c4c0301 [PM-15847] libs/components strict migration (#15738)
This PR migrates `libs/components` to use strict TypeScript.

- Remove `@ts-strict-ignore` from each file in `libs/components` and resolved any new compilation errors
- Converted ViewChild and ContentChild decorators to use the new signal-based queries using the [Angular signal queries migration](https://angular.dev/reference/migrations/signal-queries)
  - Made view/content children `required` where appropriate, eliminating the need for additional null checking. This helped simplify the strict migration.

---

Co-authored-by: Vicki League <vleague@bitwarden.com>
2025-08-18 15:36:45 -04:00

47 lines
1.2 KiB
TypeScript

import { Component, input } from "@angular/core";
import { AbstractControl, UntypedFormGroup } from "@angular/forms";
import { I18nPipe } from "@bitwarden/ui-common";
@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 BitErrorSummary {
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);
}
}