Sanity Library Reference Docs
    Preparing search index...

    Type Alias IncomingReferencesOptions

    type IncomingReferencesOptions = {
        actions?: IncomingReferenceAction[];
        creationAllowed?: boolean | string[];
        description?: string;
        filter?: string | IncomingReferencesFilterResolver;
        filterParams?: Record<string, string>;
        name: string;
        onLinkDocument?: (
            document: SanityDocument,
            reference: {
                _ref: string;
                _strengthenOnPublish?: { type: string };
                _type: "reference";
                _weak?: boolean;
            },
        ) => SanityDocument
        | false;
        title?: string;
        types: (IncomingReferenceType | CrossDatasetIncomingReference)[];
    }
    Index

    Properties

    Callback to define the actions that will be shown for the incoming reference document.

    For example:

    actions: ({document, client}) => {
    return [
    {label: 'Unlink document',
    icon: TrashIcon,
    tone: 'critical',
    onClick: async () => {
    await client.createOrReplace({...document, _id: getDraftId(document._id), author: undefined})
    }
    }
    ]
    }

    The context of the linked document.

    The actions that will be shown for the linked document.

    creationAllowed?: boolean | string[]

    Whether to allow creation of new references. default: true

    If false, it will only be possible to link to existing documents, if the onLinkDocument callback is provided. If true, it will be possible to create new references to any template of the type. If an array is provided, it will only be possible to create new references that match the template id in the array provided.

    To define how a creation will be handled, you can use the initialValue option in the document type that will be linked.

    For example: I want to create a new book from an author: In the author we will have the with the creationAllowed set to true.

    Then in the book type, we will have the initialValue option to define the default values for the new book. This initialValue callback will receive the reference object that needs to be linked to the book in the params.

    // Book type
    export default {
    type: 'document',
    name: 'book',
    title: 'Book',
    fields: [
    {
    name: 'author',
    type: 'reference',
    to: [{type: 'author'}],
    },
    ],
    initialValue: (params) => {
    return {
    author: params?.reference,
    }
    }
    description?: string
    filter?: string | IncomingReferencesFilterResolver

    The filter query to apply to the incoming references in addition to the type filter. For example: filter all books that are from an specific editorial brand: editorialBrand == "Random House" The _type filter is applied automatically.

    filterParams?: Record<string, string>
    name: string
    onLinkDocument?: (
        document: SanityDocument,
        reference: {
            _ref: string;
            _strengthenOnPublish?: { type: string };
            _type: "reference";
            _weak?: boolean;
        },
    ) => SanityDocument
    | false

    Callback to link a document to a reference.

    This function is called when a user wants to link an existing document to the current document. It receives two parameters:

    • The document that will be linked (the target document)
    • The reference object with all properties needed to establish the link

    The developer must implement this function to define where and how the reference should be placed within the document structure. The function should return the modified document with the reference properly added.

    For example:

    • We want to link a book to an author:
     onLinkDocument: (document, reference) => {
    return {
    ...document,
    author: reference,
    }
    }

    Type declaration

      • (
            document: SanityDocument,
            reference: {
                _ref: string;
                _strengthenOnPublish?: { type: string };
                _type: "reference";
                _weak?: boolean;
            },
        ): SanityDocument
        | false
      • Parameters

        • document: SanityDocument

          The document to link to the reference.

        • reference: {
              _ref: string;
              _strengthenOnPublish?: { type: string };
              _type: "reference";
              _weak?: boolean;
          }

          The reference to link to the document.

        Returns SanityDocument | false

        The document to link to the reference. If false, the document will not be linked to the reference.

    title?: string
    types: (IncomingReferenceType | CrossDatasetIncomingReference)[]

    The type of the incoming references.