Blocks / Matrix field

Matrix field

Rows rated against the same columns: one radio (or box) per cell, named by its row and column.

import { MatrixField } from "@forgeron/ui";
/01 → Default
PoorFairGoodGreat
Speed
Documentation
Support
import { MatrixField, type FormMatrixAnswer } from "@forgeron/ui";
import { useState } from "react";

const rows = [
  { id: "speed", label: "Speed" },
  { id: "docs", label: "Documentation" },
  { id: "support", label: "Support" },
];
const columns = [
  { id: "1", label: "Poor" },
  { id: "2", label: "Fair" },
  { id: "3", label: "Good" },
  { id: "4", label: "Great" },
];

export default function Default() {
  const [value, setValue] = useState<FormMatrixAnswer>({ speed: "3" });
  return (
    <div style={{ width: 520, maxWidth: "100%" }}>
      <MatrixField
        label="How satisfied are you with each part?"
        rows={rows}
        columns={columns}
        value={value}
        onChange={setValue}
      />
    </div>
  );
}
/02 → Multiple
MonTueWedThuFri
Early
Late
import { MatrixField } from "@forgeron/ui";

export default function Multiple() {
  return (
    <div style={{ width: 520, maxWidth: "100%" }}>
      <MatrixField
        label="Which days can you work each shift?"
        multiple
        rows={[
          { id: "early", label: "Early" },
          { id: "late", label: "Late" },
        ]}
        columns={[
          { id: "mon", label: "Mon" },
          { id: "tue", label: "Tue" },
          { id: "wed", label: "Wed" },
          { id: "thu", label: "Thu" },
          { id: "fri", label: "Fri" },
        ]}
        defaultValue={{ early: ["mon", "wed"] }}
      />
    </div>
  );
}
API → Props
PropTypeDefaultDescription
labelstring—Names the matrix for assistive technology: the question.
rows / columns{ id, label }[]—What is rated, and the scale it is rated on.
value / defaultValueRecord<rowId, columnId | columnId[]>—The chosen column of each row. Controlled or not.
onChange(value) => void—After every choice.
multiplebooleanfalseSeveral columns may be chosen on a row.
invalid / describedByboolean / string—Ties the matrix to its error.
disabledboolean—Locks the matrix.
classNamestring—Extra classes.