1
+ /* eslint-disable */
2
+ import React , { useState , useEffect , useRef } from 'react' ;
3
+ import { RiArrowDropDownLine } from 'react-icons/ri' ;
4
+
5
+ interface Option {
6
+ label : string ;
7
+ value : string ;
8
+ }
9
+
10
+ interface CustomSelectProps {
11
+ options : Option [ ] ;
12
+ onSelect : ( selected : string ) => void ;
13
+ value : string ;
14
+ }
15
+
16
+ const CustomSelect : React . FC < CustomSelectProps > = ( { options, onSelect, value } ) => {
17
+ const [ selectedLabel , setSelectedLabel ] = useState ( 'Select an option' ) ;
18
+ const [ isOpen , setIsOpen ] = useState ( false ) ;
19
+ const wrapperRef = useRef < HTMLDivElement > ( null ) ;
20
+
21
+ useEffect ( ( ) => {
22
+ const selectedOption = options . find ( option => option . value === value ) ;
23
+ if ( selectedOption ) {
24
+ setSelectedLabel ( selectedOption . label ) ;
25
+ }
26
+ } , [ value , options ] ) ;
27
+
28
+ useEffect ( ( ) => {
29
+ const handleClickOutside = ( event : MouseEvent ) => {
30
+ if ( wrapperRef . current && ! wrapperRef . current . contains ( event . target as Node ) ) {
31
+ setIsOpen ( false ) ;
32
+ }
33
+ } ;
34
+ document . addEventListener ( 'mousedown' , handleClickOutside ) ;
35
+ return ( ) => {
36
+ document . removeEventListener ( 'mousedown' , handleClickOutside ) ;
37
+ } ;
38
+ } , [ ] ) ;
39
+
40
+ const handleOptionClick = ( option : Option ) => {
41
+ setSelectedLabel ( option . label ) ;
42
+ setIsOpen ( false ) ;
43
+ onSelect ( option . value ) ;
44
+ } ;
45
+
46
+ return (
47
+ < div className = "custom-select-wrapper" ref = { wrapperRef } >
48
+ < div className = "custom-select" onClick = { ( ) => setIsOpen ( ! isOpen ) } >
49
+ < div className = "select-selected" >
50
+ { selectedLabel }
51
+ < RiArrowDropDownLine className = "dropdown-icon" />
52
+ </ div >
53
+ { isOpen && (
54
+ < div className = "select-items" >
55
+ { options . map ( ( option , index ) => (
56
+ < div
57
+ key = { index }
58
+ className = "select-option"
59
+ onClick = { ( ) => handleOptionClick ( option ) }
60
+ >
61
+ { option . label }
62
+ </ div >
63
+ ) ) }
64
+ </ div >
65
+ ) }
66
+ </ div >
67
+ </ div >
68
+ ) ;
69
+ } ;
70
+
71
+ export default CustomSelect ;
0 commit comments