import { IconBug } from "@tabler/icons-react"; import type { JsonSchema } from "json-schema-library"; import { Children, type ReactElement, type ReactNode, cloneElement, isValidElement } from "react"; import { IconButton } from "ui/components/buttons/IconButton"; import { JsonViewer } from "ui/components/code/JsonViewer"; import * as Formy from "ui/components/form/Formy"; import { useFormContext, useFormError, useFormValue } from "ui/components/form/json-schema-form/Form"; import { Popover } from "ui/components/overlay/Popover"; import { getLabel } from "./utils"; export type FieldwrapperProps = { name: string; label?: string | false; required?: boolean; schema?: JsonSchema; debug?: object | boolean; wrapper?: "group" | "fieldset"; hidden?: boolean; children: ReactElement | ReactNode; errorPlacement?: "top" | "bottom"; }; export function FieldWrapper({ name, label: _label, required, schema, wrapper, hidden, errorPlacement = "bottom", children }: FieldwrapperProps) { const errors = useFormError(name, { strict: true }); const examples = schema?.examples || []; const examplesId = `${name}-examples`; const description = schema?.description; const label = typeof _label !== "undefined" ? _label : schema ? getLabel(name, schema) : name; const Errors = errors.length > 0 && ( {errors.map((e) => e.message).join(", ")} ); return ( 0} as={wrapper === "fieldset" ? "fieldset" : "div"} className={hidden ? "hidden" : "relative"} > {errorPlacement === "top" && Errors} {label && ( {label} {required && *} )}
{Children.count(children) === 1 && isValidElement(children) ? cloneElement(children, { // @ts-ignore list: examples.length > 0 ? examplesId : undefined }) : children} {examples.length > 0 && ( {examples.map((e, i) => ( )}
{description && {description}} {errorPlacement === "bottom" && Errors}
); } const FieldDebug = ({ name, schema, required }: Pick) => { const { options } = useFormContext(); if (!options?.debug) return null; const { value } = useFormValue(name); const errors = useFormError(name, { strict: true }); return (
( )} >
); };