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

Dropdown's useOptionAsValue does not take precedence #7308

Closed
dingzhanjun opened this issue Oct 4, 2024 · 15 comments
Closed

Dropdown's useOptionAsValue does not take precedence #7308

dingzhanjun opened this issue Oct 4, 2024 · 15 comments
Labels
Resolution: By Design The behavior in the issue is by design and the component exhibits the expected behavior
Milestone

Comments

@dingzhanjun
Copy link

dingzhanjun commented Oct 4, 2024

Describe the bug

I use dropdown to display a list of data, in the onChange event, the event.value is always property's ID instead of whole option object when useOptionAsValue presents

Reproducer

https://stackblitz.com/edit/vitejs-vite-rrgaoc?file=src%2FApp.tsx

System Information

React: 18.2
Primereact: latest

Steps to reproduce the behavior

select one item from dropdown, it display option's ID when both useOptionAsValue and optionValue present

Expected behavior

when both useOptionAsValue and optionValue present, useOptionAsValue should take precedence and select value should be the whole option object

@dingzhanjun dingzhanjun added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Oct 4, 2024
@melloware
Copy link
Member

@Rekl0w related to #7134

@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Oct 4, 2024
@Rekl0w
Copy link
Contributor

Rekl0w commented Oct 4, 2024

This will work, you shouldn't use optionValue prop if you want to get the whole object. If you have an object that has value key but you don't want to pick the value key, then you should use the useOptionAsValue prop, yet it works both ways. I changed the PrimeReact version from latest to 10.8.3 at Stackblitz link.

@melloware melloware added Resolution: By Design The behavior in the issue is by design and the component exhibits the expected behavior and removed Type: Bug Issue contains a defect related to a specific component. labels Oct 4, 2024
@melloware melloware added this to the 10.8.3 milestone Oct 4, 2024
@dingzhanjun
Copy link
Author

Is the latest not the same with 10.8.3?

@melloware
Copy link
Member

what we have seen on StackBlitz is latest is sometimes cached and changing it to 10.8.3 or whatever version forces it to reload.

@Rekl0w
Copy link
Contributor

Rekl0w commented Oct 4, 2024

Agreed. @melloware

@dingzhanjun
Copy link
Author

dingzhanjun commented Oct 4, 2024

Thanks for your quick response, there is something I do not understand, here is the code from dropdown

const getOptionValue = (option) => {
            if (props.useOptionAsValue) {
                return option;
            }

            const optionValue = props.optionValue ? ObjectUtils.resolveFieldData(option, props.optionValue) : option ? option['value'] : ObjectUtils.resolveFieldData(option, 'value');

            return props.optionValue || ObjectUtils.isNotEmpty(optionValue) ? optionValue : option;
        };

the code shows that useOptionAsValue should always take precedence

@Rekl0w
Copy link
Contributor

Rekl0w commented Oct 4, 2024

It is already. You can try it with or without useOptionAsValue prop at my provided Stackblitz link.

@dingzhanjun
Copy link
Author

your does work, I was wondering why, it does not make sense according to the code

@melloware melloware closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2024
@Rekl0w
Copy link
Contributor

Rekl0w commented Oct 4, 2024

Which code are you talking about ? Stackblitz link or codebase ?

@dingzhanjun
Copy link
Author

dingzhanjun commented Oct 4, 2024

the code snippet posted above from code base, props.useOptionAsValue should take precedence:

const getOptionValue = (option) => {
            if (props.useOptionAsValue) {
                return option;
            }

            const optionValue = props.optionValue ? ObjectUtils.resolveFieldData(option, props.optionValue) : option ? option['value'] : ObjectUtils.resolveFieldData(option, 'value');

            return props.optionValue || ObjectUtils.isNotEmpty(optionValue) ? optionValue : option;
        };

@Rekl0w
Copy link
Contributor

Rekl0w commented Oct 4, 2024

Like i said it already has precedence. It is placed top of the function and has return.

@dingzhanjun
Copy link
Author

Sorry, I misunderstood, could my stackbliz be because of cache that does not load the latest package

@Rekl0w
Copy link
Contributor

Rekl0w commented Oct 4, 2024

It can be.

@dingzhanjun
Copy link
Author

thank you so much!

@Rekl0w
Copy link
Contributor

Rekl0w commented Oct 4, 2024

You're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: By Design The behavior in the issue is by design and the component exhibits the expected behavior
Projects
None yet
Development

No branches or pull requests

3 participants