Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 83 additions & 18 deletions src/components/SchemaEditor/AddFieldButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ import {
TooltipTrigger,
} from "../../components/ui/tooltip.tsx";
import { useTranslation } from "../../hooks/use-translation.ts";
import { cn } from "../../lib/utils.ts";
import type { NewField, SchemaType } 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,
}) => {
Expand All @@ -37,32 +40,58 @@ const AddFieldButton: FC<AddFieldButtonProps> = ({
const [fieldType, setFieldType] = useState<SchemaType>("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 t = useTranslation();
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;

onAddField({
const field = {
name: fieldName,
type: fieldType,
description: fieldDesc,
required: fieldRequired,
required: useNameRegex ? false : fieldRequired,
additionalProperties:
fieldType === "object" ? additionalProperties : undefined,
});
};

if (useNameRegex) {
onAddPatternField(field);
} else {
onAddField(field);
}

setFieldName("");
setFieldType("string");
setFieldDesc("");
setFieldRequired(false);
setUseNameRegex(false);
setDialogOpen(false);
setAdditionalProperties(true);
};
Expand Down Expand Up @@ -106,28 +135,62 @@ const AddFieldButton: FC<AddFieldButtonProps> = ({
htmlFor={fieldNameId}
className="text-sm font-medium"
>
{t.fieldNameLabel}
{useNameRegex ? t.fieldNameRegexLabel : t.fieldNameLabel}
</label>
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Info className="h-4 w-4 text-muted-foreground shrink-0" />
</TooltipTrigger>
<TooltipContent className="max-w-[90vw]">
<p>{t.fieldNameTooltip}</p>
<p>
{useNameRegex
? t.fieldNameRegexHelp
: t.fieldNameTooltip}
</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
<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={t.fieldNamePlaceholder}
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>
Expand Down Expand Up @@ -158,18 +221,20 @@ const AddFieldButton: FC<AddFieldButtonProps> = ({
/>
</div>

<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>
{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
Expand Down
Loading