-
Notifications
You must be signed in to change notification settings - Fork 536
/
SelectPanel.tsx
172 lines (151 loc) · 5.61 KB
/
SelectPanel.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
import React, {useCallback, useMemo} from 'react'
import {FilteredActionList, FilteredActionListProps} from '../FilteredActionList'
import {OverlayProps} from '../Overlay'
import {ItemInput} from '../ActionList/List'
import {FocusZoneHookSettings} from '../hooks/useFocusZone'
import {DropdownButton} from '../DropdownMenu'
import {ItemProps} from '../ActionList'
import {AnchoredOverlay, AnchoredOverlayProps} from '../AnchoredOverlay'
import Box from '../Box'
import {TextInputProps} from '../TextInput'
import {useProvidedStateOrCreate} from '../hooks/useProvidedStateOrCreate'
import {AnchoredOverlayWrapperAnchorProps} from '../AnchoredOverlay/AnchoredOverlay'
import {useProvidedRefOrCreate} from '../hooks'
interface SelectPanelSingleSelection {
selected: ItemInput | undefined
onSelectedChange: (selected: ItemInput | undefined) => void
}
interface SelectPanelMultiSelection {
selected: ItemInput[]
onSelectedChange: (selected: ItemInput[]) => void
}
interface SelectPanelBaseProps {
onOpenChange: (
open: boolean,
gesture: 'anchor-click' | 'anchor-key-press' | 'click-outside' | 'escape' | 'selection'
) => void
placeholder?: string
overlayProps?: Partial<OverlayProps>
}
export type SelectPanelProps = SelectPanelBaseProps &
Omit<FilteredActionListProps, 'selectionVariant'> &
Pick<AnchoredOverlayProps, 'open'> &
AnchoredOverlayWrapperAnchorProps &
(SelectPanelSingleSelection | SelectPanelMultiSelection)
function isMultiSelectVariant(
selected: SelectPanelSingleSelection['selected'] | SelectPanelMultiSelection['selected']
): selected is SelectPanelMultiSelection['selected'] {
return Array.isArray(selected)
}
const focusZoneSettings: Partial<FocusZoneHookSettings> = {
// Let FilteredActionList handle focus zone
disabled: true
}
export function SelectPanel({
open,
onOpenChange,
renderAnchor = props => <DropdownButton {...props} />,
anchorRef: externalAnchorRef,
placeholder,
selected,
onSelectedChange,
filterValue: externalFilterValue,
onFilterChange: externalOnFilterChange,
items,
textInputProps,
overlayProps,
...listProps
}: SelectPanelProps): JSX.Element {
const [filterValue, setInternalFilterValue] = useProvidedStateOrCreate(externalFilterValue, undefined, '')
const onFilterChange: FilteredActionListProps['onFilterChange'] = useCallback(
(value, e) => {
externalOnFilterChange(value, e)
setInternalFilterValue(value)
},
[externalOnFilterChange, setInternalFilterValue]
)
const anchorRef = useProvidedRefOrCreate(externalAnchorRef)
const onOpen: AnchoredOverlayProps['onOpen'] = useCallback(gesture => onOpenChange(true, gesture), [onOpenChange])
const onClose = useCallback(
(gesture: Parameters<Exclude<AnchoredOverlayProps['onClose'], undefined>>[0] | 'selection') => {
onOpenChange(false, gesture)
},
[onOpenChange]
)
const renderMenuAnchor = useMemo(() => {
if (renderAnchor === null) {
return null
}
const selectedItems = Array.isArray(selected) ? selected : [...(selected ? [selected] : [])]
return <T extends React.HTMLAttributes<HTMLElement>>(props: T) => {
return renderAnchor({
...props,
children: selectedItems.length ? selectedItems.map(item => item.text).join(', ') : placeholder
})
}
}, [placeholder, renderAnchor, selected])
const itemsToRender = useMemo(() => {
return items.map(item => {
const isItemSelected = isMultiSelectVariant(selected) ? selected.includes(item) : selected === item
return {
...item,
role: 'option',
selected: 'selected' in item && item.selected === undefined ? undefined : isItemSelected,
onAction: (itemFromAction, event) => {
item.onAction?.(itemFromAction, event)
if (event.defaultPrevented) {
return
}
if (isMultiSelectVariant(selected)) {
const otherSelectedItems = selected.filter(selectedItem => selectedItem !== item)
const newSelectedItems = selected.includes(item) ? otherSelectedItems : [...otherSelectedItems, item]
const multiSelectOnChange = onSelectedChange as SelectPanelMultiSelection['onSelectedChange']
multiSelectOnChange(newSelectedItems)
return
}
// single select
const singleSelectOnChange = onSelectedChange as SelectPanelSingleSelection['onSelectedChange']
singleSelectOnChange(item === selected ? undefined : item)
onClose('selection')
}
} as ItemProps
})
}, [onClose, onSelectedChange, items, selected])
const inputRef = React.useRef<HTMLInputElement>(null)
const focusTrapSettings = {
initialFocusRef: inputRef
}
const extendedTextInputProps: Partial<TextInputProps> = useMemo(() => {
return {
sx: {m: 2},
contrast: true,
...textInputProps
}
}, [textInputProps])
return (
<AnchoredOverlay
renderAnchor={renderMenuAnchor}
anchorRef={anchorRef}
open={open}
onOpen={onOpen}
onClose={onClose}
overlayProps={overlayProps}
focusTrapSettings={focusTrapSettings}
focusZoneSettings={focusZoneSettings}
>
<Box display="flex" flexDirection="column" width="100%" height="100%">
<FilteredActionList
filterValue={filterValue}
onFilterChange={onFilterChange}
{...listProps}
role="listbox"
items={itemsToRender}
selectionVariant={isMultiSelectVariant(selected) ? 'multiple' : 'single'}
textInputProps={extendedTextInputProps}
inputRef={inputRef}
/>
</Box>
</AnchoredOverlay>
)
}
SelectPanel.displayName = 'SelectPanel'