Fix linter issues in DynamicJsonForm.tsx

This commit is contained in:
cliffhall 2025-12-03 10:08:13 -05:00
commit d0c5b884c6

View file

@ -11,7 +11,11 @@ import { Input } from "@/components/ui/input";
import JsonEditor from "./JsonEditor"; import JsonEditor from "./JsonEditor";
import { updateValueAtPath } from "@/utils/jsonUtils"; import { updateValueAtPath } from "@/utils/jsonUtils";
import { generateDefaultValue } from "@/utils/schemaUtils"; import { generateDefaultValue } from "@/utils/schemaUtils";
import type { JsonValue, JsonSchemaType } from "@/utils/jsonUtils"; import type {
JsonValue,
JsonSchemaType,
JsonSchemaConst,
} from "@/utils/jsonUtils";
import { useToast } from "@/lib/hooks/useToast"; import { useToast } from "@/lib/hooks/useToast";
import { CheckCheck, Copy } from "lucide-react"; import { CheckCheck, Copy } from "lucide-react";
@ -88,7 +92,9 @@ const DynamicJsonForm = forwardRef<DynamicJsonFormRef, DynamicJsonFormProps>(
if (!hasType) return false; if (!hasType) return false;
const includesType = (t: string) => const includesType = (t: string) =>
Array.isArray(s.type) ? s.type.includes(t as any) : s.type === t; Array.isArray(s.type)
? (s.type as ReadonlyArray<string>).includes(t)
: s.type === t;
// Primitive at top-level // Primitive at top-level
if (primitiveTypes.some(includesType)) return true; if (primitiveTypes.some(includesType)) return true;
@ -299,9 +305,11 @@ const DynamicJsonForm = forwardRef<DynamicJsonFormRef, DynamicJsonFormProps>(
switch (fieldType) { switch (fieldType) {
case "string": { case "string": {
// Titled single-select using oneOf/anyOf with const/title pairs // Titled single-select using oneOf/anyOf with const/title pairs
const titledOptions = (propSchema.oneOf ?? propSchema.anyOf)?.filter( const titledOptions = (
(opt) => (opt as any).const !== undefined, (propSchema.oneOf ?? propSchema.anyOf) as
) as { const: string; title?: string }[] | undefined; | (JsonSchemaType | JsonSchemaConst)[]
| undefined
)?.filter((opt): opt is JsonSchemaConst => "const" in opt);
if (titledOptions && titledOptions.length > 0) { if (titledOptions && titledOptions.length > 0) {
return ( return (
@ -326,7 +334,10 @@ const DynamicJsonForm = forwardRef<DynamicJsonFormRef, DynamicJsonFormProps>(
> >
<option value="">Select an option...</option> <option value="">Select an option...</option>
{titledOptions.map((option) => ( {titledOptions.map((option) => (
<option key={option.const} value={option.const}> <option
key={String(option.const)}
value={String(option.const)}
>
{option.title ?? String(option.const)} {option.title ?? String(option.const)}
</option> </option>
))} ))}
@ -337,8 +348,8 @@ const DynamicJsonForm = forwardRef<DynamicJsonFormRef, DynamicJsonFormProps>(
// Untitled single-select using enum (with optional legacy enumNames for labels) // Untitled single-select using enum (with optional legacy enumNames for labels)
if (propSchema.enum) { if (propSchema.enum) {
const names = Array.isArray((propSchema as any).enumNames) const names = Array.isArray(propSchema.enumNames)
? (propSchema as any).enumNames ? propSchema.enumNames
: undefined; : undefined;
return ( return (
<div className="space-y-2"> <div className="space-y-2">
@ -525,18 +536,20 @@ const DynamicJsonForm = forwardRef<DynamicJsonFormRef, DynamicJsonFormProps>(
const itemSchema = propSchema.items as JsonSchemaType; const itemSchema = propSchema.items as JsonSchemaType;
let multiOptions: { value: string; label: string }[] | null = null; let multiOptions: { value: string; label: string }[] | null = null;
const titledMulti = (itemSchema.anyOf ?? itemSchema.oneOf)?.filter( const titledMulti = (
(opt) => (opt as any).const !== undefined, (itemSchema.anyOf ?? itemSchema.oneOf) as
) as { const: string; title?: string }[] | undefined; | (JsonSchemaType | JsonSchemaConst)[]
| undefined
)?.filter((opt): opt is JsonSchemaConst => "const" in opt);
if (titledMulti && titledMulti.length > 0) { if (titledMulti && titledMulti.length > 0) {
multiOptions = titledMulti.map((o) => ({ multiOptions = titledMulti.map((o) => ({
value: o.const, value: String(o.const),
label: o.title ?? String(o.const), label: o.title ?? String(o.const),
})); }));
} else if (itemSchema.enum) { } else if (itemSchema.enum) {
const names = Array.isArray((itemSchema as any).enumNames) const names = Array.isArray(itemSchema.enumNames)
? (itemSchema as any).enumNames ? itemSchema.enumNames
: undefined; : undefined;
multiOptions = itemSchema.enum.map((v, i) => ({ multiOptions = itemSchema.enum.map((v, i) => ({
value: v, value: v,