-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathToolSettings.tsx
More file actions
244 lines (224 loc) · 5.7 KB
/
ToolSettings.tsx
File metadata and controls
244 lines (224 loc) · 5.7 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, { useState } from 'react';
import RowInputRange from './RowInputRange';
import RowSegmentedControl from './RowSegmentedControl';
import RowDoubleRange from './RowDoubleRange';
import { Button } from '../Button';
import { Checkbox } from '../Checkbox/Checkbox';
import { Label } from '../Label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../Select';
import { Switch } from '../Switch';
import { Tooltip, TooltipContent, TooltipTrigger } from '../Tooltip';
const SETTING_TYPES = {
RANGE: 'range',
RADIO: 'radio',
CUSTOM: 'custom',
DOUBLE_RANGE: 'double-range',
BUTTON: 'button',
CHECKBOX: 'checkbox',
SWITCH: 'switch',
SELECT: 'select',
};
function ToolSettings({ options }) {
if (!options) {
return null;
}
if (typeof options === 'function') {
const OptionsComponent = options;
return <OptionsComponent />;
}
return (
<div className="text-foreground space-y-2 pb-4">
{options?.map(option => {
if (option.condition && option.condition?.({ options }) === false) {
return null;
}
switch (option.type) {
case SETTING_TYPES.RANGE:
return renderRangeSetting(option);
case SETTING_TYPES.RADIO:
return renderRadioSetting(option);
case SETTING_TYPES.DOUBLE_RANGE:
return renderDoubleRangeSetting(option);
case SETTING_TYPES.CUSTOM:
return renderCustomSetting(option);
case SETTING_TYPES.BUTTON:
return renderButtonSetting(option);
case SETTING_TYPES.CHECKBOX:
return renderCheckboxSetting(option);
case SETTING_TYPES.SWITCH:
return renderSwitchSetting(option);
case SETTING_TYPES.SELECT:
return renderSelectSetting(option);
default:
return null;
}
})}
</div>
);
}
const renderLabelWithTooltip = (label: React.ReactNode, tooltip?: string) => {
if (!label) {
return null;
}
if (!tooltip) {
return <span>{label}</span>;
}
return (
<Tooltip>
<TooltipTrigger asChild>
<span className="cursor-help">{label}</span>
</TooltipTrigger>
<TooltipContent side="top">{tooltip}</TooltipContent>
</Tooltip>
);
};
const renderRangeSetting = option => {
return (
<div
className="flex items-center"
key={option.id}
>
<div className="w-1/3 text-[13px]">{renderLabelWithTooltip(option.name, option.tooltip)}</div>
<div
className="w-2/3"
data-cy={option.id}
>
<RowInputRange
minValue={option.min}
maxValue={option.max}
step={option.step}
value={option.value}
onChange={value => option.onChange?.(value)}
allowNumberEdit={true}
inputClassName="ml-1 w-4/5 cursor-pointer"
/>
</div>
</div>
);
};
const renderRadioSetting = option => {
return (
<RowSegmentedControl
key={option.id}
option={option}
onChange={option.onChange}
/>
);
};
function renderDoubleRangeSetting(option) {
return (
<RowDoubleRange
key={option.id}
values={option.value}
onChange={option.onChange}
minValue={option.min}
maxValue={option.max}
step={option.step}
showLabel={false}
tooltip={option.tooltip}
/>
);
}
const renderCustomSetting = option => {
return (
<div key={option.id}>
{typeof option.children === 'function' ? option.children() : option.children}
</div>
);
};
const renderButtonSetting = option => {
const button = (
<Button
key={option.id}
variant={option.variant || 'ghost'}
className={option.className || ''}
onClick={() => option.onChange()}
>
{option.name}
</Button>
);
if (option.tooltip) {
return (
<Tooltip>
<TooltipTrigger asChild>{button}</TooltipTrigger>
<TooltipContent side="top">{option.tooltip}</TooltipContent>
</Tooltip>
);
}
return button;
};
const renderCheckboxLabel = option => {
return (
<Label
htmlFor={option.id}
className="cursor-pointer"
>
{renderLabelWithTooltip(option.name, option.tooltip)}
</Label>
);
};
const renderCheckboxSetting = option => {
return (
<div
key={option.id}
className="flex items-center gap-2"
>
<Checkbox
id={option.id}
checked={option.value}
onCheckedChange={checked => {
option.onChange?.(checked);
}}
/>
{renderCheckboxLabel(option)}
</div>
);
};
const renderSwitchSetting = option => {
return (
<div
key={option.id}
className="flex items-center gap-2"
>
<Switch
id={option.id}
checked={option.value}
onCheckedChange={checked => {
option.onChange?.(checked);
}}
/>
{renderCheckboxLabel(option)}
</div>
);
};
const renderSelectSetting = option => {
return (
<div
className="flex items-center"
key={option.id}
>
<div className="w-1/3 text-[13px]">{renderLabelWithTooltip(option.name, option.tooltip)}</div>
<div className="w-2/3">
<Select
onValueChange={value => option.onChange?.(value)}
value={option.value}
>
<SelectTrigger className="w-full overflow-hidden">
<SelectValue />
</SelectTrigger>
<SelectContent>
{option.values.map(value => (
<SelectItem
key={value.id}
value={value.id}
>
{value.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
);
};
export default ToolSettings;