From b81c2d22f1fc2456b6fe4023f819ce518a88c934 Mon Sep 17 00:00:00 2001 From: Victoria League Date: Tue, 30 Jul 2024 11:48:45 -0400 Subject: [PATCH] [CL-352] Fix Angular errors related to form element changes (#10211) --- .../src/form-field/form-field.component.html | 20 +++++++++---------- .../src/form-field/form-field.component.ts | 10 ++++++++++ .../src/form-field/prefix.directive.ts | 14 ++++++++----- .../src/form-field/suffix.directive.ts | 14 ++++++++----- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/libs/components/src/form-field/form-field.component.html b/libs/components/src/form-field/form-field.component.html index ae7445085d6..d621e2fd695 100644 --- a/libs/components/src/form-field/form-field.component.html +++ b/libs/components/src/form-field/form-field.component.html @@ -20,25 +20,25 @@ class="tw-gap-1 tw-bg-background tw-rounded-lg tw-flex tw-min-h-11 [&:not(:has(button:enabled)):has(input:read-only)]:tw-bg-secondary-100 [&:not(:has(button:enabled)):has(textarea:read-only)]:tw-bg-secondary-100" >
@@ -90,8 +90,8 @@ }" >
@@ -100,8 +100,8 @@
diff --git a/libs/components/src/form-field/form-field.component.ts b/libs/components/src/form-field/form-field.component.ts index b45001f58eb..0651f3fa8d5 100644 --- a/libs/components/src/form-field/form-field.component.ts +++ b/libs/components/src/form-field/form-field.component.ts @@ -2,6 +2,7 @@ import { AfterContentChecked, Component, ContentChild, + ElementRef, HostBinding, HostListener, Input, @@ -26,6 +27,9 @@ export class BitFormFieldComponent implements AfterContentChecked { @ContentChild(BitHintComponent) hint: BitHintComponent; @ContentChild(BitLabel) label: BitLabel; + @ViewChild("prefixContainer") prefixContainer: ElementRef; + @ViewChild("suffixContainer") suffixContainer: ElementRef; + @ViewChild(BitErrorComponent) error: BitErrorComponent; @Input({ transform: booleanAttribute }) @@ -35,6 +39,9 @@ export class BitFormFieldComponent implements AfterContentChecked { @Input({ transform: booleanAttribute }) disableReadOnlyBorder = false; + protected prefixHasChildren = signal(false); + protected suffixHasChildren = signal(false); + get inputBorderClasses(): string { const shouldFocusBorderAppear = this.defaultContentIsFocused(); @@ -91,5 +98,8 @@ export class BitFormFieldComponent implements AfterContentChecked { } else { this.input.ariaDescribedBy = undefined; } + + this.prefixHasChildren.set(this.prefixContainer?.nativeElement.childElementCount > 0); + this.suffixHasChildren.set(this.suffixContainer?.nativeElement.childElementCount > 0); } } diff --git a/libs/components/src/form-field/prefix.directive.ts b/libs/components/src/form-field/prefix.directive.ts index 371cc51c78b..bcfc9d01fe4 100644 --- a/libs/components/src/form-field/prefix.directive.ts +++ b/libs/components/src/form-field/prefix.directive.ts @@ -1,4 +1,4 @@ -import { Directive, HostBinding, Input, Optional } from "@angular/core"; +import { AfterContentInit, Directive, HostBinding, Input, OnInit, Optional } from "@angular/core"; import { BitIconButtonComponent } from "../icon-button/icon-button.component"; @@ -7,15 +7,13 @@ import { BitFormFieldComponent } from "./form-field.component"; @Directive({ selector: "[bitPrefix]", }) -export class BitPrefixDirective { +export class BitPrefixDirective implements OnInit, AfterContentInit { @HostBinding("class") @Input() get classList() { return ["tw-text-muted"]; } @HostBinding("attr.aria-describedby") - get ariaDescribedBy() { - return this.parentFormField?.label?.id || null; - } + protected ariaDescribedBy: string; constructor( @Optional() private parentFormField: BitFormFieldComponent, @@ -27,4 +25,10 @@ export class BitPrefixDirective { this.iconButtonComponent.size = "small"; } } + + ngAfterContentInit() { + if (this.parentFormField?.label?.id) { + this.ariaDescribedBy = this.parentFormField.label.id; + } + } } diff --git a/libs/components/src/form-field/suffix.directive.ts b/libs/components/src/form-field/suffix.directive.ts index e79d78aeeed..39fcce916cc 100644 --- a/libs/components/src/form-field/suffix.directive.ts +++ b/libs/components/src/form-field/suffix.directive.ts @@ -1,4 +1,4 @@ -import { Directive, HostBinding, Input, Optional } from "@angular/core"; +import { AfterContentInit, Directive, HostBinding, Input, OnInit, Optional } from "@angular/core"; import { BitIconButtonComponent } from "../icon-button/icon-button.component"; @@ -7,15 +7,13 @@ import { BitFormFieldComponent } from "./form-field.component"; @Directive({ selector: "[bitSuffix]", }) -export class BitSuffixDirective { +export class BitSuffixDirective implements OnInit, AfterContentInit { @HostBinding("class") @Input() get classList() { return ["tw-text-muted"]; } @HostBinding("attr.aria-describedby") - get ariaDescribedBy() { - return this.parentFormField?.label?.id || null; - } + protected ariaDescribedBy: string; constructor( @Optional() private parentFormField: BitFormFieldComponent, @@ -27,4 +25,10 @@ export class BitSuffixDirective { this.iconButtonComponent.size = "small"; } } + + ngAfterContentInit() { + if (this.parentFormField?.label?.id) { + this.ariaDescribedBy = this.parentFormField.label.id; + } + } }