Skip to content

Commit

Permalink
fix: option should display as selected even when value is an object
Browse files Browse the repository at this point in the history
  • Loading branch information
adamberecz committed Sep 16, 2024
1 parent 3a61fcc commit 8fb373f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/composables/useOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import normalize from './../utils/normalize'
import isObject from './../utils/isObject'
import isNullish from './../utils/isNullish'
import arraysEqual from './../utils/arraysEqual'
import objectsEqual from './../utils/objectsEqual'
import toRef from './../utils/toRef'

export default function useOptions (props, context, dep)
Expand Down Expand Up @@ -325,7 +326,10 @@ export default function useOptions (props, context, dep)

switch (mode.value) {
case 'single':
return !isNullish(iv.value) && iv.value[valueProp.value] == option[valueProp.value]
return !isNullish(iv.value) && (
iv.value[valueProp.value] == option[valueProp.value] ||
(typeof iv.value[valueProp.value] === 'object' && typeof option[valueProp.value] === 'object' && objectsEqual(iv.value[valueProp.value], option[valueProp.value]))
)

case 'tags':
case 'multiple':
Expand Down
37 changes: 37 additions & 0 deletions src/utils/objectsEqual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const objectsEqual = (obj1, obj2) => {
// If both are strictly equal, return true
if (obj1 === obj2) {
return true
}

// If either is not an object or is null, return false (handles primitive types and null)
if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) {
return false
}

// Get the keys of both objects
const keys1 = Object.keys(obj1)
const keys2 = Object.keys(obj2)

// If they have a different number of keys, they're not equal
if (keys1.length !== keys2.length) {
return false
}

// Compare each key-value pair recursively
for (let key of keys1) {
// Check if both objects have the same key
if (!keys2.includes(key)) {
return false
}

// Recursively compare the values
if (!objectsEqual(obj1[key], obj2[key])) {
return false
}
}

return true
}

export default objectsEqual

0 comments on commit 8fb373f

Please sign in to comment.