This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuggestion-list-creator.js
80 lines (74 loc) · 1.73 KB
/
suggestion-list-creator.js
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
'use strict'
const h = require('react-hyperscript')
const styled = require('styled-components').default
const { MenuView, HeaderItem, Item, Separator } = require(
'@zendeskgarden/react-menus')
const NoItemsMessage = styled.div`
margin: 16px;
text-align: center;
`
const render = (
suggestions,
selectedKeys,
getSectionSuggestions,
renderSectionTitle,
getItemProps,
focusedKey
) => {
const toMenuItems = (menuItems, section) => {
menuItems.push(h(HeaderItem, renderSectionTitle(section)))
getSectionSuggestions(section)
.sort((a,b) => a[2].localeCompare(b[2]))
.forEach(([key, suggestion]) => {
menuItems.push(
h(
Item,
getItemProps({
key,
focused: focusedKey === key,
checked: selectedKeys.has(key)
}),
suggestion
)
)
})
menuItems.push(h(Separator))
return menuItems
}
const menuItems = suggestions.reduce(toMenuItems, [])
return menuItems.length === 0
? h(NoItemsMessage, 'No items found')
: menuItems
}
module.exports = ({
suggestions,
selectedKeys,
getSectionSuggestions,
renderSectionTitle,
wrapperRef
}) => (
{getMenuProps, getItemProps, placement, focusedKey}) => {
return h(MenuView,
getMenuProps({
placement,
animate: true,
small: true,
style: {
width: wrapperRef
? wrapperRef.getBoundingClientRect().width
: 0
}
}), [
h('div', {style: {maxHeight: '150px', overflowY: 'auto'}},
render(
suggestions,
selectedKeys,
getSectionSuggestions,
renderSectionTitle,
getItemProps,
focusedKey
)
)
]
)
}