2024-12-09 11:58:50 -08:00
|
|
|
// FIXME: Update this file to be type safe and remove this and next line
|
|
|
|
|
// @ts-strict-ignore
|
2025-02-03 20:11:59 +01:00
|
|
|
|
2022-06-02 09:34:13 +02:00
|
|
|
import { Component, Input } from "@angular/core";
|
2022-07-26 14:48:11 +02:00
|
|
|
import { AbstractControl, UntypedFormGroup } from "@angular/forms";
|
2022-06-02 09:34:13 +02:00
|
|
|
|
2025-01-17 16:42:31 +01:00
|
|
|
import { I18nPipe } from "@bitwarden/ui-common";
|
2024-12-17 23:29:48 +01:00
|
|
|
|
2022-06-02 09:34:13 +02:00
|
|
|
@Component({
|
|
|
|
|
selector: "bit-error-summary",
|
2025-02-03 20:11:59 +01:00
|
|
|
template: ` @if (errorCount > 0) {
|
2023-03-21 11:28:15 +01:00
|
|
|
<i class="bwi bwi-error"></i> {{ "fieldsNeedAttention" | i18n: errorString }}
|
2025-02-03 20:11:59 +01:00
|
|
|
}`,
|
2022-06-02 09:34:13 +02:00
|
|
|
host: {
|
|
|
|
|
class: "tw-block tw-text-danger tw-mt-2",
|
|
|
|
|
"aria-live": "assertive",
|
|
|
|
|
},
|
2024-12-17 23:29:48 +01:00
|
|
|
standalone: true,
|
2025-02-03 20:11:59 +01:00
|
|
|
imports: [I18nPipe],
|
2022-06-02 09:34:13 +02:00
|
|
|
})
|
|
|
|
|
export class BitErrorSummary {
|
|
|
|
|
@Input()
|
2022-07-26 14:48:11 +02:00
|
|
|
formGroup: UntypedFormGroup;
|
2022-06-02 09:34:13 +02:00
|
|
|
|
|
|
|
|
get errorCount(): number {
|
|
|
|
|
return this.getErrorCount(this.formGroup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get errorString() {
|
|
|
|
|
return this.errorCount.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 14:48:11 +02:00
|
|
|
private getErrorCount(form: UntypedFormGroup): number {
|
2022-06-02 09:34:13 +02:00
|
|
|
return Object.values(form.controls).reduce((acc: number, control: AbstractControl) => {
|
2022-07-26 14:48:11 +02:00
|
|
|
if (control instanceof UntypedFormGroup) {
|
2022-06-02 09:34:13 +02:00
|
|
|
return acc + this.getErrorCount(control);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (control.errors == null) {
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 09:57:25 -07:00
|
|
|
if (!control.dirty && control.untouched) {
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-02 09:34:13 +02:00
|
|
|
return acc + Object.keys(control.errors).length;
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|