Skip to content

Commit bcc982f

Browse files
committed
adding event listener to handle outside click
1 parent 48e299b commit bcc982f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/pages/AccountLiveLocation/components/LayerSelector/LayerSelector.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useRef, useEffect } from 'react'
22
import classNames from 'classnames'
33
import { Icon } from './../../../Generic'
44
import styles from './LayerSelector.module.scss'
@@ -14,17 +14,33 @@ const LayerSelector = ({
1414
const [showMenu, setShowMenu] = React.useState(false)
1515
const [height, setHeight] = React.useState(0)
1616

17+
const handleState = () => {
18+
setShowMenu(false)
19+
setHeight(0)
20+
}
21+
1722
const handleClick = e => {
18-
if (e.target.parentElement.value) {
19-
setShowMenu(false)
20-
setHeight(0)
23+
if (!layerSelectorEl.current.contains(e.target)) {
24+
//handleOutsideClick
25+
handleState()
26+
return
27+
} else {
28+
//handleInsideClick
29+
handleState()
2130
setSelectedMapLayer({
2231
type: e.target.parentElement.value,
2332
data: e.target.parentElement.getAttribute('data-layer')
2433
})
2534
}
2635
}
2736

37+
useEffect(() => {
38+
document.addEventListener('mousedown', handleClick, false)
39+
return () => document.removeEventListener('mousedown', handleClick, false)
40+
}, [])
41+
42+
const layerSelectorEl = useRef(null)
43+
2844
return (
2945
<div
3046
className={classNames(styles.LayerSelectorContainer, {

0 commit comments

Comments
 (0)