Skip to Content

RelationshipField

Search and select related CMS entries from caller-provided options under relation constraints.

RelationshipField filters supplied options by the target collection(s), locale, and allowed statuses in the relationship field descriptor. Search filters option labels locally by default. Set searchMode="remote" and provide onSearch to use caller-fetched search results without filtering them again by label; the component itself does not fetch.

The preview simulates a remote search using in-memory options.

390 × 844

API

PropTypeRequiredDefault
labelReactNodeYes—
relationExtract<FieldDescriptor, { type: "relationship" }>Yes—
optionsreadonly RelationshipOption[]Yes—
valuestring | readonly string[] | nullYes—
onChange(value: string | string[] | null) => voidYes—
localestringNo—
onSearch(query: string) => voidNo—
searchMode"local" | "remote"No"local"
loading / disabledbooleanNofalse
error / classNameReactNode / stringNo—

RelationshipOption contains an entry ID, display label, content type, locale, and status. Values returned to the caller are IDs, not copied entry data.

import { useState } from "react";
import { fields } from "@puzzle-ui/cms";
import { RelationshipField } from "@puzzle-ui/pieces";

const relatedArticles = fields.relationship({
  target: "article",
  multiple: true,
  sameLocale: true,
  allowedStatuses: ["published"],
});
const authorizedOptions = [{
  id: "field-notes",
  label: "Field notes for a calmer launch",
  contentType: "article",
  locale: "en-US",
  status: "published" as const,
}];

const RelatedArticlesField = () => {
  const [selectedIds, setSelectedIds] = useState<string[]>([]);
  return (
    <RelationshipField
      label="Related articles"
      relation={relatedArticles}
      options={authorizedOptions}
      value={selectedIds}
      locale="en-US"
      onChange={(next) =>
        setSelectedIds(Array.isArray(next) ? [...next] : next ? [next] : [])
      }
    />
  );
};

Selected IDs are listed separately so they can be removed even when they are not in the current options. An unavailable label can mean a deleted, inaccessible, or simply off-page entry; the host decides how to resolve it. When relation.sameLocale is enabled, supply the source locale; no options are offered without it. The host must revalidate target type, status, locale, tenant, and permission on every server mutation. A displayed or selected ID is not authorization.

API maturity

API maturity: Stable.