-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy pathdebugger-picker.component.tsx
More file actions
130 lines (117 loc) · 3.39 KB
/
debugger-picker.component.tsx
File metadata and controls
130 lines (117 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { useEffect, useState } from "react";
import styles from "./debugger-picker.module.scss";
import Select, { SingleValue, OptionsOrGroups, GroupBase } from "react-select";
import { DebuggerPickerOptionModel } from "@/features/common/models/debugger-picker-option.model";
import { LibraryFilterLabel } from "@/features/libraries/models/library-filters.model";
import { isGroupedOptionsType } from "./utils";
interface PickerLabelProps {
label: string | null;
}
const getGroupLabel = (
options: OptionsOrGroups<
DebuggerPickerOptionModel,
GroupBase<DebuggerPickerOptionModel>
>,
selected: DebuggerPickerOptionModel
): LibraryFilterLabel | undefined => {
if(!isGroupedOptionsType(options)) return undefined
const group = (options as GroupBase<DebuggerPickerOptionModel>[]).find(
(group) => group.options.some((opt) => opt.value === selected.value)
);
return group ? group.label as LibraryFilterLabel : undefined;
};
const PickerLabel: React.FC<PickerLabelProps> = ({ label }) => {
return (
<div className={styles.picker__label}>
<span className={styles.picker__fullName}>{label}</span>
</div>
);
};
interface DebuggerPickerComponentProps {
label: string | null;
languageCode: string;
options: OptionsOrGroups<
DebuggerPickerOptionModel,
GroupBase<DebuggerPickerOptionModel>
>;
isGrouped?: boolean;
selectedOptionCode: DebuggerPickerOptionModel | null;
handleSelection: (
selection: string,
parentLabel?: LibraryFilterLabel
) => void;
placeholder: string | null;
minWidth: string | null;
}
export const DebuggerPickerComponent: React.FC<
DebuggerPickerComponentProps
> = ({
label,
options,
handleSelection,
selectedOptionCode,
placeholder,
minWidth,
}) => {
const [isClient, setIsClient] = useState(false);
const handleChange = (
selection: SingleValue<DebuggerPickerOptionModel>
) => {
if (!selection) {
return;
}
const groupLabel = getGroupLabel(options, selection);
handleSelection(selection.value, groupLabel);
};
useEffect(() => {
setIsClient(true);
}, []);
if (!isClient) {
return (
<div className={styles.picker} data-has-label={label !== null}>
<PickerLabel label={label} />
<div className="react-select-placeholder"></div>
</div>
);
}
return (
<div className={styles.picker} data-has-label={label !== null}>
{label && <PickerLabel label={label} />}
<Select
aria-label={"Debugger picker"}
className="react-select-container"
onChange={handleChange}
value={selectedOptionCode}
options={options}
menuPortalTarget={document.body}
classNamePrefix={"react-select"}
isSearchable={false}
placeholder={placeholder}
styles={{
control: (base) => ({
...base,
minHeight: "1.75rem",
height: "1.75rem",
...(minWidth ? { minWidth: minWidth } : {}),
}),
valueContainer: (base) => ({
...base,
height: "1.75rem",
padding: "0 8px",
}),
input: (base) => ({
...base,
margin: "0px",
}),
indicatorSeparator: () => ({
display: "none",
}),
indicatorsContainer: (base) => ({
...base,
height: "1.75rem",
}),
}}
></Select>
</div>
);
};