This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
/
DropList.Combobox.jsx
146 lines (132 loc) · 3.42 KB
/
DropList.Combobox.jsx
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
import React, { useState, useRef, useEffect } from 'react'
import { useCombobox } from 'downshift'
import useDeepCompareEffect from 'use-deep-compare-effect'
import { noop } from '../../utilities/other'
import {
itemToString,
isItemSelected,
renderListContents,
} from './DropList.utils'
import {
getA11ySelectionMessageCommon,
onIsOpenChangeCommon,
stateReducerCommon,
} from './DropList.downshift.common'
import {
DropListWrapperUI,
InputSearchHolderUI,
MenuListUI,
} from './DropList.css'
import ListItem, { generateListItemKey } from './DropList.ListItem'
import { VARIANTS } from './DropList.constants'
function Combobox({
closeOnSelection = true,
customEmptyList = null,
'data-cy': dataCy = `DropList.${VARIANTS.COMBOBOX}`,
selectedItem = null,
selectedItems,
isOpen = false,
items = [],
menuCSS,
handleSelectedItemChange = noop,
renderCustomListItem = null,
toggleOpenedState = noop,
withMultipleSelection = false,
}) {
const [inputItems, setInputItems] = useState(items)
const inputEl = useRef(null)
const {
getComboboxProps,
getInputProps,
getItemProps,
getMenuProps,
highlightedIndex,
inputValue,
} = useCombobox({
initialInputValue: '',
initialIsOpen: isOpen,
isOpen,
items: inputItems,
itemToString,
selectedItem,
getA11ySelectionMessage: ({ selectedItem }) => {
return getA11ySelectionMessageCommon({
selectedItem,
selectedItems,
withMultipleSelection,
})
},
onInputValueChange({ inputValue }) {
setInputItems(
items.filter(item =>
itemToString(item).toLowerCase().startsWith(inputValue.toLowerCase())
)
)
},
onIsOpenChange(changes) {
onIsOpenChangeCommon({
closeOnSelection,
toggleOpenedState,
type: `${VARIANTS.COMBOBOX}.${changes.type}`,
})
},
onSelectedItemChange: handleSelectedItemChange,
stateReducer(state, actionAndChanges) {
const { changes, type } = actionAndChanges
return stateReducerCommon({
changes,
closeOnSelection,
selectedItems,
state,
type: `${VARIANTS.COMBOBOX}.${type}`,
withMultipleSelection,
})
},
})
useEffect(() => {
isOpen && inputEl.current.focus()
}, [isOpen])
useDeepCompareEffect(() => {
setInputItems(items)
}, [items])
function renderListItem(item, index) {
const itemProps = {
highlightedIndex,
index,
isSelected: isItemSelected({
item,
selectedItem,
selectedItems,
}),
item,
key: generateListItemKey(item, index),
withMultipleSelection,
renderCustomListItem,
...getItemProps({ item, index }),
}
return <ListItem {...itemProps} />
}
return (
<DropListWrapperUI
className="DropList DropList__Combobox"
data-cy={dataCy}
variant="combobox"
menuCSS={menuCSS}
{...getComboboxProps()}
>
<InputSearchHolderUI show={items.length > 0}>
<input {...getInputProps({ ref: inputEl })} placeholder="Search" />
</InputSearchHolderUI>
<MenuListUI className="MenuList MenuList-Combobox" {...getMenuProps()}>
{renderListContents({
customEmptyList,
emptyList: items.length === 0,
inputValue,
items: inputItems,
renderListItem,
})}
</MenuListUI>
</DropListWrapperUI>
)
}
export default Combobox