-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSelectorOption.tsx
More file actions
178 lines (165 loc) · 5.25 KB
/
SelectorOption.tsx
File metadata and controls
178 lines (165 loc) · 5.25 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
import React, { ReactNode } from 'react';
import HoverableButton from '@features/layers/hovercard/HoverableButton';
import { PositionInGroup } from '@shared/lib/PositionInGroup';
import { SelectorDisplay, useSelectorDisplay } from './SelectorDisplayContext';
type OptionProps<T> = {
getOptionDescription?: (value: T) => React.ReactNode;
getOptionLabel?: (value: T) => React.ReactNode; // optional label renderer
isSelected: boolean;
labelSuffix?: ReactNode; // Appeals after the label
labelWhenEmpty?: string; // for multi-select options
onClick: (value: T) => void;
option: T | T[];
optionStyle?: React.CSSProperties;
position?: PositionInGroup; // used for styling
};
function SelectorOption<T extends React.Key>({
getOptionDescription,
getOptionLabel = (val) => val as string,
isSelected,
labelSuffix,
labelWhenEmpty,
onClick,
option,
optionStyle,
position = PositionInGroup.Standalone,
}: OptionProps<T>) {
let className = 'selectorOption';
const { display } = useSelectorDisplay();
if (isSelected) className += ' selected';
if (!isSelected) className += ' unselected';
if (display === SelectorDisplay.InlineDropdown && position === PositionInGroup.Standalone)
className += ' hoverableText';
return (
<HoverableButton
className={className}
hoverContent={
getOptionDescription && (
<OptionDescription<T> option={option} getOptionDescription={getOptionDescription} />
)
}
onClick={() => onClick(Array.isArray(option) ? option[0] : option)}
role="option"
style={{ ...getOptionStyle(display, isSelected, position), ...optionStyle }}
>
<OptionLabel<T>
option={option}
labelWhenEmpty={labelWhenEmpty}
getOptionLabel={getOptionLabel}
/>
{labelSuffix}
</HoverableButton>
);
}
type OptionDescriptionProps<T> = {
option: T | T[];
getOptionDescription: (value: T) => React.ReactNode;
};
function OptionDescription<T extends React.Key>({
option,
getOptionDescription,
}: OptionDescriptionProps<T>) {
if (Array.isArray(option)) {
if (option.length === 0) return undefined;
return option.map((opt, idx) => (
<React.Fragment key={idx}>
{idx > 0 && ', '}
{getOptionDescription(opt)}
</React.Fragment>
));
}
return getOptionDescription(option);
}
type OptionLabelProps<T> = {
option: T | T[];
labelWhenEmpty?: string;
getOptionLabel: (value: T) => React.ReactNode;
};
function OptionLabel<T extends React.Key>({
option,
labelWhenEmpty,
getOptionLabel,
}: OptionLabelProps<T>) {
if (Array.isArray(option)) {
if (option.length === 0) {
return labelWhenEmpty ?? 'None';
} else {
return option.map((opt, idx) => (
<React.Fragment key={String(opt)}>
{idx > 0 && ' or '}
{getOptionLabel(opt)}
</React.Fragment>
));
}
}
return getOptionLabel(option);
}
export function getOptionStyle(
display: SelectorDisplay,
isSelected: boolean,
position: PositionInGroup,
): React.CSSProperties {
// Standard option style
const style: React.CSSProperties = {
border: '0.125em solid var(--color-button-primary)',
borderRadius: '0px',
cursor: 'pointer',
lineHeight: '1em',
padding: '0.5em',
whiteSpace: 'nowrap',
};
// Customize based on position and display type
switch (display) {
case SelectorDisplay.ButtonGroup:
if (position === PositionInGroup.Last) {
style.marginLeft = '-0.125em';
style.borderRadius = '0 1em 1em 0';
} else if (position === PositionInGroup.First) {
style.borderRadius = '1em 0 0 1em';
} else if (position === PositionInGroup.Middle) {
style.marginLeft = '-0.125em';
}
break;
case SelectorDisplay.ButtonList:
style.borderRadius = '1em';
if (!isSelected) style.border = '0.125em solid var(--color-button-secondary)';
break;
case SelectorDisplay.InlineDropdown:
// The standalone option should match the regular page text
if (position === PositionInGroup.Standalone) {
style.margin = '-0.25em';
style.border = 'none';
style.borderRadius = '.5em';
style.padding = '0.25em';
return style;
}
// otherwise return the Dropdown style
return getOptionStyle(SelectorDisplay.Dropdown, isSelected, position);
case SelectorDisplay.Dropdown:
style.textAlign = 'left';
style.width = '100%';
style.borderRadius = undefined;
if (position === PositionInGroup.First) {
style.borderRadius = '1em 1em 0 0';
style.margin = '0 0 -0.125em 0';
style.borderBottom = 'none';
} else if (position === PositionInGroup.Last) {
style.borderRadius = '0 0 1em 1em';
style.margin = '0';
style.borderTop = 'none';
} else if (position === PositionInGroup.Middle) {
style.margin = '0 0 -0.125em 0';
style.borderRadius = '0';
style.borderTop = 'none';
style.borderBottom = 'none';
} else if (position === PositionInGroup.Only) {
style.borderRadius = '1em';
} else if (position === PositionInGroup.Standalone) {
style.borderRadius = '1em';
style.width = 'fit-content';
}
break;
}
return style;
}
export default SelectorOption;