Dyrected
Examples & RecipesRecipe Library

Calculate field values and format strings live

Automatically calculate prices, format reference codes, and transform field values in real time without custom JavaScript functions.

Automatically calculate prices, format reference codes, and transform field values in real time without custom JavaScript functions.

Use this when

  • build reactive form transforms that survive Cloud schema sync
  • compute field values without writing raw JavaScript functions
  • calculate status-based pricing or conditional totals declaratively
  • format reference codes or IDs from sibling inputs in real-time

Dyrected concepts

admin.hooks.onChange, when.then, when.match, when.sibling, when.concat, when.slugify

Additional packages: No additional packages.

Decisions and cautions

Use this recipe only when its runtime matches the project you are documenting or building. Cloud recipes must stay inside the managed content backend boundary. Self-hosted recipes may use the server runtime, database, hooks, and infrastructure you control.

Complete recipe

This is the canonical source compiled and behavior-tested by @dyrected/knowledge.

import {
  defineCollection,
  defineNumberField,
  defineSelectField,
  defineTextField,
  expr,
} from "@dyrected/core";

export const Invoices = defineCollection({
  slug: "invoices",
  fields: [
    defineTextField({
      name: "title",
      label: "Invoice Title",
      required: true,
    }),

    defineTextField({
      name: "slug",
      label: "Slug",
      required: true,
      admin: {
        hooks: {
          onChange: expr.ifElse(
            expr.empty("value"),
            expr.slugify("siblingData.title"),
            "value"
          ),
        },
      },
    }),

    defineSelectField({
      name: "planTier",
      label: "Plan Tier",
      defaultValue: "standard",
      options: [
        { label: "Free Trial", value: "trial" },
        { label: "Standard", value: "standard" },
        { label: "Enterprise", value: "enterprise" },
      ],
    }),

    defineNumberField({
      name: "basePrice",
      label: "Base Price (₦)",
      admin: {
        readOnly: true,
        hooks: {
          onChange: expr.match()
            .case(expr.sibling("planTier").equals("trial"), 0)
            .case(expr.sibling("planTier").equals("standard"), 25000)
            .case(expr.sibling("planTier").equals("enterprise"), 100000)
            .otherwise(25000),
        },
      },
    }),

    defineTextField({
      name: "referenceCode",
      label: "Payment Reference",
      admin: {
        readOnly: true,
        hooks: {
          onChange: expr.concat(
            "INV-",
            expr.upper("siblingData.planTier"),
            "-",
            expr.slugify("siblingData.title")
          ),
        },
      },
    }),
  ],
});

On this page

Dyrected| Cloud

Get your backend ready in minutes

Use a managed database, storage, APIs, and admin dashboard without setting up the infrastructure yourself.

Set Up My Backend