-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathMenu.tsx
More file actions
63 lines (58 loc) · 1.56 KB
/
Menu.tsx
File metadata and controls
63 lines (58 loc) · 1.56 KB
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
62
63
import * as React from 'react'
import styled from 'styled-components'
import { useDebounce } from '../hooks'
import { CommonStyle } from '../styles'
import { Customize, Item } from '../types'
import { $width } from '../vars'
import { ItemElement } from './Item'
import { Search } from './Search'
export const Styles = styled.div`
padding: 10px;
width: ${$width}px;
margin-top: -20px;
margin-left: -${$width / 2}px;
`
type Props = {
items: Item[]
delay: number
searchBar?: boolean
onHide(): void
components?: Customize
}
export function Menu(props: Props) {
const [hide, cancelHide] = useDebounce(props.onHide, props.delay)
const [filter, setFilter] = React.useState('')
const filterRegexp = new RegExp(filter, 'i')
const filteredList = props.items.filter(item => item.label.match(filterRegexp))
const Component = props.components?.main?.() || Styles
const Common = props.components?.common?.() || CommonStyle
return <Component
onMouseOver={() => {
cancelHide()
}}
onMouseLeave={() => {
hide?.()
}}
onWheel={(e: React.WheelEvent) => {
e.stopPropagation()
}}
data-testid="context-menu"
>
{props.searchBar && (
<Common>
<Search value={filter} onChange={setFilter} component={props.components?.search?.()} />
</Common>
)}
{filteredList.map(item => {
return <ItemElement
key={item.key}
data={item}
delay={props.delay}
hide={props.onHide}
components={props.components}
>
{item.label}
</ItemElement>
})}
</Component>
}