1
1
import React , { useEffect , useState } from "react" ;
2
2
import styles from "./debugger-picker.module.scss" ;
3
- import Select , { SingleValue } from "react-select" ;
3
+ import Select , { SingleValue , OptionsOrGroups , GroupBase } from "react-select" ;
4
4
import { DebuggerPickerOptionModel } from "@/features/common/models/debugger-picker-option.model" ;
5
5
import { LibraryFilterLabel } from "@/features/libraries/models/library-filters.model" ;
6
6
7
+
7
8
interface PickerLabelProps {
8
9
label : string | null ;
9
10
}
10
11
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
+
11
27
const PickerLabel : React . FC < PickerLabelProps > = ( { label } ) => {
12
28
return (
13
29
< div className = { styles . picker__label } >
@@ -19,9 +35,16 @@ const PickerLabel: React.FC<PickerLabelProps> = ({ label }) => {
19
35
interface DebuggerPickerComponentProps {
20
36
label : string | null ;
21
37
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 ;
25
48
placeholder : string | null ;
26
49
minWidth : string | null ;
27
50
}
@@ -39,13 +62,13 @@ export const DebuggerPickerComponent: React.FC<
39
62
const [ isClient , setIsClient ] = useState ( false ) ;
40
63
41
64
const handleChange = (
42
- selection : SingleValue < DebuggerPickerOptionModel [ "options" ] [ 0 ] >
65
+ selection : SingleValue < DebuggerPickerOptionModel >
43
66
) => {
44
67
if ( ! selection ) {
45
68
return ;
46
69
}
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 ) ;
49
72
} ;
50
73
51
74
useEffect ( ( ) => {
0 commit comments