-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathActionMenu.component.tsx
More file actions
142 lines (135 loc) · 5.5 KB
/
ActionMenu.component.tsx
File metadata and controls
142 lines (135 loc) · 5.5 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { MutableRefObject } from 'react'
import { CustomInput } from '../CustomInput'
import { Popover } from '../Popover'
import { SelectPickerMenuListFooter } from '../SelectPicker/common'
import { ActionMenuItem } from './ActionMenuItem'
import { ActionMenuItemType, ActionMenuProps } from './types'
import { useActionMenu } from './useActionMenu.hook'
import './actionMenu.scss'
export const ActionMenu = <T extends string | number = string | number>({
id,
options,
onClick,
position,
alignment,
width,
isSearchable,
disableDescriptionEllipsis,
buttonProps,
children,
onOpen,
footerConfig,
}: ActionMenuProps<T>) => {
// HOOKS
const {
open,
filteredOptions,
flatOptions,
triggerProps,
overlayProps,
popoverProps,
focusedIndex,
searchTerm,
handleSearch,
itemsRef,
setFocusedIndex,
closePopover,
scrollableRef,
} = useActionMenu({
id,
options,
position,
alignment,
width,
isSearchable,
onOpen,
})
// HANDLERS
const handleOptionMouseEnter = (index: number) => () => setFocusedIndex(index)
const handleOptionOnClick = (item: ActionMenuItemType<T>) => () => {
onClick(item)
closePopover()
}
return (
<Popover
open={open}
overlayProps={overlayProps}
popoverProps={popoverProps}
triggerProps={triggerProps}
buttonProps={buttonProps}
triggerElement={children}
>
<div className="flexbox-col mxh-300">
{isSearchable && (
<div
role="search"
className="action-menu__searchbox bg__primary border__secondary-translucent--bottom"
>
<CustomInput
name="action-menu-search-box"
value={searchTerm}
placeholder="Search"
onChange={handleSearch}
fullWidth
autoFocus
/>
</div>
)}
<ul
ref={scrollableRef as MutableRefObject<HTMLUListElement>}
role="menu"
className="action-menu m-0 p-0 flex-grow-1 dc__overflow-auto dc__overscroll-none"
>
{filteredOptions.length > 0 ? (
filteredOptions.map((option, sectionIndex) => (
<li
key={option.groupLabel || `no-group-label-${sectionIndex}`}
role="menuitem"
className="action-menu__group flexbox-col dc__gap-4 py-4"
>
{option.groupLabel && (
<h4 className="bg__menu--secondary dc__truncate m-0 fs-12 lh-18 cn-9 fw-6 py-4 px-12 dc__position-sticky dc__top-0 dc__zi-1">
{option.groupLabel}
</h4>
)}
{option.items.length > 0 ? (
<ul className="action-menu__group-list p-0">
{option.items.map((item, itemIndex) => {
const index = flatOptions.findIndex(
(flatOption) =>
flatOption.sectionIndex === sectionIndex &&
flatOption.itemIndex === itemIndex,
)
return (
<ActionMenuItem<T>
key={`${item.label}-${item.id}`}
item={item}
itemRef={itemsRef.current[index]}
isFocused={index === focusedIndex}
onMouseEnter={handleOptionMouseEnter(index)}
onClick={handleOptionOnClick(item)}
disableDescriptionEllipsis={disableDescriptionEllipsis}
/>
)
})}
</ul>
) : (
<p className="m-0 fs-13 lh-18 fw-4 cn-7 py-6 px-12">No options in this group</p>
)}
</li>
))
) : (
<li role="menuitem" className="py-8 px-12">
<p className="m-0 fs-13 lh-20 fw-4 cn-7">No options</p>
</li>
)}
</ul>
{footerConfig && (
<div className="bg__menu--secondary border__secondary-translucent--top">
<SelectPickerMenuListFooter menuListFooterConfig={footerConfig} />
</div>
)}
</div>
</Popover>
)
}