-
Notifications
You must be signed in to change notification settings - Fork 943
Expand file tree
/
Copy pathuseMultipleSelect.tsx
More file actions
143 lines (138 loc) · 4.05 KB
/
Copy pathuseMultipleSelect.tsx
File metadata and controls
143 lines (138 loc) · 4.05 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
143
import * as React from 'react'
import {useSelect, useMultipleSelection} from '../../src'
import {type UseMultipleSelectionReturnValue} from '../../src/hooks/useMultipleSelection/index.types'
import {colors} from '../utils'
import './shared.css'
const initialSelectedItems = colors.slice(0, 2)
function getFilteredItems(selectedItems: string[]) {
return colors.filter(colour => !selectedItems.includes(colour))
}
export default function DropdownMultipleSelect() {
const {
getSelectedItemProps,
getDropdownProps,
addSelectedItem,
removeSelectedItem,
selectedItems,
} = useMultipleSelection({
initialSelectedItems,
}) as unknown as UseMultipleSelectionReturnValue<string>
const items = getFilteredItems(selectedItems)
const {
isOpen,
selectedItem,
getToggleButtonProps,
getLabelProps,
getMenuProps,
highlightedIndex,
getItemProps,
} = useSelect({
selectedItem: null,
defaultHighlightedIndex: 0, // after selection, highlight the first item.
items,
stateReducer: (state, actionAndChanges) => {
const {changes, type} = actionAndChanges
switch (type) {
case useSelect.stateChangeTypes.ToggleButtonKeyDownEnter:
case useSelect.stateChangeTypes.ToggleButtonKeyDownSpaceButton:
case useSelect.stateChangeTypes.ItemClick:
return {
...changes,
isOpen: true, // keep the menu open after selection.
}
default:
return changes
}
},
onStateChange: ({type, selectedItem: newSelectedItem}) => {
switch (type) {
case useSelect.stateChangeTypes.ToggleButtonKeyDownEnter:
case useSelect.stateChangeTypes.ToggleButtonKeyDownSpaceButton:
case useSelect.stateChangeTypes.ItemClick:
if (newSelectedItem) {
addSelectedItem(newSelectedItem)
}
break
default:
break
}
},
})
return (
<div className="container">
<label
style={{
fontWeight: 'bolder',
color: selectedItem ? selectedItem : 'black',
}}
{...getLabelProps()}
>
Choose an element:
</label>
<div className="tag-group">
{selectedItems.map(function renderSelectedItem(
selectedItemForRender: string,
index: number,
) {
return (
<span
className="tag"
key={`selected-item-${index}`}
{...getSelectedItemProps({
selectedItem: selectedItemForRender,
index,
})}
>
{selectedItemForRender}
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
<span
style={{padding: '4px', cursor: 'pointer'}}
onClick={e => {
e.stopPropagation()
removeSelectedItem(selectedItemForRender)
}}
>
✕
</span>
</span>
)
})}
<div
style={{
padding: '4px',
textAlign: 'center',
border: '1px solid black',
backgroundColor: 'lightgray',
cursor: 'pointer',
}}
{...getToggleButtonProps(
getDropdownProps({preventKeyAction: isOpen}),
)}
>
Pick some colors {isOpen ? <>↑</> : <>↓</>}
</div>
</div>
<ul {...getMenuProps()} className="menu">
{isOpen
? items.map((item, index) => (
<li
style={{
padding: '4px',
backgroundColor:
highlightedIndex === index ? '#bde4ff' : undefined,
}}
key={`${item}${index}`}
{...getItemProps({
item,
index,
'data-testid': `downshift-item-${index}`,
})}
>
{item}
</li>
))
: null}
</ul>
</div>
)
}