SPB Git

Repository Public

RequestForm.tsx 5.8 KB 140 lines tsx

1// Explorateur d'API — formulaire généré depuis le schéma : un champ par propriété (texte, nombre, liste déroulante, case,2// liste « une valeur par ligne », objet imbriqué, repli JSON brut). Les champs facultatifs laissés vides ne sont pas envoyés.3import { t } from "../../../i18n"4import type { Field, FormValue, FormValues, Problem } from "../../../admin/openapi"56const TYPE_LABEL: Record<Field["kind"], string> = { string: "string", number: "number", integer: "integer", boolean: "boolean", enum: "enum", stringList: "string[]", object: "object", json: "JSON" }78function hintOf(f: Field): string {9  const parts: string[] = []10  if (f.description) parts.push(f.description)11  if (f.kind === "stringList") parts.push(f.options.length ? t("adm.explorer.field.listOf", { values: f.options.join(", ") }) : t("adm.explorer.field.list"))12  if (f.kind === "json") parts.push(t("adm.explorer.field.json"))13  if (f.nullable) parts.push(t("adm.explorer.field.nullable"))14  if (f.defaultValue !== undefined && f.defaultValue !== "" && !(Array.isArray(f.defaultValue) && !f.defaultValue.length)) parts.push(t("adm.explorer.field.default", { value: typeof f.defaultValue === "string" ? f.defaultValue : JSON.stringify(f.defaultValue) }))15  return parts.join(" · ")16}1718export interface FieldProps {19  field: Field20  value: FormValue21  onChange: (v: FormValue) => void22  /** chemin du champ : « body.to », « query.limit » — sert d'identifiant et relie les problèmes de saisie */23  path: string24  idBase: string25  problems: readonly Problem[]26  /** champ racine d'un corps : ses propriétés sont rendues sans cadre ni légende */27  root?: boolean28}2930export function FieldInput({ field: f, value, onChange, path, idBase, problems, root }: FieldProps) {31  const id = `${idBase}-${path.replace(/[^A-Za-z0-9_-]/g, "_")}`32  const hint = hintOf(f)33  const problem = problems.find((p) => p.path === path)34  const describedBy = [hint ? `${id}-h` : "", problem ? `${id}-e` : ""].filter(Boolean).join(" ") || undefined35  const text = typeof value === "string" ? value : ""36  const common = { id, "aria-describedby": describedBy, "aria-required": f.required || undefined, "aria-invalid": problem ? true : undefined, "data-testid": `f-${path}` } as const3738  if (f.kind === "object") {39    const obj: FormValues = typeof value === "object" && value !== null ? value : {}40    const children = f.children.map((c) => <FieldInput key={c.name} field={c} value={obj[c.name]} onChange={(v) => onChange({ ...obj, [c.name]: v })} path={`${path}.${c.name}`} idBase={idBase} problems={problems} />)41    if (root) return <div className="adm-explorer-fields">{children}</div>42    return (43      <fieldset className="adm-explorer-group">44        <legend>45          <code>{f.name}</code>46          <span className="badge">{TYPE_LABEL.object}</span>47          {f.required ? <Required /> : null}48        </legend>49        {hint ? <p className="hint">{hint}</p> : null}50        <div className="adm-explorer-fields">{children}</div>51      </fieldset>52    )53  }5455  const label = (56    <label htmlFor={id}>57      <code>{f.name}</code>58      {f.required ? <Required /> : null}59      <span className="badge">{f.format ? `${TYPE_LABEL[f.kind]} · ${f.format}` : TYPE_LABEL[f.kind]}</span>60    </label>61  )62  const foot = (63    <>64      {hint ? (65        <span className="hint" id={`${id}-h`}>66          {hint}67        </span>68      ) : null}69      {problem ? (70        <span className="adm-error small" id={`${id}-e`}>71          {t(`adm.explorer.problem.${problem.kind}`)}72        </span>73      ) : null}74    </>75  )7677  if (f.kind === "boolean") {78    return (79      <div className="field adm-explorer-field">80        <div className="row wrap">81          <label className="check" htmlFor={id}>82            <input type="checkbox" {...common} checked={value === true} onChange={(e) => onChange(e.target.checked)} />83            <code>{f.name}</code>84            {f.required ? <Required /> : null}85          </label>86          <span className="badge">{value === undefined ? t("adm.explorer.field.unset") : String(value)}</span>87          {value !== undefined ? (88            <button type="button" className="btn small ghost" onClick={() => onChange(undefined)} aria-label={t("adm.explorer.field.clearNamed", { name: f.name })}>89              {t("adm.explorer.field.clear")}90            </button>91          ) : null}92        </div>93        {foot}94      </div>95    )96  }97  if (f.kind === "enum") {98    return (99      <div className="field adm-explorer-field">100        {label}101        <select className="select" {...common} value={text} onChange={(e) => onChange(e.target.value)}>102          <option value="">{t("adm.explorer.field.unset")}</option>103          {f.options.map((o) => (104            <option key={o} value={o}>105              {o}106            </option>107          ))}108        </select>109        {foot}110      </div>111    )112  }113  if (f.kind === "stringList" || f.kind === "json" || f.multiline) {114    return (115      <div className="field adm-explorer-field wide">116        {label}117        <textarea className={`textarea${f.kind === "string" ? "" : " mono"}`} {...common} rows={f.kind === "json" ? 4 : 3} value={text} onChange={(e) => onChange(e.target.value)} spellCheck={f.kind === "string"} autoComplete="off" />118        {foot}119      </div>120    )121  }122  const numeric = f.kind === "number" || f.kind === "integer"123  return (124    <div className="field adm-explorer-field">125      {label}126      <input className="input" {...common} type="text" inputMode={numeric ? (f.kind === "integer" ? "numeric" : "decimal") : undefined} value={text} onChange={(e) => onChange(e.target.value)} autoComplete="off" spellCheck={false} autoCapitalize="off" />127      {foot}128    </div>129  )130}131132function Required() {133  return (134    <span className="adm-explorer-req">135      <span aria-hidden="true">*</span>136      <span className="sr-only">{t("adm.explorer.field.required")}</span>137    </span>138  )139}140
Files main
Menu

Theme