primereact icon indicating copy to clipboard operation
primereact copied to clipboard

Option with value 0 has different behavior than any other number in dropdown

Open espentveit opened this issue 1 year ago • 6 comments

Describe the bug

Hi,

Seems like the behavior has been changed by commit 7312fe466e189581c53c3fe43bf3381db4b59455.

return props.optionValue ? ObjectUtils.resolveFieldData(option, props.optionValue) : ObjectUtils.resolveFieldData(option, 'value') || option;

The || here passes option through if the value is 0.

0 || "Test" == "Test" true

Reproducer

No response

PrimeReact version

10.6.5

React version

18.x

Language

TypeScript

Build / Runtime

Create React App (CRA)

Browser(s)

No response

Steps to reproduce the behavior

No response

Expected behavior

No response

espentveit avatar May 13 '24 12:05 espentveit

cc @Rekl0w

melloware avatar May 13 '24 12:05 melloware

it probably needs to be ObjectUtils.isNotEmpty(props.optionValue)

melloware avatar May 13 '24 12:05 melloware

No, it is weird that i tried almost everything but not works for this. I also tried to use old version of this function but still the same behavior. I guess this is another bug. @melloware

Rekl0w avatar May 13 '24 14:05 Rekl0w

The case where I tested was for the one with the 'value' key set. Maybe the isNotEmpty should check the output of resolveFieldData?

const getOptionValue = (option) => {
  if (props.optionValue) {
    return ObjectUtils.resolveFieldData(option, props.optionValue);
  } else {
    const optionValue = ObjectUtils.resolveFieldData(option, "value");
    return ObjectUtils.isNotEmpty(optionValue) ? optionValue : option;
  }
};

Edit: Or maybe use

const getOptionValue = (option) => {
  if (props.optionValue) {
    return ObjectUtils.resolveFieldData(option, props.optionValue);
  } else {
    return option.hasOwnProperty("value") ? ObjectUtils.resolveFieldData(option, "value") : option;
  }
};

espentveit avatar May 13 '24 15:05 espentveit

Both codes causes the blank dropdown options.

image

If you try the old codes before i've changed, it behaves the same. Like i said i don't think that code causes this bug.

Rekl0w avatar May 13 '24 17:05 Rekl0w

Ok. I'll test some more tomorrow

espentveit avatar May 13 '24 20:05 espentveit

Fixed with: https://github.com/primefaces/primereact/pull/7083

melloware avatar Aug 24 '24 11:08 melloware