7
7
OptionBody ,
8
8
Li
9
9
} from "./style" ;
10
+ import Portal from "../../Atoms/Portal/portal" ;
10
11
11
12
const Select :React . FC < selectProps > = ( props ) => {
12
13
@@ -20,7 +21,7 @@ const Select:React.FC<selectProps> = (props) => {
20
21
} = props ;
21
22
22
23
const [ show , setShow ] = useState ( false ) ;
23
-
24
+ const [ bodyCoordinates , setBodyCoordinates ] = useState ( { x : 0 , y : 0 , height : 0 } ) ;
24
25
const selectRef = useRef < HTMLDivElement > ( null ) ;
25
26
const buttonRef = useRef < HTMLButtonElement > ( null ) ;
26
27
@@ -41,14 +42,21 @@ const Select:React.FC<selectProps> = (props) => {
41
42
return ( ) => document . addEventListener ( 'click' , onClickoutside ) ;
42
43
} , [ selectRef ] ) ;
43
44
45
+ const handleButtonClick = ( ) => {
46
+ setShow ( ! show ) ;
47
+ if ( buttonRef . current ) {
48
+ const coords = buttonRef . current . getBoundingClientRect ( ) ;
49
+ setBodyCoordinates ( { x : coords . x , y : coords . y , height : coords . height } )
50
+ }
51
+ }
44
52
45
53
return (
46
54
< >
47
55
< StyledSelect width = { width } ref = { selectRef } >
48
56
< Button
49
57
width = { width }
50
58
variant = "default"
51
- onClick = { ( ) => setShow ( ! show ) }
59
+ onClick = { handleButtonClick }
52
60
noborder
53
61
leftIcon = { leftIcon }
54
62
rightIcon = { rightIcon }
@@ -57,21 +65,24 @@ const Select:React.FC<selectProps> = (props) => {
57
65
< Text > { selectedValue } </ Text >
58
66
</ Button >
59
67
{ show &&
60
- < OptionBody
61
- width = { width }
62
- justifyTop = { buttonRef . current && buttonRef . current . offsetHeight }
63
- >
64
- < ul >
65
- { options . map ( ( { label, value} ) =>
66
- < Li
67
- isSelected = { selectedValue === value }
68
- onClick = { ( ) => handleClick ( value ) }
69
- >
70
- { label }
71
- </ Li >
72
- ) }
73
- </ ul >
74
- </ OptionBody >
68
+ < Portal >
69
+ < OptionBody
70
+ width = { width }
71
+ justifyTop = { buttonRef . current && buttonRef . current . offsetHeight }
72
+ coords = { bodyCoordinates }
73
+ >
74
+ < ul >
75
+ { options . map ( ( { label, value} ) =>
76
+ < Li
77
+ isSelected = { selectedValue === value }
78
+ onClick = { ( ) => handleClick ( value ) }
79
+ >
80
+ { label }
81
+ </ Li >
82
+ ) }
83
+ </ ul >
84
+ </ OptionBody >
85
+ </ Portal >
75
86
}
76
87
</ StyledSelect >
77
88
</ >
0 commit comments