-
Notifications
You must be signed in to change notification settings - Fork 19
/
menu-item.tsx
109 lines (102 loc) · 2.88 KB
/
menu-item.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { ClickEvent, MenuItem as Item } from '@szhsin/react-menu'
import React from 'react'
import { useNavigate } from 'react-router-dom'
import { CopyToClipboard } from '../../copy-to-clipboard/copy-to-clipboard'
import { Truncate } from '../../truncate/truncate'
export interface MenuItemProps {
name: string
link?: { url: string; external?: boolean }
contentLeft?: React.ReactNode
contentRight?: React.ReactNode
onClick?: (e: ClickEvent) => void
copy?: string
copyTooltip?: string
containerClassName?: string
className?: string
textClassName?: string
isActive?: boolean
truncateLimit?: number
disabled?: boolean
itemContentCustom?: React.ReactNode
}
export function MenuItem(props: MenuItemProps) {
const {
name,
link,
contentLeft,
contentRight,
onClick,
copy,
copyTooltip,
isActive = false,
textClassName = 'text-text-500 dark:text-text-200',
className = '',
containerClassName = '',
truncateLimit = 34,
disabled = false,
itemContentCustom,
} = props
const navigate = useNavigate()
const disabledClassName = disabled ? 'opacity-50 cursor-not-allowed' : ''
const itemContent = itemContentCustom ? (
itemContentCustom
) : (
<>
<div className={`flex items-center truncate ${className}`}>
{copy && (
<div onClick={(e) => e.preventDefault()}>
<CopyToClipboard
content={copy}
tooltipContent={copyTooltip}
className="mr-4 text-text-500 dark:text-text-200"
/>
</div>
)}
{contentLeft && (
<span className="mr-3" data-testid="menu-icon">
{contentLeft}
</span>
)}
<span className={`menu-item__name text-sm font-medium ${textClassName}`}>
<Truncate text={name} truncateLimit={truncateLimit} />
</span>
</div>
<div className="flex items-center">{contentRight && <span className="ml-3">{contentRight}</span>}</div>
</>
)
if (link?.external) {
return (
<Item
className={`menu-item ${isActive ? 'menu-item--hover' : ''} ${containerClassName}`}
href={link.url}
data-testid="menuItem"
target="_blank"
rel="noopener noreferrer"
onClick={(e: ClickEvent) => {
e.syntheticEvent.stopPropagation()
onClick && onClick(e)
}}
>
{itemContent}
</Item>
)
} else {
return (
<Item
className={`menu-item ${isActive ? 'menu-item--hover' : ''} ${containerClassName} ${disabledClassName}`}
data-testid="menuItem"
defaultValue="prod"
onClick={(e: ClickEvent) => {
e.syntheticEvent.preventDefault()
if (!disabled) {
link?.url && navigate(link?.url)
onClick && onClick(e)
}
}}
>
{itemContent}
</Item>
)
}
}
export default MenuItem