import { useState } from "react"; import type { HostInfo, Notebook, ParamDef, ParamValues } from "@shellbook/core"; /** An included notebook (`::use`) whose parameters the form shows as a group. */ export interface ParamGroup { scope: string; title: string; } export function ParamsForm({ notebook, values, errors, hosts, groups = [], onChange, onReset, }: { notebook: Notebook; values: ParamValues; errors: Record; hosts: HostInfo[]; /** Active included notebooks; parameters of inactive ones are hidden. */ groups?: ParamGroup[]; onChange(name: string, value: string): void; /** Clears the values typed for this notebook. */ onReset?(): void; }) { const [open, setOpen] = useState(true); const field = (param: ParamDef) => { const id = `param-${param.name}`; const local = param.scope ? param.name.slice(param.scope.length + 1) : param.name; const common = { id, required: param.required, value: values[param.name] ?? "", "aria-labelledby": `${id}-label`, "aria-invalid": !!errors[param.name], // Red only for a wrong value; an empty field just still needs one. className: errors[param.name] && values[param.name] ? "invalid" : undefined, "aria-describedby": errors[param.name] ? `${id}-error` : undefined, onChange: ( e: React.ChangeEvent, ) => onChange(param.name, e.target.value), }; return ( ); }; const shown = [ ...notebook.params.filter((param) => !param.scope), ...groups.flatMap((group) => notebook.params.filter((param) => param.scope === group.scope)), ]; const missing = shown.filter((param) => param.required && !values[param.name]).length; if (!shown.length) return null; return (
e.preventDefault()} >

Parameters

{missing ? `${missing} required missing` : "All required set"} {onReset && open && ( )}
{open && notebook.params.filter((param) => !param.scope).map(field)} {open && groups.map((group) => { const params = notebook.params.filter((param) => param.scope === group.scope); return params.length ? (
{group.title} {params.map(field)}
) : null; })}
); }