-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.tsx
175 lines (165 loc) Β· 4.52 KB
/
menu.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React, { Component, ReactNode, Ref } from "react"
import { GetItemPropsOptions, GetMenuPropsOptions } from "downshift"
import classNames from "classnames"
import Spinner from "../spinner"
import { wrapLink } from "../../lib/buttonHelper"
interface Item<T> {
value: T | { customValue: T }
name: string
key?: string
placeholder?: boolean
preMatch?: string
match?: string
postMatch?: string
}
interface Props<T> {
getItemProps: (props: GetItemPropsOptions<Item<T>>) => Record<string, unknown>
getMenuProps: (
props: GetMenuPropsOptions,
options?: Record<string, unknown>
) => Record<string, unknown>
setHighlightedIndex: (index: number, state?: Record<string, unknown>) => void
highlightedIndex?: number
selectedItem?: Item<T>
inputValue?: string
options: Array<Item<T>>
className?: string
equal?: (a: T, b: T) => boolean
innerRef?: Ref<HTMLUListElement>
renderOption?: (option: {
name: string | ReactNode
value: T
isSelected: boolean
}) => ReactNode
noResults?: string
isFetching?: boolean
}
const isCustomValue = (value): boolean => {
if (value && typeof value === "object") {
return "customValue" in value
}
return false
}
const hightlightItem = <T extends unknown>({
name,
preMatch,
match,
postMatch,
value,
}: Item<T>): ReactNode => {
return match ? (
<>
{preMatch}
<span
className={classNames("font-bold underline", {
// eslint-disable-next-line @typescript-eslint/naming-convention
select_withQuotes: isCustomValue(value),
})}
>
{match}
</span>
{postMatch}
</>
) : (
name
)
}
class Menu<T> extends Component<Props<T>> {
equalWrapper = (a, b) => {
const { equal } = this.props
const defaultEqual = (x, y) => x === y
return (equal || defaultEqual)(a, b)
}
render = () => {
const {
getItemProps,
getMenuProps,
setHighlightedIndex,
highlightedIndex,
selectedItem,
options,
className,
innerRef,
noResults,
inputValue,
isFetching,
} = this.props
let { renderOption } = this.props
if (!renderOption) {
renderOption = ({ name }) => name
}
const padding = "px-20 py-10"
return options.length ? (
<ul
{...getMenuProps(
{
className: classNames(
"border border-grey-3 absolute z-dropdownMenu bg-white cursor-normal inset-x-0 list-reset overflow-y-scroll custom-scrollbar max-h-dropdownSM md:max-h-dropdown py-10 shadow-soft rounded-4",
className
),
onMouseLeave: () => {
setHighlightedIndex(null)
},
},
{ suppressRefError: true }
)}
ref={innerRef}
data-testid="dropdown-options"
>
{options.map((item, index) => {
const isSelected =
selectedItem && this.equalWrapper(selectedItem.value, item.value)
const { clonedElement, isWrapped } = wrapLink(
renderOption({
value: item.value as T,
name: hightlightItem(item),
isSelected,
}),
padding
)
return (
// eslint-disable-next-line react/jsx-key
<li
data-testid={item.name}
{...getItemProps({
key:
item.key ||
(typeof item.value !== "object" && item.value.toString()) ||
`item-${index}`,
item,
className: classNames(
"hover:bg-grey-bright transition duration-200 cursor-pointer",
{
"font-bold text-teal": isSelected,
"bg-grey-bright": index === highlightedIndex,
"text-grey-4": item.placeholder,
"text-teal": isCustomValue(item.value),
[padding]: !isWrapped,
}
),
})}
>
{clonedElement}
</li>
)
})}
</ul>
) : inputValue && noResults ? (
<div
className={classNames(
"p-20 text-grey-4 border border-grey-2 absolute z-dropdownMenu bg-white cursor-normal inset-x-0 shadow-soft rounded-4",
className
)}
>
{isFetching ? (
<div className="text-salmon w-12/12 flex justify-center">
<Spinner />
</div>
) : (
noResults
)}
</div>
) : null
}
}
export default Menu