1
0
mirror of https://github.com/bitwarden/browser synced 2026-03-01 02:51:24 +00:00

[CL-352] Fix Angular errors related to form element changes (#10211)

This commit is contained in:
Victoria League
2024-07-30 11:48:45 -04:00
committed by GitHub
parent 625da0776d
commit b81c2d22f1
4 changed files with 38 additions and 20 deletions

View File

@@ -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"
>
<div
#prefixSlot
[hidden]="prefixSlot.childElementCount === 0"
#prefixContainer
class="tw-flex tw-items-center tw-gap-1 tw-pl-3 tw-py-2"
[hidden]="!prefixHasChildren()"
>
<ng-container *ngTemplateOutlet="prefixContent"></ng-container>
</div>
<div
class="default-content tw-w-full tw-relative tw-py-2 has-[bit-select]:tw-p-0 has-[bit-multi-select]:tw-p-0 has-[input:read-only:not([hidden])]:tw-bg-secondary-100 has-[textarea:read-only:not([hidden])]:tw-bg-secondary-100"
[ngClass]="[
prefixSlot.childElementCount === 0 ? 'tw-rounded-l-lg tw-pl-3' : '',
suffixSlot.childElementCount === 0 ? 'tw-rounded-r-lg tw-pr-3' : '',
prefixHasChildren() ? '' : 'tw-rounded-l-lg tw-pl-3',
suffixHasChildren() ? '' : 'tw-rounded-r-lg tw-pr-3',
]"
>
<ng-container *ngTemplateOutlet="defaultContent"></ng-container>
</div>
<div
#suffixSlot
[hidden]="suffixSlot.childElementCount === 0"
#suffixContainer
class="tw-flex tw-items-center tw-gap-1 tw-pr-3 tw-py-2"
[hidden]="!suffixHasChildren()"
>
<ng-container *ngTemplateOutlet="suffixContent"></ng-container>
</div>
@@ -90,8 +90,8 @@
}"
>
<div
#prefixSlot
[hidden]="prefixSlot.childElementCount === 0"
#prefixContainer
[hidden]="!prefixHasChildren()"
class="tw-flex tw-items-center tw-gap-1 tw-pl-3 tw-py-2"
>
<ng-container *ngTemplateOutlet="prefixContent"></ng-container>
@@ -100,8 +100,8 @@
<ng-container *ngTemplateOutlet="defaultContent"></ng-container>
</div>
<div
#suffixSlot
[hidden]="suffixSlot.childElementCount === 0"
#suffixContainer
[hidden]="!suffixHasChildren()"
class="tw-flex tw-items-center tw-gap-1 tw-pr-3 tw-py-2"
>
<ng-container *ngTemplateOutlet="suffixContent"></ng-container>

View File

@@ -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<HTMLDivElement>;
@ViewChild("suffixContainer") suffixContainer: ElementRef<HTMLDivElement>;
@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);
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}