/
index.tsx
61 lines (52 loc) · 1.46 KB
/
index.tsx
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
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import { DropdownMenuProps } from './interfaces'
import { DropdownMenuStyled, DropdownSubmenuStyled } from './styles'
/**
* Выпадающее меню
*/
const DropdownMenu: React.FC<DropdownMenuProps> = ({
children,
name,
title,
...other
}) => {
/**
* Открытие и закрытие городов в меню
*/
const [citiesOpened, citiesOpenedSetter] = useState(false)
const toggleMenuCities = useCallback(() => {
citiesOpenedSetter(!citiesOpened)
}, [citiesOpened])
useEffect(() => {
if (!citiesOpened) {
return
}
const closeCitiesOpenedEvent = () => {
citiesOpenedSetter(false)
}
window.document.addEventListener('click', closeCitiesOpenedEvent)
return () => {
window.document.removeEventListener('click', closeCitiesOpenedEvent)
}
}, [citiesOpened])
return useMemo(() => {
return (
(children && (
<DropdownMenuStyled {...other}>
<a
//href={`/city/${coordsUrl}`}
onClick={toggleMenuCities}
title={title}
>
{name} <i className="fa fa-angle-down"></i>
</a>
<DropdownSubmenuStyled opened={citiesOpened}>
{children}
</DropdownSubmenuStyled>
</DropdownMenuStyled>
)) ||
null
)
}, [children, citiesOpened, name, other, title, toggleMenuCities])
}
export default DropdownMenu