mirror of
https://github.com/bitwarden/browser
synced 2025-12-12 14:23:32 +00:00
Fix angular performance issue on color-password.component (#14560)
* Fix performance issue, by using track $index instead of character as it can contain duplicate characters * Migrate component to use Angular signal inputs * Make file ts-strict compliant --------- Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
9e467163db
commit
26ecf3191c
@@ -1,7 +1,4 @@
|
|||||||
// FIXME: Update this file to be type safe and remove this and next line
|
import { Component, computed, HostBinding, input } from "@angular/core";
|
||||||
// @ts-strict-ignore
|
|
||||||
|
|
||||||
import { Component, HostBinding, Input } from "@angular/core";
|
|
||||||
|
|
||||||
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
||||||
|
|
||||||
@@ -14,10 +11,10 @@ enum CharacterType {
|
|||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: "bit-color-password",
|
selector: "bit-color-password",
|
||||||
template: `@for (character of passwordArray; track character; let i = $index) {
|
template: `@for (character of passwordCharArray(); track $index; let i = $index) {
|
||||||
<span [class]="getCharacterClass(character)">
|
<span [class]="getCharacterClass(character)">
|
||||||
<span>{{ character }}</span>
|
<span>{{ character }}</span>
|
||||||
@if (showCount) {
|
@if (showCount()) {
|
||||||
<span class="tw-whitespace-nowrap tw-text-xs tw-leading-5 tw-text-main">{{ i + 1 }}</span>
|
<span class="tw-whitespace-nowrap tw-text-xs tw-leading-5 tw-text-main">{{ i + 1 }}</span>
|
||||||
}
|
}
|
||||||
</span>
|
</span>
|
||||||
@@ -25,8 +22,13 @@ enum CharacterType {
|
|||||||
standalone: true,
|
standalone: true,
|
||||||
})
|
})
|
||||||
export class ColorPasswordComponent {
|
export class ColorPasswordComponent {
|
||||||
@Input() password: string = null;
|
password = input<string>("");
|
||||||
@Input() showCount = false;
|
showCount = input<boolean>(false);
|
||||||
|
|
||||||
|
// Convert to an array to handle cases that strings have special characters, i.e.: emoji.
|
||||||
|
passwordCharArray = computed(() => {
|
||||||
|
return Array.from(this.password() ?? "");
|
||||||
|
});
|
||||||
|
|
||||||
characterStyles: Record<CharacterType, string[]> = {
|
characterStyles: Record<CharacterType, string[]> = {
|
||||||
[CharacterType.Emoji]: [],
|
[CharacterType.Emoji]: [],
|
||||||
@@ -40,16 +42,11 @@ export class ColorPasswordComponent {
|
|||||||
return ["tw-min-w-0", "tw-whitespace-pre-wrap", "tw-break-all"];
|
return ["tw-min-w-0", "tw-whitespace-pre-wrap", "tw-break-all"];
|
||||||
}
|
}
|
||||||
|
|
||||||
get passwordArray() {
|
|
||||||
// Convert to an array to handle cases that strings have special characters, i.e.: emoji.
|
|
||||||
return Array.from(this.password);
|
|
||||||
}
|
|
||||||
|
|
||||||
getCharacterClass(character: string) {
|
getCharacterClass(character: string) {
|
||||||
const charType = this.getCharacterType(character);
|
const charType = this.getCharacterType(character);
|
||||||
const charClass = this.characterStyles[charType];
|
const charClass = this.characterStyles[charType];
|
||||||
|
|
||||||
if (this.showCount) {
|
if (this.showCount()) {
|
||||||
return charClass.concat([
|
return charClass.concat([
|
||||||
"tw-inline-flex",
|
"tw-inline-flex",
|
||||||
"tw-flex-col",
|
"tw-flex-col",
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ describe("LoginCredentialsViewComponent", () => {
|
|||||||
|
|
||||||
const passwordColor = passwordField.query(By.directive(ColorPasswordComponent));
|
const passwordColor = passwordField.query(By.directive(ColorPasswordComponent));
|
||||||
|
|
||||||
expect(passwordColor.componentInstance.password).toBe(cipher.login.password);
|
expect(passwordColor.componentInstance.password()).toBe(cipher.login.password);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("records event", () => {
|
it("records event", () => {
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ describe("PasswordHistoryViewComponent", () => {
|
|||||||
it("renders all passwords", () => {
|
it("renders all passwords", () => {
|
||||||
const passwords = fixture.debugElement.queryAll(By.directive(ColorPasswordComponent));
|
const passwords = fixture.debugElement.queryAll(By.directive(ColorPasswordComponent));
|
||||||
|
|
||||||
expect(passwords.map((password) => password.componentInstance.password)).toEqual([
|
expect(passwords.map((password) => password.componentInstance.password())).toEqual([
|
||||||
"bad-password-1",
|
"bad-password-1",
|
||||||
"bad-password-2",
|
"bad-password-2",
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user