Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 12f2539

Browse files
authored
add sorting to selects where it makes sense (#298)
1 parent 3b56477 commit 12f2539

4 files changed

Lines changed: 54 additions & 6 deletions

File tree

admin-ui/src/component/ReservationUnits/ReservationUnitEditor/EnumSelect.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const EnumSelect = ({
1717
value,
1818
type,
1919
errorText,
20+
sort = false,
2021
}: {
2122
id: string;
2223
label: string;
@@ -27,12 +28,19 @@ const EnumSelect = ({
2728
onChange: (value: string) => void;
2829
type: { [key: string]: string };
2930
errorText?: string;
31+
sort?: boolean;
3032
}): JSX.Element => {
3133
const { t } = useTranslation();
34+
3235
const options: OptionType[] = Object.keys(type).map((key) => ({
3336
value: type[key],
3437
label: t(`${id}.${type[key]}`),
3538
}));
39+
if (sort) {
40+
options.sort((a, b) =>
41+
a.label.toLowerCase().localeCompare(b.label.toLowerCase())
42+
);
43+
}
3644

3745
return (
3846
<Select

admin-ui/src/component/ReservationUnits/ReservationUnitEditor/ReservationUnitEditor.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { useMutation, useQuery } from "@apollo/client";
22
import {
33
Accordion,
44
Checkbox,
5-
Combobox,
65
Fieldset,
76
Link,
87
Notification,
@@ -74,6 +73,7 @@ import {
7473
UPDATE_RESERVATION_UNIT,
7574
} from "./queries";
7675
import FormErrorSummary from "../../../common/FormErrorSummary";
76+
import SortedCompobox from "./SortedCompobox";
7777

7878
const bufferTimeOptions = [
7979
{ value: 900, label: "15 minuuttia" },
@@ -567,7 +567,7 @@ const ReservationUnitEditor = (): JSX.Element | null => {
567567
);
568568
})}
569569
<Normal>
570-
<Combobox
570+
<SortedCompobox
571571
id="spacePks"
572572
multiselect
573573
required
@@ -595,7 +595,7 @@ const ReservationUnitEditor = (): JSX.Element | null => {
595595
/>
596596
</Normal>
597597
<Normal>
598-
<Combobox
598+
<SortedCompobox
599599
id="resourcePks"
600600
multiselect
601601
label={t("ReservationUnitEditor.label.resourcePks")}
@@ -687,6 +687,7 @@ const ReservationUnitEditor = (): JSX.Element | null => {
687687
<EditorGrid>
688688
<Normal>
689689
<Select
690+
sort
690691
required
691692
id="reservationUnitTypePk"
692693
label={t(
@@ -711,7 +712,8 @@ const ReservationUnitEditor = (): JSX.Element | null => {
711712
/>
712713
</Normal>
713714
<Normal>
714-
<Combobox
715+
<SortedCompobox
716+
sort
715717
multiselect
716718
label={t("ReservationUnitEditor.purposesLabel")}
717719
placeholder={t(
@@ -737,7 +739,8 @@ const ReservationUnitEditor = (): JSX.Element | null => {
737739
/>
738740
</Normal>
739741
<Normal>
740-
<Combobox
742+
<SortedCompobox
743+
sort
741744
multiselect
742745
label={t("ReservationUnitEditor.equipmentsLabel")}
743746
placeholder={t(
@@ -1051,6 +1054,7 @@ const ReservationUnitEditor = (): JSX.Element | null => {
10511054
<Normal>
10521055
<Select
10531056
id="metadataSetPk"
1057+
sort
10541058
required
10551059
options={state.metadataOptions}
10561060
label={t("ReservationUnitEditor.label.metadataSetPk")}
@@ -1096,6 +1100,7 @@ const ReservationUnitEditor = (): JSX.Element | null => {
10961100
</Wide>
10971101
<Wide>
10981102
<EnumSelect
1103+
sort
10991104
id="authentication"
11001105
required
11011106
value={state.reservationUnitEdit.authentication || "WEAK"}
@@ -1257,6 +1262,7 @@ const ReservationUnitEditor = (): JSX.Element | null => {
12571262
return (
12581263
<Normal>
12591264
<Select
1265+
sort
12601266
id={name}
12611267
key={name}
12621268
label={t(`ReservationUnitEditor.label.${propName}`)}

admin-ui/src/component/ReservationUnits/ReservationUnitEditor/Select.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import { memoize } from "lodash";
23
import { Select as HDSSelect } from "hds-react";
34
import { OptionType } from "../../../common/types";
45

@@ -21,6 +22,7 @@ const Select = ({
2122
placeholder,
2223
helper,
2324
errorText,
25+
sort = false,
2426
}: {
2527
id: string;
2628
label: string;
@@ -31,13 +33,24 @@ const Select = ({
3133
placeholder?: string;
3234
helper?: string;
3335
errorText?: string;
36+
sort?: boolean;
3437
}): JSX.Element => {
38+
const sortedOpts = memoize((originalOptions) => {
39+
const opts = [...originalOptions];
40+
if (sort) {
41+
opts.sort((a, b) =>
42+
a.label.toLowerCase().localeCompare(b.label.toLowerCase())
43+
);
44+
}
45+
return opts;
46+
})(options);
47+
3548
return (
3649
<HDSSelect
3750
id={id}
3851
label={label}
3952
placeholder={placeholder}
40-
options={options}
53+
options={sortedOpts}
4154
required={required}
4255
onChange={(e: OptionType) => {
4356
if (typeof e.value !== "undefined") {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from "react";
2+
import memoize from "lodash/memoize";
3+
import { ComboboxProps, Combobox } from "hds-react";
4+
import { OptionType } from "../../../common/types";
5+
6+
const SortedCompobox = (
7+
props: ComboboxProps<OptionType> & { sort?: boolean }
8+
): JSX.Element => {
9+
const sortedOpts = memoize((originalOptions) => {
10+
const opts = [...originalOptions];
11+
if (props.sort) {
12+
opts.sort((a, b) =>
13+
a.label.toLowerCase().localeCompare(b.label.toLowerCase())
14+
);
15+
}
16+
return opts;
17+
})(props.options);
18+
19+
return <Combobox {...props} options={sortedOpts} />;
20+
};
21+
export default SortedCompobox;

0 commit comments

Comments
 (0)