Blocks / Question card

Question card

One question edited where it stands: title, help, type, required, options, limits; duplicate, delete, a handle to reorder.

import { QuestionCard, newFormQuestion, convertFormQuestion, formQuestionInfo } from "@forgeron/ui";
/01 → Default

Shown under the question.

Type
Options
import { QuestionCard, type FormQuestion } from "@forgeron/ui";
import { useState } from "react";

export default function Default() {
  const [question, setQuestion] = useState<FormQuestion>({
    id: "q1",
    type: "singleChoice",
    title: "How did you hear about us?",
    help: "Pick the closest one.",
    required: true,
    options: [
      { id: "o1", label: "A friend" },
      { id: "o2", label: "Search" },
      { id: "o3", label: "An event" },
    ],
  });
  return (
    <div style={{ width: 640, maxWidth: "100%" }}>
      <QuestionCard
        value={question}
        onChange={setQuestion}
        index={1}
        defaultOpen
        onDuplicate={() => {}}
        onDelete={() => {}}
      />
    </div>
  );
}
/02 → Limits

Shown under the question.

Type
import { QuestionCard, newFormQuestion, type FormQuestion } from "@forgeron/ui";

// Each type brings its own editor: limits of a number, a format of text, the scale of a rating,
// the rows and columns of a matrix, the files taken.
const questions: FormQuestion[] = [
  {
    ...newFormQuestion("number", "n1"),
    title: "Team size",
    validation: { min: 1, max: 500, integer: true },
  },
  {
    ...newFormQuestion("scale", "n2"),
    title: "How likely are you to recommend us?",
    range: { min: 0, max: 10, nps: true },
  },
  { ...newFormQuestion("matrix", "n3"), title: "Rate each part" },
  {
    ...newFormQuestion("file", "n4"),
    title: "Attach your brief",
    validation: { maxFiles: 3, maxSizeMb: 10, accept: ".pdf,image/*" },
  },
];

export default function Limits() {
  return (
    <div style={{ display: "grid", gap: 8, width: 640, maxWidth: "100%" }}>
      {questions.map((q, i) => (
        <QuestionCard key={q.id} defaultValue={q} index={i + 1} defaultOpen={i === 0} />
      ))}
    </div>
  );
}
/03 → Reorder
import { QuestionCard, moveFormItem, newFormQuestion, type FormQuestion } from "@forgeron/ui";
import { useState } from "react";

// The parent owns the order: Alt + Up and Alt + Down on a row (or its grip) ask for a move.
export default function Reorder() {
  const [list, setList] = useState<FormQuestion[]>([
    { ...newFormQuestion("shortText", "a"), title: "First name" },
    { ...newFormQuestion("shortText", "b"), title: "Last name" },
    { ...newFormQuestion("date", "c"), title: "Date of birth" },
  ]);
  const move = (id: string, delta: -1 | 1) =>
    setList((cur) => {
      const from = cur.findIndex((q) => q.id === id);
      return moveFormItem(cur, from, from + delta);
    });
  return (
    <div style={{ display: "grid", gap: 8, width: 640, maxWidth: "100%" }}>
      {list.map((q, i) => (
        <QuestionCard
          key={q.id}
          value={q}
          onChange={(next) => setList((cur) => cur.map((x) => (x.id === q.id ? next : x)))}
          index={i + 1}
          onMove={(delta) => move(q.id, delta)}
        />
      ))}
    </div>
  );
}
API → Props
PropTypeDefaultDescription
value / defaultValueFormQuestion—{ id, type, title, help?, required?, placeholder?, options?, columns?, multiple?, range?, validation?, showIf? }. Controlled or not.
onChange(question: FormQuestion) => void—After every edit.
indexnumber—Its number in the form, shown in the row.
open / defaultOpenbooleanfalseShows the editor under the row. Controlled or not, with onOpenChange.
onDuplicate / onDelete() => void—The two buttons at the end of the row.
onMove(delta: -1 | 1) => void—Alt + Up and Alt + Down on the row, and the grip. Absent: no handle, no reordering.
onDragStart / onDragEnd() => void—Makes the handle draggable; the parent owns the drop targets (FormBuilder does).
problemsstring[]—What is wrong, shown above the editor and counted in the row. See `formProblems`.
logicReactNode—A slot under the limits: the show-if of the question. The card knows nothing of the others.
hasLogicboolean—Marks the row with Logic.
takenIdsstring[]—Every id of the form, so the options made here never collide.
readOnlyboolean—Text only, no controls.
classNamestring—Extra classes.