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: When the options object has a field named value, the selected value automatically takes the value of that field instead of the entire object #7134

Closed
mohamadbdeir opened this issue Sep 8, 2024 · 8 comments · Fixed by #7141 · May be fixed by leoo1992/GeradorQRCode#95
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@mohamadbdeir
Copy link
Contributor

Describe the bug

When the options objects for the Dropdown have a field named 'value' the selected value would use that as a value instead of the entire object. I'm not sure if that is a mistake or done on purpose but seems odd .

Reproducer

https://stackblitz.com/edit/stackblitz-starters-kmdg5m?file=src%2FApp.tsx

System Information

"primereact": "10.0.9"

Steps to reproduce the behavior

Use the dropdown component, you can try and copy the one from the documentation, rename any field of the options objects and give it the the name 'value' and you will see that the selected value for the dropdown takes that value instead of the entire object

Expected behavior

It should define the selected value as the entire object and not a single field.

@mohamadbdeir mohamadbdeir added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Sep 8, 2024
@Rekl0w
Copy link
Contributor

Rekl0w commented Sep 9, 2024

If you give the anything but value key, it'll give the entire object (Example: val). This was requested change that if you don't give the optionValue prop but if you have the value key on your object, it will automatically pick the value.

@mohamadbdeir
Copy link
Contributor Author

mohamadbdeir commented Sep 9, 2024

@Rekl0w

If you give the anything but value key, it'll give the entire object (Example: val). This was
requested change that if you don't give the optionValue prop but if you have the value key on your object, it will automatically pick the value.

I see, but if my options have a key named value, how do I set optionValue prop to use the entire object ?

@Rekl0w
Copy link
Contributor

Rekl0w commented Sep 9, 2024

You can't. You have to change the key name.

@mohamadbdeir
Copy link
Contributor Author

You can't. You have to change the key name.

But we don't always have control over the object structure especially when it's fetched from a backend we don't control..
Thinking about submitting a pull request

@mohamadbdeir
Copy link
Contributor Author

@melloware what do you think ?

@sja-cslab
Copy link
Contributor

Just want to reference my issues because they seem to be related

#6589
#6822

@melloware
Copy link
Member

@mohamadbdeir i think this has been reported before. Multliselect and DropDown seem to handle it differently.

MultiSelect:

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

            if (props.optionValue) {
                const data = ObjectUtils.resolveFieldData(option, props.optionValue);

                return data !== null ? data : option;
            }

            return option && option.value !== undefined ? option.value : option;
        };

DropDown:

        const getOptionValue = (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;
        };

It looks like Multiselect has an extra props.useOptionAsValue ?

@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 Sep 9, 2024
Rekl0w added a commit to Rekl0w/primereact that referenced this issue Sep 9, 2024
@Rekl0w
Copy link
Contributor

Rekl0w commented Sep 9, 2024

PR submitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment