-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(filterable-select): cannot backspace with object as values
refactored the code to run setMatchingText only when the object change instead of on every render fixes: #7064
- Loading branch information
1 parent
e2c1dd9
commit eae40b5
Showing
4 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/components/select/__internal__/utils/are-objects-equal.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import areObjectsEqual from "./are-objects-equal"; | ||
|
||
const value1 = { id: "Amber", value: 1, text: "Amber" }; | ||
const value2 = { id: "Amber", value: 1, text: "Amber" }; | ||
const value3 = { id: "Black", value: 2, text: "Black" }; | ||
const value4 = { id: "Black", value: 2 }; | ||
const value5 = { ids: "Amber", values: 1, text: "Amber" }; | ||
const invalidParam = "notAnObject" as unknown as Record<string, unknown>; | ||
|
||
test("returns true when the objects have the same keys and values", () => { | ||
expect(areObjectsEqual(value1, value2)).toBe(true); | ||
}); | ||
|
||
test("returns false when the objects have the same keys but not the same values", () => { | ||
expect(areObjectsEqual(value1, value3)).toBe(false); | ||
}); | ||
|
||
test("returns false when the objects do not have the same keys length", () => { | ||
expect(areObjectsEqual(value1, value4)).toBe(false); | ||
}); | ||
|
||
test("returns false when the objects have the same keys length but different key names", () => { | ||
expect(areObjectsEqual(value1, value5)).toBe(false); | ||
}); | ||
|
||
test("throws error if both parameters are not objects", () => { | ||
expect(areObjectsEqual(value1, invalidParam)).toBe(false); | ||
expect(areObjectsEqual(invalidParam, value1)).toBe(false); | ||
}); |
32 changes: 32 additions & 0 deletions
32
src/components/select/__internal__/utils/are-objects-equal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export default function areObjectsEqual( | ||
obj1: Record<string, unknown>, | ||
obj2: Record<string, unknown>, | ||
) { | ||
// Check if both arguments are objects | ||
if ( | ||
typeof obj1 !== "object" || | ||
typeof obj2 !== "object" || | ||
obj1 === null || | ||
obj2 === null | ||
) { | ||
return false; | ||
} | ||
// Get the keys of both objects | ||
const keys1 = Object.keys(obj1); | ||
const keys2 = Object.keys(obj2); | ||
// If the number of keys is different, objects aren't equal | ||
if (keys1.length !== keys2.length) { | ||
return false; | ||
} // Compare keys and values | ||
for (const key of keys1) { | ||
// Check if obj2 has the key and values are strictly equal | ||
if ( | ||
!Object.prototype.hasOwnProperty.call(obj2, key) || | ||
obj1[key] !== obj2[key] | ||
) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters