Ir al contenido
Docxcelerate

← Registro

Recipient block

Opening

The salutation is the half worth having. A document generated in bulk meets recipients whose names it does not have — a joint tenancy, a company, a record where the field was never filled in — so the greeting branches, and the fallback is formal rather than clever.

npx dxcl add recipient-block
exporta RecipientBlock · se resuelve en 3 nodos
01

Datos de vista previa

el JSON con el que se construyó esta vista previa
{
  "recipient": {
    "name": "Adaeze Nkemelu",
    "formalName": "Ms Nkemelu",
    "addressLines": [
      "Flat 6",
      "22 Colston Street",
      "Bristol",
      "BS1 5AE"
    ]
  }
}
02

Vista previa renderizada

el componente renderizado por su cuenta
Open ↗
Renderizado por Docxcelerate en tiempo de compilación Abrir ↗
03

Nodos resueltos

lo que recibe un renderizador — 3 nodos
recipient-name paragraph · static Adaeze Nkemelu
recipient-address paragraph · static Flat 6, 22 Colston Street, Bristol, BS1 5AE
salutation paragraph · static Dear Ms Nkemelu,
04

Los archivos

lo que escribe la instalación; el archivo seleccionado es el componente
nodes/recipient-block.node.tsx Código
import { type Nodes, Paragraph, useState } from "docxcelerate/template";

/**
 * Who the document is addressed to, and how it greets them.
 *
 * The salutation is the interesting half. A document generated in bulk meets
 * recipients whose names it does not have — a joint tenancy, a company, a
 * record where the field was never filled in — and "Dear ," is the kind of
 * mistake that gets screenshotted. So the greeting branches, and the fallback
 * is deliberately formal rather than clever.
 *
 * Installed by `dxcl add recipient-block`.
 */

/** What this component reads. Add these fields to your document data type. */
export interface RecipientData {
  recipient: {
    /** The addressee. Leave empty where you genuinely do not have a name. */
    name?: string;
    /** Street, town, postcode — one entry per line. */
    addressLines: string[];
    /** How to greet them, when a first name would be too familiar. */
    formalName?: string;
  };
}

export const RecipientBlock: Nodes = () => {
  const [recipient] = useState((data: RecipientData) => data.recipient);
  const greeting = recipient.formalName ?? recipient.name;

  return (
    <>
      {recipient.name && <Paragraph id="recipient-name">{recipient.name}</Paragraph>}
      <Paragraph id="recipient-address">{recipient.addressLines.join(", ")}</Paragraph>
      {greeting
        ? <Paragraph id="salutation">Dear {greeting},</Paragraph>
        : <Paragraph id="salutation-unnamed">Dear Sir or Madam,</Paragraph>}
    </>
  );
};