-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutocomplete.tsx
215 lines (196 loc) · 6.08 KB
/
Autocomplete.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import React, { useEffect, useMemo, useRef, useState } from 'react'
import {
autoUpdate,
flip,
FloatingFocusManager,
FloatingPortal,
offset,
size,
useDismiss,
useFloating,
useInteractions,
useListNavigation,
useRole
} from '@floating-ui/react'
import { useTheme } from 'styled-components'
import { fuzzySearch, MenuListItemType } from '@mexit/core'
import { Loading } from '../../Style/Loading'
import { DisplayShortcut } from '../../Style/Tooltip'
import { IconDisplay } from '../IconDisplay'
import { DefaultMIcons } from '../Icons'
import {
AutoCompleteActions,
AutoCompleteInput,
AutoCompleteSelector,
AutoCompleteSuggestions,
StyledLoading
} from './Autocomplete.style'
import { MenuItem } from './Dropdown'
import { MenuClassName, MenuItemClassName } from './Dropdown.classes'
interface AutoCompleteProps {
onEnter: any
clearOnEnter?: boolean
onCommandEnter?: () => void
disableMenu?: boolean
defaultItems: Array<MenuListItemType>
defaultValue?: string
}
export const AutoComplete: React.FC<AutoCompleteProps> = (props) => {
const { defaultItems = [], disableMenu, defaultValue, onEnter, onCommandEnter, clearOnEnter } = props
const [open, setOpen] = useState(false)
const [isLoading, setIsLoading] = useState(false)
const [inputValue, setInputValue] = useState(defaultValue ?? '')
const [activeIndex, setActiveIndex] = useState<number | null>(null)
const listRef = useRef<Array<HTMLElement | null>>([])
const { x, y, strategy, refs, context } = useFloating<HTMLInputElement>({
whileElementsMounted: autoUpdate,
open,
onOpenChange: setOpen,
placement: 'bottom-start',
middleware: [
offset({ mainAxis: 15 }),
flip({ padding: 10 }),
size({
apply({ rects, availableHeight, elements }) {
Object.assign(elements.floating.style, {
width: `${rects.reference.width}px`,
maxHeight: `${availableHeight}px`
})
},
padding: 10
})
]
})
const theme = useTheme()
const role = useRole(context, { role: 'listbox' })
const dismiss = useDismiss(context)
const listNav = useListNavigation(context, {
listRef,
activeIndex,
onNavigate: setActiveIndex,
virtual: true,
loop: true
})
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions([role, dismiss, listNav])
function onChange(event: React.ChangeEvent<HTMLInputElement>) {
const value = event.target.value
setInputValue(value)
if (value) {
setOpen(true)
setActiveIndex(0)
}
}
useEffect(() => {
setInputValue(defaultValue ?? '')
}, [defaultValue])
const items = useMemo(() => {
if (!inputValue) return defaultItems
const res = fuzzySearch(defaultItems, inputValue, (item) => item.label)
return res
}, [inputValue])
const handleOnSelect = (item: MenuListItemType) => {
setInputValue(item.label)
setIsLoading(true)
item.onSelect(() => {
setIsLoading(false)
setInputValue('')
})
setActiveIndex(null)
setOpen(false)
}
return (
<>
<AutoCompleteSelector>
<IconDisplay color={theme.tokens.colors.primary.hover} size={20} icon={DefaultMIcons.AI} />
<AutoCompleteInput
value={inputValue}
{...getReferenceProps({
ref: refs.setReference,
onChange,
placeholder: 'Ask me anything...',
'aria-autocomplete': 'list',
onKeyDown(event) {
if (event.key === 'Enter') {
if (event.metaKey && onCommandEnter) {
onCommandEnter()
}
if (activeIndex !== null && items[activeIndex] && !disableMenu) {
handleOnSelect(items[activeIndex])
} else {
if (onEnter && inputValue) {
setIsLoading(true)
onEnter(inputValue).then(() => {
if (clearOnEnter) {
setInputValue('')
setActiveIndex(null)
setIsLoading(false)
setOpen(false)
}
})
}
}
}
}
})}
/>
<AutoCompleteActions>
{isLoading ? (
<StyledLoading>
<Loading dots={3} transparent />
</StyledLoading>
) : (
<>
<DisplayShortcut shortcut="Enter" />
<span>to send</span>
</>
)}
</AutoCompleteActions>
</AutoCompleteSelector>
<FloatingPortal>
{open && items.length > 0 && !disableMenu && (
<FloatingFocusManager context={context} initialFocus={-1} visuallyHiddenDismiss>
<AutoCompleteSuggestions
type="modal"
{...getFloatingProps({
className: MenuClassName,
ref: refs.setFloating,
style: {
position: strategy,
top: y ?? 0,
left: x ?? 0
},
onKeyDown(event) {
if (event.key === 'Tab') {
setOpen(false)
}
}
})}
>
{items.map((menuItem, index) => {
return (
<MenuItem
key={menuItem.id}
icon={menuItem.icon}
// fontSize="small"
label={menuItem.label}
{...getItemProps({
role: 'menuitem',
className: MenuItemClassName,
ref(node) {
listRef.current[index] = node
},
onClick() {
handleOnSelect(menuItem)
}
})}
isActive={index === activeIndex}
/>
)
})}
</AutoCompleteSuggestions>
</FloatingFocusManager>
)}
</FloatingPortal>
</>
)
}