forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropdownButton.tsx
More file actions
50 lines (45 loc) · 1.81 KB
/
DropdownButton.tsx
File metadata and controls
50 lines (45 loc) · 1.81 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
import { ArrowDown } from "@element-plus/icons-vue"
import Flex from "@pages/components/Flex"
import { ElButton, ElDropdown, ElDropdownItem, ElDropdownMenu, ElLink } from "element-plus"
import { type Component, computed, defineComponent, type VNode } from "vue"
export type DropdownButtonItem<T> = {
key: T
icon: Component | VNode
label: string
onClick?: () => void
}
const DropdownButton = defineComponent<{ items: DropdownButtonItem<unknown>[] }>(props => {
const trigger = computed(() => props.items?.[0])
const list = computed(() => props.items.slice(1))
const handleClick = (key: unknown) => props.items?.find(i => i.key === key)?.onClick?.()
return () => !!trigger.value && (
<Flex align="center" gap={3}>
<ElButton
link
type="primary"
icon={trigger.value?.icon}
onClick={trigger.value?.onClick}
>
{trigger.value?.label ?? 'NaN'}
</ElButton>
{!!list.value?.length && (
<ElDropdown
placement="bottom-end"
v-slots={{
default: () => <ElLink type="primary" underline="never" icon={ArrowDown} />,
dropdown: () => (
<ElDropdownMenu>
{list.value?.map(({ icon, label, key }) => (
<ElDropdownItem icon={icon} onClick={() => handleClick(key)}>
{label}
</ElDropdownItem>
))}
</ElDropdownMenu>
),
}}
/>
)}
</Flex>
)
}, { props: ['items'] })
export default DropdownButton