Skip to content

Commit

Permalink
Sort specific lists in dropdown menu alphabetically
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
owi92 committed Jun 4, 2024
1 parent d6b7df0 commit d83beec
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/utils/dropDownUtils.ts
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -111,5 +120,7 @@ export const formatDropDownOptions = (
}
}

return formattedOptions;
return hasCustomOrder
? formattedOptions
: formattedOptions.sort((a, b) => a.label.localeCompare(b.label));
};

0 comments on commit d83beec

Please sign in to comment.