-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathAddFieldButton.tsx
More file actions
288 lines (271 loc) · 10.2 KB
/
Copy pathAddFieldButton.tsx
File metadata and controls
288 lines (271 loc) · 10.2 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { CirclePlus } from "lucide-react";
import { type FC, type FormEvent, useContext, useId, useState } from "react";
import { Badge } from "../../components/ui/badge.tsx";
import { Button } from "../../components/ui/button.tsx";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "../../components/ui/dialog.tsx";
import { Input } from "../../components/ui/input.tsx";
import { RootSchemaContext } from "../../hooks/use-root-schema.ts";
import { useTranslation } from "../../hooks/use-translation.ts";
import { collectRefTargets } from "../../lib/refUtils.ts";
import { cn } from "../../lib/utils.ts";
import type { NewField, SchemaEditorType } from "../../types/jsonSchema.ts";
import SchemaTypeSelector from "./SchemaTypeSelector.tsx";
interface AddFieldButtonProps {
onAddField: (field: NewField) => void;
onAddPatternField: (field: NewField) => void;
variant?: "primary" | "secondary";
autoFocus?: boolean;
}
const AddFieldButton: FC<AddFieldButtonProps> = ({
onAddField,
onAddPatternField,
variant = "primary",
autoFocus = true,
}) => {
const [dialogOpen, setDialogOpen] = useState(false);
const [fieldName, setFieldName] = useState("");
const [fieldType, setFieldType] = useState<SchemaEditorType>("string");
const [fieldDesc, setFieldDesc] = useState("");
const [fieldRequired, setFieldRequired] = useState(false);
const [useNameRegex, setUseNameRegex] = useState(false);
const [additionalProperties, setAdditionalProperties] = useState(true);
const fieldNameId = useId();
const fieldNameHelpId = useId();
const fieldDescId = useId();
const fieldRequiredId = useId();
const fieldTypeId = useId();
const additionalPropertiesId = useId();
const formId = useId();
const t = useTranslation();
const rootSchema = useContext(RootSchemaContext);
const regexError = (() => {
if (!useNameRegex || !fieldName.trim()) return "";
try {
new RegExp(fieldName);
return "";
} catch {
return t.fieldNameRegexError;
}
})();
const setNameRegexMode = (enabled: boolean) => {
setUseNameRegex(enabled);
if (enabled) {
setFieldRequired(false);
}
};
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
if (!fieldName.trim()) return;
if (regexError) return;
const field = {
name: fieldName,
type: fieldType,
description: fieldDesc,
required: useNameRegex ? false : fieldRequired,
additionalProperties:
fieldType === "object" ? additionalProperties : undefined,
// New references point at the first definition in the document,
// or at the root when there is none yet
validation:
fieldType === "ref" && rootSchema !== undefined
? { $ref: collectRefTargets(rootSchema)[0]?.pointer ?? "#" }
: undefined,
};
if (useNameRegex) {
onAddPatternField(field);
} else {
onAddField(field);
}
setFieldName("");
setFieldType("string");
setFieldDesc("");
setFieldRequired(false);
setUseNameRegex(false);
setDialogOpen(false);
setAdditionalProperties(true);
};
return (
<>
<Button
type="button"
onClick={() => setDialogOpen(true)}
variant={variant === "primary" ? "default" : "outline"}
size="sm"
className="flex items-center gap-1.5 group"
>
<CirclePlus
size={16}
className="group-hover:scale-110 transition-transform"
/>
<span>{t.fieldAddNewButton}</span>
</Button>
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogContent className="md:max-w-[1200px] max-h-[85vh] w-[95vw] p-4 sm:p-6 jsonjoy">
<DialogHeader className="mb-4">
<DialogTitle className="text-xl flex flex-wrap items-center gap-2">
{t.fieldAddNewLabel}
<Badge variant="secondary" className="text-xs">
{t.fieldAddNewBadge}
</Badge>
</DialogTitle>
<DialogDescription className="text-sm">
{t.fieldAddNewDescription}
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-6" id={formId}>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div className="space-y-4 min-w-[280px]">
<div>
<div className="flex flex-wrap items-center gap-2 mb-1.5">
<label
htmlFor={fieldNameId}
className="text-sm font-medium"
>
{useNameRegex ? t.fieldNameRegexLabel : t.fieldNameLabel}
</label>
<button
type="button"
onClick={() => setNameRegexMode(!useNameRegex)}
className="text-xs text-muted-foreground hover:text-foreground underline-offset-2 hover:underline"
>
{useNameRegex
? t.fieldNameUseExactName
: t.fieldNameUseRegex}
</button>
</div>
<Input
id={fieldNameId}
value={fieldName}
onChange={(e) => setFieldName(e.target.value)}
placeholder={
useNameRegex
? t.fieldNameRegexPlaceholder
: t.fieldNamePlaceholder
}
aria-describedby={
useNameRegex ? fieldNameHelpId : undefined
}
aria-invalid={regexError ? true : undefined}
className="font-mono text-sm w-full"
autoFocus={autoFocus}
required
/>
{useNameRegex ? (
<p
id={fieldNameHelpId}
className={cn(
"text-xs mt-1.5",
regexError
? "text-destructive"
: "text-muted-foreground",
)}
>
{regexError || t.fieldNameRegexHelp}
</p>
) : null}
</div>
<div>
<label
htmlFor={fieldDescId}
className="block text-sm font-medium mb-1.5"
>
{t.fieldDescription}
</label>
<Input
id={fieldDescId}
value={fieldDesc}
onChange={(e) => setFieldDesc(e.target.value)}
placeholder={t.fieldDescriptionPlaceholder}
className="text-sm w-full"
/>
</div>
{useNameRegex ? null : (
<div className="flex items-center gap-3 p-3 rounded-lg border bg-muted/50">
<input
type="checkbox"
id={fieldRequiredId}
checked={fieldRequired}
onChange={(e) => setFieldRequired(e.target.checked)}
className="rounded border-gray-300 shrink-0"
/>
<label htmlFor={fieldRequiredId} className="text-sm">
{t.fieldRequiredLabel}
</label>
</div>
)}
{fieldType === "object" ? (
<div className="flex items-center gap-3 p-3 rounded-lg border bg-muted/50">
<input
type="checkbox"
id={additionalPropertiesId}
checked={additionalProperties}
onChange={(e) =>
setAdditionalProperties(e.target.checked)
}
className="rounded border-gray-300 shrink-0"
/>
<label htmlFor={additionalPropertiesId} className="text-sm">
{t.additionalPropertiesAllow}
</label>
</div>
) : null}
</div>
<div className="space-y-4 min-w-[280px]">
<div>
<label
htmlFor={fieldTypeId}
className="block text-sm font-medium mb-1.5"
>
{t.fieldType}
</label>
<SchemaTypeSelector
id={fieldTypeId}
value={fieldType}
onChange={setFieldType}
/>
</div>
<div className="rounded-lg border bg-muted/50 p-3 hidden md:block">
<p className="text-xs font-medium mb-2">
{t.fieldTypeExample}
</p>
<code className="text-sm bg-background/80 p-2 rounded block overflow-x-auto">
{fieldType === "string" && '"example"'}
{fieldType === "number" && "42"}
{fieldType === "boolean" && "true"}
{fieldType === "object" && '{ "key": "value" }'}
{fieldType === "array" && '["item1", "item2"]'}
{fieldType === "anyOf" && '{ "anyOf": [...] }'}
{fieldType === "oneOf" && '{ "oneOf": [...] }'}
{fieldType === "allOf" && '{ "allOf": [...] }'}
{fieldType === "ref" && '{ "$ref": "#/$defs/address" }'}
</code>
</div>
</div>
</div>
<DialogFooter className="mt-6 gap-2 flex-wrap">
<Button
type="button"
variant="outline"
size="sm"
onClick={() => setDialogOpen(false)}
>
{t.fieldAddNewCancel}
</Button>
<Button type="submit" size="sm" form={formId}>
{t.fieldAddNewConfirm}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
</>
);
};
export default AddFieldButton;