Skip to content

Commit 5d53f81

Browse files
committed
support grouped and single options for debugger picker
1 parent 29f443b commit 5d53f81

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/features/common/components/debugger-picker/debugger-picker.component.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
import React, { useEffect, useState } from "react";
22
import styles from "./debugger-picker.module.scss";
3-
import Select, { SingleValue } from "react-select";
3+
import Select, { SingleValue, OptionsOrGroups, GroupBase } from "react-select";
44
import { DebuggerPickerOptionModel } from "@/features/common/models/debugger-picker-option.model";
55
import { LibraryFilterLabel } from "@/features/libraries/models/library-filters.model";
66

7+
78
interface PickerLabelProps {
89
label: string | null;
910
}
1011

12+
const getGroupLabel = (
13+
options: OptionsOrGroups<
14+
DebuggerPickerOptionModel,
15+
GroupBase<DebuggerPickerOptionModel>
16+
>,
17+
selected: DebuggerPickerOptionModel
18+
): LibraryFilterLabel | undefined => {
19+
if (!Array.isArray(options)) return undefined;
20+
21+
const group = (options as GroupBase<DebuggerPickerOptionModel>[]).find(
22+
(group) => group.options.some((opt) => opt.value === selected.value)
23+
);
24+
return group ? group.label as LibraryFilterLabel : undefined;
25+
};
26+
1127
const PickerLabel: React.FC<PickerLabelProps> = ({ label }) => {
1228
return (
1329
<div className={styles.picker__label}>
@@ -19,9 +35,16 @@ const PickerLabel: React.FC<PickerLabelProps> = ({ label }) => {
1935
interface DebuggerPickerComponentProps {
2036
label: string | null;
2137
languageCode: string;
22-
options: DebuggerPickerOptionModel[];
23-
selectedOptionCode: DebuggerPickerOptionModel["options"][0] | null;
24-
handleSelection: (selection: string, parentLabel?: LibraryFilterLabel) => void
38+
options: OptionsOrGroups<
39+
DebuggerPickerOptionModel,
40+
GroupBase<DebuggerPickerOptionModel>
41+
>;
42+
isGrouped?: boolean;
43+
selectedOptionCode: DebuggerPickerOptionModel | null;
44+
handleSelection: (
45+
selection: string,
46+
parentLabel?: LibraryFilterLabel
47+
) => void;
2548
placeholder: string | null;
2649
minWidth: string | null;
2750
}
@@ -39,13 +62,13 @@ export const DebuggerPickerComponent: React.FC<
3962
const [isClient, setIsClient] = useState(false);
4063

4164
const handleChange = (
42-
selection: SingleValue<DebuggerPickerOptionModel["options"][0]>
65+
selection: SingleValue<DebuggerPickerOptionModel>
4366
) => {
4467
if (!selection) {
4568
return;
4669
}
47-
const parentLabel = options.find(group => group.options.some(opt => opt.value === selection.value))?.label
48-
handleSelection(selection.value, parentLabel);
70+
const groupLabel = getGroupLabel(options, selection);
71+
handleSelection(selection.value, groupLabel);
4972
};
5073

5174
useEffect(() => {

0 commit comments

Comments
 (0)