From 947b7d6fa890b1f64c0f9fc8926d3746cec92572 Mon Sep 17 00:00:00 2001 From: Vytautas Lozickas <36911475+VytautasLozickas@users.noreply.github.com> Date: Thu, 11 Jul 2024 16:21:53 +0300 Subject: [PATCH] Re-fix primefaces#6561: Return scalar values directly as a label for Dropdown (#6872) * Fix primefaces#6561: Return scalar values directly as a label for Dropdown * Reformat code --- components/lib/dropdown/Dropdown.js | 6 +++++- components/lib/utils/ObjectUtils.js | 4 ++++ components/lib/utils/utils.d.ts | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/components/lib/dropdown/Dropdown.js b/components/lib/dropdown/Dropdown.js index 7b1782fa7d..1980f30988 100644 --- a/components/lib/dropdown/Dropdown.js +++ b/components/lib/dropdown/Dropdown.js @@ -886,7 +886,11 @@ export const Dropdown = React.memo( }; const getOptionLabel = (option) => { - const optionLabel = props.optionLabel ? ObjectUtils.resolveFieldData(option, props.optionLabel) : option ? option['label'] : ObjectUtils.resolveFieldData(option, 'label'); + if (ObjectUtils.isScalar(option)) { + return `${option}`; + } + + const optionLabel = props.optionLabel ? ObjectUtils.resolveFieldData(option, props.optionLabel) : option['label']; return `${optionLabel}`; }; diff --git a/components/lib/utils/ObjectUtils.js b/components/lib/utils/ObjectUtils.js index 49b346ad08..659e3f5c43 100644 --- a/components/lib/utils/ObjectUtils.js +++ b/components/lib/utils/ObjectUtils.js @@ -386,6 +386,10 @@ export default class ObjectUtils { return /^[a-zA-Z\u00C0-\u017F]$/.test(char); } + static isScalar(value) { + return value != null && (typeof value === 'string' || typeof value === 'number' || typeof value === 'bigint' || typeof value === 'boolean'); + } + /** * Firefox-v103 does not currently support the "findLast" method. It is stated that this method will be supported with Firefox-v104. * https://caniuse.com/mdn-javascript_builtins_array_findlast diff --git a/components/lib/utils/utils.d.ts b/components/lib/utils/utils.d.ts index 3c40163b32..db8c8d7954 100644 --- a/components/lib/utils/utils.d.ts +++ b/components/lib/utils/utils.d.ts @@ -146,6 +146,7 @@ export declare class ObjectUtils { static isString(value: any): boolean; static isPrintableCharacter(char: string): boolean; static isLetter(char: string): boolean; + static isScalar(value: any): boolean; static findLast(value: any[], callback: () => any): any; static findLastIndex(value: any[], callback: () => any): number; static sort(value1: any, value2: any, order: number, locale: string | string[]): number;