-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathdebugger-picker.component.tsx
More file actions
139 lines (127 loc) · 3.79 KB
/
debugger-picker.component.tsx
File metadata and controls
139 lines (127 loc) · 3.79 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
131
132
133
134
135
136
137
138
139
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,
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 (
<>
<PickerLabel label={label} />
<div className="react-select-placeholder"></div>
</>
);
}
return (
<>
{label && <PickerLabel label={label} />}
<Select
aria-label={"Debugger picker"}
className="react-select-container"
onChange={handleChange}
options={options}
menuPortalTarget={document.body}
classNamePrefix={"react-select"}
isSearchable={false}
placeholder={placeholder}
styles={{
control: (base) => ({
...base,
alignItems: 'center',
cursor: 'pointer',
display: 'flex',
flexWrap: 'wrap',
justifyContent: 'space-between',
minHeight: '2.5rem',
position: 'relative',
transition: '0.2s ease-out',
borderRadius: '1rem',
boxShadow: 'rgba(0, 0, 0, 0.04) 0px 1px 1px -0.5px, rgba(0, 0, 0, 0.04) 0px 3px 3px -1.5px, rgba(0, 0, 0, 0.04) 0px 0px 0px 1px inset',
boxSizing: 'border-box',
height: '2.5rem',
width: '100%',
padding: '0px 0.25rem',
minWidth: '240px',
outline: '0px !important',
...(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>
</>
);
};