Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getOption does not work when value is an object. #407

Open
mathieutu opened this issue May 11, 2024 · 1 comment
Open

getOption does not work when value is an object. #407

mathieutu opened this issue May 11, 2024 · 1 comment

Comments

@mathieutu
Copy link

mathieutu commented May 11, 2024

Version

  • Vue version: 3

Description

Hi, thanks for your great package.
When using an object as value, we can' t properly set the modelValue from the outside:

const getOption = (val) => {
return eo.value[eo.value.map(o => String(o[valueProp.value])).indexOf(String(val))]
}

Indeed, String(val) will be [Object object], as well as String(o), so the first object item in the list will be selected.

We could either use JSON.stringify instead of String, or let the user provide their own function.

What would you think?

I'd be glad to open a PR.
Mathieu.

Demo

https://jsfiddle.net/jhrbdwk6/

@mathieutu
Copy link
Author

mathieutu commented May 11, 2024

I've patched the package with yarn patch, and

-    return eo.value[eo.value.map(o => String(o[valueProp.value])).indexOf(String(val))]
+    return eo.value[eo.value.map(o => JSON.stringify(o[valueProp.value])).indexOf(JSON.stringify(val))]

works pretty well.

(Btw I also wonder why you use an indexOf, instead of doing a standard find:

return eo.value.find(o => JSON.stringify(o[valueProp.value]) === JSON.stringify(val));

)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant