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

Fix #6858: Dropdown crashes when label is an empty string #6871

Conversation

VytautasLozickas
Copy link
Contributor

Fix #6858 - resolved a crash when passing an empty string as a label value for the Dropdown's options.

The solution was to make the getOptionLabel function in the Dropdown component always return a string. The return value of this function ends up in the DropdownItem component and it is used directly as a child of the span element (if no item template is provided):

components/lib/dropdown/DropdownItem.js:79

<span {...itemGroupLabelProps}>{content}</span>

This clearly shows that there is no intention for the value here to be anything other than a string (it could also be a ReactNode of course, in the case of a custom item template being used). Well ok, it can technically also be null or undefined, but that doesn't change the point. Further cementing the fact that the return value of the getOptionLabel function should always be a string is a call of toLocaleLowerCase method on the return value of this function. For example in the Dropdown search logic:

components/lib/dropdown/Dropdown.js:297

return isValidOption(option) && getOptionLabel(option)?.toLocaleLowerCase(props.filterLocale).startsWith(searchValue.current.toLocaleLowerCase(props.filterLocale));

The current return value of the getOptionLabel function was pretty much any, meaning that an entire option object could have also been returned, which has no (apparent) use within the Dropdown component and simply crashes the component.

This change also resolves another possible crash when the optionLabel prop is provided to the Dropdown component with a custom key/path to a label value from the options object, but the actual key/path on the options object does not exist.

Another side effect of this change is that boolean values passed as option labels to the Dropdown component now actually get rendered as true or false (which is kind of nice, IMO).

Copy link

vercel bot commented Jul 11, 2024

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 11, 2024 10:09am
primereact-v9 ⬜️ Ignored (Inspect) Visit Preview Jul 11, 2024 10:09am

@VytautasLozickas VytautasLozickas changed the title Fix primefaces#6858: Dropdown crashes when label is an empty string Fix #6858: Dropdown crashes when label is an empty string Jul 11, 2024
@melloware melloware added the Type: Bug Issue contains a defect related to a specific component. label Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dropdown: Object not valid when you use an Empty String ""
2 participants