-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSelector.tsx
More file actions
222 lines (203 loc) · 6.17 KB
/
Selector.tsx
File metadata and controls
222 lines (203 loc) · 6.17 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
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
216
217
218
219
220
221
222
import React, { ReactNode, useState } from 'react';
import { useClickOutside } from '@shared/hooks/useClickOutside';
import { getPositionInGroup } from '@shared/lib/PositionInGroup';
import {
SelectorDisplay,
SelectorDisplayProvider,
useSelectorDisplay,
} from './SelectorDisplayContext';
import { SelectorDropdown } from './SelectorDropdown';
import SelectorLabel from './SelectorLabel';
import SelectorOption from './SelectorOption';
type Props<T> = {
display?: SelectorDisplay;
getOptionDescription?: (value: T) => React.ReactNode;
getOptionLabel?: (value: T) => React.ReactNode;
labelWhenEmpty?: string; // for multi-select options
onChange: (value: T) => void;
optionStyle?: React.CSSProperties;
options: readonly T[];
selected: T | T[];
selectorDescription?: ReactNode;
selectorLabel?: ReactNode;
selectorStyle?: React.CSSProperties;
};
function Selector<T extends React.Key>({
display,
getOptionDescription,
getOptionLabel = (val) => val as string,
labelWhenEmpty,
onChange,
optionStyle,
options,
selected,
selectorDescription,
selectorLabel,
selectorStyle,
}: Props<T>) {
const [expanded, setExpanded] = useState(false);
const optionsRef = useClickOutside(() => setExpanded(false));
return (
<SelectorContainer manualStyle={selectorStyle} manualDisplay={display}>
{selectorLabel && <SelectorLabel label={selectorLabel} description={selectorDescription} />}
<div style={{ position: 'relative', display: 'flex', alignItems: 'center' }}>
{/* The dropdown menu or the button list */}
<OptionsContainer
isExpanded={expanded}
containerRef={optionsRef}
hasSelectorLabel={!!selectorLabel}
>
<Options<T>
getOptionDescription={getOptionDescription}
getOptionLabel={getOptionLabel}
onClick={(option) => {
setExpanded(false);
onChange(option);
}}
optionStyle={optionStyle}
options={options}
selected={selected}
/>
</OptionsContainer>
{/* Standalone option to open/close the dropdown menu */}
<DropdownButton<T>
getOptionDescription={getOptionDescription}
getOptionLabel={getOptionLabel}
isExpanded={expanded}
labelWhenEmpty={labelWhenEmpty}
selected={selected}
toggleDropdown={() => setExpanded((prev) => !prev)}
/>
</div>
</SelectorContainer>
);
}
const SelectorContainer: React.FC<
React.PropsWithChildren<{
manualStyle?: React.CSSProperties;
manualDisplay?: SelectorDisplay;
}>
> = ({ children, manualStyle, manualDisplay }) => {
const { display: inheritedDisplay } = useSelectorDisplay();
const display = manualDisplay ?? inheritedDisplay;
const style: React.CSSProperties = {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
flexWrap: 'wrap',
};
if (display === SelectorDisplay.ButtonList) {
style.flexDirection = 'column';
style.alignItems = 'start';
} else if (display === SelectorDisplay.ButtonGroup) {
style.gap = '-0.125em'; // Overlap the buttons slightly
}
// Prepare the container. If there was a manual display, then wrap in a provider.
const container = (
<div className={'selector ' + display} style={{ ...style, ...manualStyle }}>
{children}
</div>
);
if (manualDisplay)
return <SelectorDisplayProvider display={manualDisplay}>{container}</SelectorDisplayProvider>;
return container;
};
type OptionsContainerProps = {
containerRef: React.RefObject<HTMLDivElement | null>;
hasSelectorLabel: boolean;
isExpanded: boolean;
};
const OptionsContainer: React.FC<React.PropsWithChildren<OptionsContainerProps>> = ({
children,
containerRef,
hasSelectorLabel,
isExpanded,
}) => {
const { display } = useSelectorDisplay();
switch (display) {
case SelectorDisplay.ButtonList:
return (
<div
ref={containerRef}
style={{
display: 'flex',
flexWrap: 'wrap',
gap: '0.25em',
marginLeft: hasSelectorLabel ? '1em' : 'none',
}}
>
{children}
</div>
);
case SelectorDisplay.ButtonGroup:
return <>{children}</>;
case SelectorDisplay.Dropdown:
case SelectorDisplay.InlineDropdown:
return (
<SelectorDropdown isOpen={isExpanded || false} containerRef={containerRef}>
{children}
</SelectorDropdown>
);
}
};
type OptionsProps<T> = {
getOptionDescription?: (value: T) => React.ReactNode;
getOptionLabel?: (value: T) => React.ReactNode; // optional label renderer
onClick: (value: T) => void;
optionStyle?: React.CSSProperties;
options: readonly T[];
selected: T | T[];
};
function Options<T extends React.Key>({
getOptionDescription,
getOptionLabel,
onClick,
optionStyle,
options,
selected,
}: OptionsProps<T>) {
return options.map((option, i) => (
<SelectorOption<T>
key={option}
getOptionDescription={getOptionDescription}
getOptionLabel={getOptionLabel}
onClick={onClick}
option={option}
optionStyle={optionStyle}
isSelected={Array.isArray(selected) ? selected.includes(option) : selected === option}
position={getPositionInGroup(i, options.length)}
/>
));
}
type DropdownButtonProps<T> = {
getOptionDescription?: (value: T) => React.ReactNode;
getOptionLabel?: (value: T) => React.ReactNode;
isExpanded: boolean;
labelWhenEmpty?: string;
selected: T | T[];
toggleDropdown: () => void;
};
function DropdownButton<T extends React.Key>({
getOptionDescription,
getOptionLabel,
isExpanded,
labelWhenEmpty,
selected,
toggleDropdown,
}: DropdownButtonProps<T>) {
const { display } = useSelectorDisplay();
if (display !== SelectorDisplay.Dropdown && display !== SelectorDisplay.InlineDropdown)
return null;
return (
<SelectorOption<T>
getOptionDescription={getOptionDescription}
getOptionLabel={getOptionLabel}
isSelected={true}
labelSuffix={isExpanded ? ' ▼' : ' ▶'}
labelWhenEmpty={labelWhenEmpty}
onClick={() => toggleDropdown()}
option={selected}
/>
);
}
export default Selector;