mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 23:33:31 +00:00
[PM-9714] Search results should clear and results reset after navigating away from Vault tab but persist if navigating to an Item view (#10378)
* created guard to clear search text when navigating between tabs * removed reset filter from from vault list filter component on destroy and move to guard renamed guard to clear vault state * Fixed bug on chip select when comparing complex objects moved compare values function to utils * Added comment for future reference * moved compare values to a seperate file * fixed lint issue
This commit is contained in:
35
libs/common/src/platform/misc/compare-values.spec.ts
Normal file
35
libs/common/src/platform/misc/compare-values.spec.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { compareValues } from "./compare-values";
|
||||
|
||||
describe("compareValues", () => {
|
||||
it("should return true for equal primitive values", () => {
|
||||
expect(compareValues(1, 1)).toEqual(true);
|
||||
expect(compareValues("bitwarden", "bitwarden")).toEqual(true);
|
||||
expect(compareValues(true, true)).toEqual(true);
|
||||
});
|
||||
|
||||
it("should return false for different primitive values", () => {
|
||||
expect(compareValues(1, 2)).toEqual(false);
|
||||
expect(compareValues("bitwarden", "bitwarden.com")).toEqual(false);
|
||||
expect(compareValues(true, false)).toEqual(false);
|
||||
});
|
||||
|
||||
it("should return true when both values are null", () => {
|
||||
expect(compareValues(null, null)).toEqual(true);
|
||||
});
|
||||
|
||||
it("should compare deeply nested objects correctly", () => {
|
||||
// Deeply nested objects
|
||||
const obj1 = { a: 1, b: { c: 2, d: { e: 3, f: [4, 5, 6] } }, g: [7, 8, { h: 9 }] };
|
||||
const obj2 = { a: 1, b: { c: 2, d: { e: 3, f: [4, 5, 6] } }, g: [7, 8, { h: 9 }] };
|
||||
|
||||
expect(compareValues(obj1, obj2)).toEqual(true);
|
||||
});
|
||||
|
||||
it("should return false for deeply nested objects with different values", () => {
|
||||
// Deeply nested objects
|
||||
const obj1 = { a: 1, b: { c: 2, d: { e: 3, f: [4, 5, 6] } }, g: [7, 8, { h: 9 }] };
|
||||
const obj2 = { a: 1, b: { c: 2, d: { e: 3, f: [4, 5, 7] } }, g: [7, 8, { h: 9 }] };
|
||||
|
||||
expect(compareValues(obj1, obj2)).toEqual(false);
|
||||
});
|
||||
});
|
||||
27
libs/common/src/platform/misc/compare-values.ts
Normal file
27
libs/common/src/platform/misc/compare-values.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Performs deep equality check between two values
|
||||
*
|
||||
* NOTE: This method uses JSON.stringify to compare objects, which may return false
|
||||
* for objects with the same properties but in different order. If order-insensitive
|
||||
* comparison becomes necessary in future, consider updating this method to use a comparison
|
||||
* that checks for property existence and value equality without regard to order.
|
||||
*/
|
||||
export function compareValues<T>(value1: T, value2: T): boolean {
|
||||
if (value1 == null && value2 == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value1 && value2 == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value1 == null && value2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof value1 !== "object" || typeof value2 !== "object") {
|
||||
return value1 === value2;
|
||||
}
|
||||
|
||||
return JSON.stringify(value1) === JSON.stringify(value2);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Component, HostListener, Input, booleanAttribute, signal } from "@angular/core";
|
||||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
|
||||
|
||||
import { compareValues } from "../../../common/src/platform/misc/compare-values";
|
||||
import { ButtonModule } from "../button";
|
||||
import { IconButtonModule } from "../icon-button";
|
||||
import { MenuModule } from "../menu";
|
||||
@@ -108,7 +109,7 @@ export class ChipSelectComponent<T = unknown> implements ControlValueAccessor {
|
||||
*/
|
||||
private findOption(tree: ChipSelectOption<T>, value: T): ChipSelectOption<T> | null {
|
||||
let result = null;
|
||||
if (tree.value !== null && tree.value === value) {
|
||||
if (tree.value !== null && compareValues(tree.value, value)) {
|
||||
return tree;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user