-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeArgumentInput.tsx
255 lines (248 loc) · 10.6 KB
/
CodeArgumentInput.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
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
245
246
247
248
249
250
251
252
253
254
255
import { Checkbox, CheckboxGroup, Flex, Item, ListView, NumberField, Picker, Radio, RadioGroup, Switch, TextArea, TextField, View } from '@adobe/react-spectrum';
import { Editor } from '@monaco-editor/react';
import { Field } from '@react-spectrum/label';
import React from 'react';
import { Controller, useFormContext } from 'react-hook-form';
import useFormCrossFieldValidation from '../hooks/form.ts';
import { Argument, ArgumentValue, isBoolArgument, isMultiSelectArgument, isNumberArgument, isPathArgument, isSelectArgument, isStringArgument, isTextArgument } from '../utils/api.types.ts';
import { Strings } from '../utils/strings.ts';
import styles from './CodeArgumentInput.module.css';
import { PathInput } from './PathInput.tsx';
interface CodeArgumentInputProps {
arg: Argument<ArgumentValue>;
value: ArgumentValue;
onChange: (name: string, value: ArgumentValue) => void;
}
const CodeArgumentInput: React.FC<CodeArgumentInputProps> = ({ arg }) => {
const { control, getValues } = useFormContext();
useFormCrossFieldValidation(arg.name);
const controllerRules = (arg: Argument<ArgumentValue>) => ({
validate: (value: ArgumentValue) => {
if (arg.required && (value === null || value === undefined || value === '' || (typeof value === 'number' && isNaN(value)) || (typeof value == 'boolean' && !value))) {
return 'Value is required';
}
if (arg.validator) {
try {
const validator = eval(arg.validator);
const allValues = getValues();
const errorMessage = validator(value, allValues);
return errorMessage || true;
} catch (error) {
console.error(`Validator for argument '${arg.name}' failed!`, error);
return `Validator failed`;
}
}
return true;
},
});
const renderInput = () => {
if (isBoolArgument(arg)) {
return (
<Controller
name={arg.name}
control={control}
rules={controllerRules(arg)}
render={({ field, fieldState }) => (
<View key={arg.name} marginBottom="size-200">
<Flex alignItems={'start'} justifyContent={'start'} direction={'column'}>
{arg.display === 'SWITCHER' ? (
<Switch {...field} isSelected={field.value} onChange={field.onChange} aria-label={`Argument '${arg.name}'`}>
{argLabel(arg)}
</Switch>
) : (
<Checkbox {...field} isSelected={field.value} isInvalid={!!fieldState.error} onChange={field.onChange} aria-label={`Argument '${arg.name}'`}>
{argLabel(arg)}
</Checkbox>
)}
{fieldState.error && <p className={styles.error}>{fieldState.error.message}</p>}
</Flex>
</View>
)}
/>
);
} else if (isStringArgument(arg)) {
return (
<Controller
name={arg.name}
control={control}
rules={controllerRules(arg)}
render={({ field, fieldState }) => (
<View key={arg.name} marginBottom="size-200">
<TextField {...field} label={argLabel(arg)} errorMessage={fieldState.error ? fieldState.error.message : undefined} validationState={fieldState.error ? 'invalid' : 'valid'} aria-label={`Argument '${arg.name}'`} width="100%" />
</View>
)}
/>
);
} else if (isTextArgument(arg)) {
return (
<Controller
name={arg.name}
control={control}
rules={controllerRules(arg)}
render={({ field, fieldState }) => (
<View key={arg.name} marginY="size-100">
{arg.language ? (
<Field label={argLabel(arg)} description={`Language: ${arg.language}`} width="100%" errorMessage={fieldState.error ? fieldState.error.message : undefined} validationState={fieldState.error ? 'invalid' : 'valid'}>
<div>
<View width="100%" backgroundColor="gray-800" borderWidth="thin" position="relative" borderColor="dark" height="100%" borderRadius="medium" padding="size-50">
<Editor aria-label={`Argument '${arg.name}'`} language={arg.language} theme="vs-dark" height="200px" options={{ scrollBeyondLastLine: false }} value={field.value?.toString() || ''} onChange={field.onChange} />
</View>
</div>
</Field>
) : (
<TextArea {...field} label={argLabel(arg)} errorMessage={fieldState.error ? fieldState.error.message : undefined} validationState={fieldState.error ? 'invalid' : 'valid'} aria-label={`Argument '${arg.name}'`} width="100%" />
)}
</View>
)}
/>
);
} else if (isSelectArgument(arg)) {
const display = arg.display === 'AUTO' ? (Object.entries(arg.options).length <= 3 ? 'RADIO' : 'DROPDOWN') : arg.display;
return (
<Controller
name={arg.name}
control={control}
rules={controllerRules(arg)}
render={({ field, fieldState }) => (
<View key={arg.name} marginBottom="size-200">
{display === 'RADIO' ? (
<RadioGroup
{...field}
orientation="horizontal"
label={argLabel(arg)}
errorMessage={fieldState.error ? fieldState.error.message : undefined}
validationState={fieldState.error ? 'invalid' : 'valid'}
aria-label={`Argument '${arg.name}'`}
>
{Object.entries(arg.options).map(([label, val]) => (
<Radio key={val?.toString()} value={val?.toString() || ''}>
{label}
</Radio>
))}
</RadioGroup>
) : (
<Picker
{...field}
label={argLabel(arg)}
selectedKey={field.value?.toString() || ''}
onSelectionChange={field.onChange}
errorMessage={fieldState.error ? fieldState.error.message : undefined}
validationState={fieldState.error ? 'invalid' : 'valid'}
aria-label={`Argument '${arg.name}'`}
>
{Object.entries(arg.options).map(([label, val]) => (
<Item key={val?.toString()}>{label}</Item>
))}
</Picker>
)}
</View>
)}
/>
);
} else if (isMultiSelectArgument(arg)) {
const display = arg.display === 'AUTO' ? (Object.entries(arg.options).length <= 3 ? 'CHECKBOX' : 'LIST') : arg.display;
return (
<Controller
name={arg.name}
control={control}
rules={controllerRules(arg)}
render={({ field, fieldState }) => (
<View key={arg.name} marginBottom="size-200">
{display === 'CHECKBOX' ? (
<CheckboxGroup
{...field}
orientation="horizontal"
label={argLabel(arg)}
errorMessage={fieldState.error ? fieldState.error.message : undefined}
validationState={fieldState.error ? 'invalid' : 'valid'}
aria-label={`Argument '${arg.name}'`}
>
{Object.entries(arg.options).map(([label, val]) => (
<Checkbox key={val?.toString()} value={val?.toString() || ''}>
{label}
</Checkbox>
))}
</CheckboxGroup>
) : (
<Field label={argLabel(arg)} width="100%" errorMessage={fieldState.error ? fieldState.error.message : undefined} validationState={fieldState.error ? 'invalid' : 'valid'}>
<div>
<ListView
{...field}
maxHeight="size-1600"
selectionMode="multiple"
selectedKeys={field.value as string[]}
onSelectionChange={(val) => field.onChange(Array.from(val as Set<string>))}
aria-label={`Argument '${arg.name}'`}
>
{Object.entries(arg.options).map(([label, val]) => (
<Item key={val?.toString()}>{label}</Item>
))}
</ListView>
</div>
</Field>
)}
</View>
)}
/>
);
} else if (isNumberArgument(arg)) {
return (
<Controller
name={arg.name}
control={control}
rules={controllerRules(arg)}
render={({ field, fieldState }) => (
<View key={arg.name} marginBottom="size-200">
<NumberField
{...field}
label={argLabel(arg)}
errorMessage={fieldState.error ? fieldState.error.message : undefined}
validationState={fieldState.error ? 'invalid' : 'valid'}
minValue={arg.min !== null ? arg.min : undefined}
maxValue={arg.max !== null ? arg.max : undefined}
hideStepper={arg.type === 'DECIMAL'}
formatOptions={arg.type === 'INTEGER' ? { maximumFractionDigits: 0 } : undefined}
aria-label={`Argument '${arg.name}'`}
/>
</View>
)}
/>
);
} else if (isPathArgument(arg)) {
return (
<Controller
name={arg.name}
control={control}
rules={controllerRules(arg)}
render={({ field, fieldState }) => (
<View key={arg.name} marginBottom={'size-200'}>
<Flex alignItems={'start'} justifyContent={'start'} direction={'column'}>
<PathInput
{...field}
selectionMode={'single'}
maxHeight={"size-3000"}
label={argLabel(arg)}
rootPath={arg.rootPath ?? ""}
errorMessage={fieldState.error ? fieldState.error.message : undefined}
isInvalid={!!fieldState.error}
aria-label={`Argument ${arg.name}`}
/>
{fieldState.error && <p className={styles.error}>{fieldState.error.message}</p>}
</Flex>
</View>
)}
/>
);
} else {
return null;
}
};
return <>{renderInput()}</>;
};
function argLabel(arg: Argument<ArgumentValue>): string {
if (arg.label) {
return arg.label;
}
return Strings.capitalizeWords(arg.name);
}
export default CodeArgumentInput;