Skip to content

Commit 8fb373f

Browse files
committed
fix: option should display as selected even when value is an object
1 parent 3a61fcc commit 8fb373f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/composables/useOptions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import normalize from './../utils/normalize'
33
import isObject from './../utils/isObject'
44
import isNullish from './../utils/isNullish'
55
import arraysEqual from './../utils/arraysEqual'
6+
import objectsEqual from './../utils/objectsEqual'
67
import toRef from './../utils/toRef'
78

89
export default function useOptions (props, context, dep)
@@ -325,7 +326,10 @@ export default function useOptions (props, context, dep)
325326

326327
switch (mode.value) {
327328
case 'single':
328-
return !isNullish(iv.value) && iv.value[valueProp.value] == option[valueProp.value]
329+
return !isNullish(iv.value) && (
330+
iv.value[valueProp.value] == option[valueProp.value] ||
331+
(typeof iv.value[valueProp.value] === 'object' && typeof option[valueProp.value] === 'object' && objectsEqual(iv.value[valueProp.value], option[valueProp.value]))
332+
)
329333

330334
case 'tags':
331335
case 'multiple':

src/utils/objectsEqual.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const objectsEqual = (obj1, obj2) => {
2+
// If both are strictly equal, return true
3+
if (obj1 === obj2) {
4+
return true
5+
}
6+
7+
// If either is not an object or is null, return false (handles primitive types and null)
8+
if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) {
9+
return false
10+
}
11+
12+
// Get the keys of both objects
13+
const keys1 = Object.keys(obj1)
14+
const keys2 = Object.keys(obj2)
15+
16+
// If they have a different number of keys, they're not equal
17+
if (keys1.length !== keys2.length) {
18+
return false
19+
}
20+
21+
// Compare each key-value pair recursively
22+
for (let key of keys1) {
23+
// Check if both objects have the same key
24+
if (!keys2.includes(key)) {
25+
return false
26+
}
27+
28+
// Recursively compare the values
29+
if (!objectsEqual(obj1[key], obj2[key])) {
30+
return false
31+
}
32+
}
33+
34+
return true
35+
}
36+
37+
export default objectsEqual

0 commit comments

Comments
 (0)