From d83beec7fe66386f1dc897658a3880d3c93e459f Mon Sep 17 00:00:00 2001 From: Ole Wieners Date: Tue, 4 Jun 2024 15:53:57 +0200 Subject: [PATCH] Sort specific lists in dropdown menu alphabetically This adds a function to check whether the options for a dropdown menu are custom ordered. If they are not, the resulting list is ordered alphabetically. Otherwise the ordering is left as is. --- src/utils/dropDownUtils.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/utils/dropDownUtils.ts b/src/utils/dropDownUtils.ts index 978fb00217..484332f761 100644 --- a/src/utils/dropDownUtils.ts +++ b/src/utils/dropDownUtils.ts @@ -1,5 +1,6 @@ import { TFunction } from "i18next"; import { DropDownType } from './../components/shared/DropDown'; +import { isJson } from "./utils"; /* * this file contains functions, which are needed for the searchable drop-down selections */ @@ -46,6 +47,14 @@ export const formatDropDownOptions = ( required: boolean, t: TFunction ) => { + /** + * This is used to determine whether any entry of the passed `unformattedOptions` + * contains an `order` field, indicating that a custom ordering for that list + * exists and the list therefore should not be ordered alphabetically. + */ + const hasCustomOrder = unformattedOptions.some((item: any) => + isJson(item.name) && JSON.parse(item.name).order !== undefined); + const formattedOptions = []; if (!required) { formattedOptions.push({ @@ -111,5 +120,7 @@ export const formatDropDownOptions = ( } } - return formattedOptions; + return hasCustomOrder + ? formattedOptions + : formattedOptions.sort((a, b) => a.label.localeCompare(b.label)); };