Flow elementsReturn

Flow elements

Return

Learn how the return element ends the flow and passes its values to the onReturn callback.


Usage

The return element defines what the multi-step form returns. When the flow reaches it, the onReturn callback is called with the returned values and the flow ends.

TSX
// src/app.tsx
import { useCallback } from "react";

import { Formity, type s, type Flow, type OnReturn } from "@formity/react";

import { AboutMeForm } from "./components/about-me-form";

type Schema = {
  render: React.ReactNode;
  struct: [
    s.Form<{ name: string; surname: string; age: number }>,
    s.Return<{ fullName: string; age: number }>,
  ];
  inputs: Record<never, never>;
  params: Record<never, never>;
};

const flow: Flow<Schema> = [
  {
    form: {
      fields: () => ({
        name: ["", []],
        surname: ["", []],
        age: [20, []],
      }),
      render: ({ fields, next }) => <AboutMeForm fields={fields} next={next} />,
    },
  },
  {
    return: ({ name, surname, age }) => ({
      fullName: `${name} ${surname}`,
      age,
    }),
  },
];

export default function App() {
  const onReturn = useCallback<OnReturn<Schema>>((output) => {
    // {
    //   fullName: string;
    //   age: number;
    // }
    console.log(output);
  }, []);

  return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}

The return function receives the flow values and returns the values passed to onReturn. Its return type must match the s.Return entry in struct.

The OnReturn<Schema> type is the type of the onReturn callback, and ReturnOutput<Schema> is the type of the values it receives.

Ending the flow

The return element is not a step, so it renders nothing. After onReturn is called, the multi-step form keeps rendering the last form, so use onReturn to submit the values and show something else.

TSX
// src/app.tsx
import { useCallback, useState } from "react";

import {
  Formity,
  type s,
  type Flow,
  type OnReturn,
  type ReturnOutput,
} from "@formity/react";

import { AboutMeForm } from "./components/about-me-form";

type Schema = {
  render: React.ReactNode;
  struct: [
    s.Form<{ name: string; surname: string; age: number }>,
    s.Return<{ fullName: string; age: number }>,
  ];
  inputs: Record<never, never>;
  params: Record<never, never>;
};

const flow: Flow<Schema> = [
  {
    form: {
      fields: () => ({
        name: ["", []],
        surname: ["", []],
        age: [20, []],
      }),
      render: ({ fields, next }) => <AboutMeForm fields={fields} next={next} />,
    },
  },
  {
    return: ({ name, surname, age }) => ({
      fullName: `${name} ${surname}`,
      age,
    }),
  },
];

export default function App() {
  const [output, setOutput] = useState<ReturnOutput<Schema> | null>(null);

  const onReturn = useCallback<OnReturn<Schema>>((output) => {
    setOutput(output);
  }, []);

  if (output) {
    return <p>Thank you, {output.fullName}!</p>;
  }

  return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}

Skipped forms

A form inside a condition, switch or loop may be skipped, so its values aren't available outside it. To use them in a return, define them first with a variables element, which the form replaces when reached.

TSX
// src/app.tsx
import { useCallback } from "react";

import { Formity, type s, type Flow, type OnReturn } from "@formity/react";

import { WorkingForm } from "./components/working-form";
import { CompanyForm } from "./components/company-form";

type Schema = {
  render: React.ReactNode;
  struct: [
    s.Form<{ working: boolean }>,
    s.Variables<{ company: string | null }>,
    s.Condition<{
      then: [s.Form<{ company: string }>];
      else: [];
    }>,
    s.Return<{ working: boolean; company: string | null }>,
  ];
  inputs: Record<never, never>;
  params: Record<never, never>;
};

const flow: Flow<Schema> = [
  {
    form: {
      fields: () => ({ working: [true, []] }),
      render: ({ fields, next }) => <WorkingForm fields={fields} next={next} />,
    },
  },
  {
    variables: () => ({ company: null }),
  },
  {
    condition: {
      if: ({ working }) => working,
      then: [
        {
          form: {
            fields: () => ({ company: ["", []] }),
            render: ({ fields, back, next }) => (
              <CompanyForm fields={fields} back={back} next={next} />
            ),
          },
        },
      ],
      else: [],
    },
  },
  {
    return: ({ working, company }) => ({ working, company }),
  },
];

export default function App() {
  const onReturn = useCallback<OnReturn<Schema>>((output) => {
    // {
    //   working: boolean;
    //   company: string | null;
    // }
    console.log(output);
  }, []);

  return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}

Multiple returns

Instead of a single return element at the end, each branch can end with its own, and only the one that is reached is executed.

TSX
// src/app.tsx
import { useCallback } from "react";

import { Formity, type s, type Flow, type OnReturn } from "@formity/react";

import { WorkingForm } from "./components/working-form";
import { CompanyForm } from "./components/company-form";

type Schema = {
  render: React.ReactNode;
  struct: [
    s.Form<{ working: boolean }>,
    s.Condition<{
      then: [
        s.Form<{ company: string }>,
        s.Return<{ working: true; company: string }>,
      ];
      else: [s.Return<{ working: false }>];
    }>,
  ];
  inputs: Record<never, never>;
  params: Record<never, never>;
};

const flow: Flow<Schema> = [
  {
    form: {
      fields: () => ({ working: [true, []] }),
      render: ({ fields, next }) => <WorkingForm fields={fields} next={next} />,
    },
  },
  {
    condition: {
      if: ({ working }) => working,
      then: [
        {
          form: {
            fields: () => ({ company: ["", []] }),
            render: ({ fields, back, next }) => (
              <CompanyForm fields={fields} back={back} next={next} />
            ),
          },
        },
        {
          return: ({ company }) => ({ working: true, company }),
        },
      ],
      else: [
        {
          return: () => ({ working: false }),
        },
      ],
    },
  },
];

export default function App() {
  const onReturn = useCallback<OnReturn<Schema>>((output) => {
    // | {
    //     working: true;
    //     company: string;
    //   }
    // | {
    //     working: false;
    //   }
    console.log(output);
  }, []);

  return <Formity<Schema> flow={flow} onReturn={onReturn} />;
}

Here onReturn receives the union of the types of all the return elements.