Blocks / Relation picker

Relation picker

Links records of another table: search by name, id or hint; one or several.

import { RelationPicker } from "@forgeron/ui";
/01 → Default
import { RelationPicker } from "@forgeron/ui";

const companies = [
  { id: "c-104", label: "Fromagerie Roy", hint: "Lyon" },
  { id: "c-212", label: "Garnier Transports", hint: "Grenoble" },
  { id: "c-318", label: "Petit & Fils", hint: "Annecy" },
  { id: "c-402", label: "Martin Distribution", hint: "Chambéry" },
];

export default function Default() {
  return (
    <div style={{ width: 260 }}>
      <RelationPicker label="Company" targets={companies} defaultValue={["c-212"]} />
    </div>
  );
}
/02 → Several
// Several records at once: the list stays open while they are ticked; search by name, id or hint.
import { RelationPicker } from "@forgeron/ui";
import { useState } from "react";

const contacts = [
  { id: "p-1", label: "Camille Roy", hint: "Fromagerie Roy" },
  { id: "p-2", label: "Hugo Bernard", hint: "Garnier" },
  { id: "p-3", label: "Léa Martin", hint: "Martin Distribution" },
  { id: "p-4", label: "Nadia Petit", hint: "Petit & Fils" },
  { id: "p-5", label: "Tom Garnier", hint: "Garnier" },
];

export default function Several() {
  const [ids, setIds] = useState<string[]>(["p-1", "p-3"]);
  return (
    <div style={{ display: "flex", gap: 16, alignItems: "center", flexWrap: "wrap" }}>
      <div style={{ width: 320 }}>
        <RelationPicker
          label="Contacts"
          multiple
          targets={contacts}
          value={ids}
          onChange={setIds}
        />
      </div>
      <RelationPicker
        label="Contacts"
        variant="chip"
        multiple
        targets={contacts}
        value={ids}
        onChange={setIds}
      />
    </div>
  );
}
API → Props
PropTypeDefaultDescription
labelstring—Names the control and the search.
targets{ id, label, hint? }[]—The records to link.
value / defaultValuestring[]—The linked ids. Controlled or not.
onChange(ids: string[]) => void—When a link changes.
multiplebooleanfalseSeveral records; the list stays open while they are ticked.
variant"field" | "chip""field"A form control, or a chip in a row.
open / onOpenChangeboolean—Opens the list from outside: a grid cell, a shortcut.
classNamestring—Extra classes.