primereact icon indicating copy to clipboard operation
primereact copied to clipboard

Fix #6733: add null check condition in getOptionValue(Dropdown)

Open KumJungMin opened this issue 1 year ago • 1 comments

Defect Fixes

  • fix #6733
  • example: https://stackblitz.com/edit/vitejs-vite-5esx3k?file=src%2FApp.jsx,package.json&terminal=dev 스크린샷 2024-07-01 15 18 42



how to resolve

1. add null check

  • when default value is 0, the selected option is not selected
  • the resolveFieldData function returns null if the value is not found, so we need to check if the value is not null :)
// before
const getOptionValue = (option) => {
	return props.optionValue ? ObjectUtils.resolveFieldData(option, props.optionValue) : ObjectUtils.resolveFieldData(option, 'value') || option;
};
// after

const getOptionValue = (option) => {
    if (props.optionValue) {
        return ObjectUtils.resolveFieldData(option, props.optionValue);
    } else {
        const dataValue = ObjectUtils.resolveFieldData(option, 'value');
        return dataValue !== null ? dataValue : option; // null checking!
    }
};

2. fix wrong check condition

  • hasSelectedOption is function.
const hasSelectedOption = () => {
    return ObjectUtils.isNotEmpty(props.value);
};

  • I changed hasSelectedOption to hasSelectedOption() to ensure the condition evaluates the result of the function call rather than a function reference.
// before
const findSelectedOptionIndex = () => {
    return hasSelectedOption ? visibleOptions.findIndex((option) => isValidSelectedOption(option)) : -1;
};
// after 
const findSelectedOptionIndex = () => {
    return hasSelectedOption() ? visibleOptions.findIndex((option) => isValidSelectedOption(option)) : -1;
};


result

https://github.com/primefaces/primereact/assets/37934668/3e4c5e13-0880-49e5-b6c1-e265cecfff3f

KumJungMin avatar Jul 01 '24 06:07 KumJungMin

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
primereact ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 9:29am
primereact-v9 ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 9:29am

vercel[bot] avatar Jul 01 '24 06:07 vercel[bot]

Thank you for your patience during our national vacation. We encountered and resolved an issue with getOptionValue and getOptionLabel reported by one of our PRO users. We prioritized fixing this problem as soon as possible before reviewing GitHub issues.

We appreciate your contribution and would like to ask you to check the latest version of the master branch to see if everything works fine for you as well.

Thank you for your support and understanding. @KumJungMin

nitrogenous avatar Jul 03 '24 09:07 nitrogenous

@nitrogenous Oh Sorry, I just checked wrong. I checked again and this issue was resolved in master. thank you for comment!

KumJungMin avatar Jul 03 '24 11:07 KumJungMin

safe to close this PR and ticket?

melloware avatar Jul 03 '24 11:07 melloware

@nitrogenous Oh Sorry, I just checked wrong. I checked again and this issue was resolved in master. thank you for comment!

I wish I had seen your pull request before. I am really sorry that we couldn't merge your PR. I would love to see your further contributions. Thank you for your support to our community

nitrogenous avatar Jul 03 '24 11:07 nitrogenous

@nitrogenous I'm glad the issue was resolved quickly. Even though it wasn't merged, it was nice to be able to read a lot of the code while trying to contribute! Thank you again for your comment. Have a nice day 😄

KumJungMin avatar Jul 03 '24 11:07 KumJungMin