npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rjsf-layout

v0.7.1

Published

Reclaim full layout control. Want to dive fast? Check this [complete example here](https://github.com/aularon/rjsf-layout-example-nextjs).

Downloads

355

Readme

React JSONSchema Form Layout: Supercharge Your RJSF Experience!

Reclaim full layout control. Want to dive fast? Check this complete example here.

Installation

npm install rjsf-layout

Basic Usage: Free Layouts

import Form, { Field } from "rjsf-layout";

<Form {...{ schema, validator }}>
  <div className="flex">
    <Field name="firstName" />
    <Field name="lastName" />
  </div>
  <div>
    <Field name="password" />
  </div>
</Form>;

The Type System: Typed Fields and Data

The above is enough to conceive any complex layout! But that's only the basic offering. Check the Next.js Example for a fully fledged example, or continue reading to learn how rjsf-layout leverages Typescript to provide more features that makes it easier, cleaner, and safer to create RJSF forms! The sections below explore different features accumulatively.

Type-safe Field Components

Used in the way below, Field is a typed field component. Similar to the untyped variant in the first example, it allows you to display fields at any point in the components tree. This version is typed, however, in that it provides typing for the name prop, and other features for nested trees.

<Form {...{ schema, validator }}>
  {({ Field }) => (
    // ^
    <>
      <div className="flex">
        <Field name="firstName" />
        <Field name="lastName" />
      </div>
      <div>
        <Field name="password" />
        <Field name="nonExistingField" />
        {/* ^ Error */}
      </div>
    </>
  )}
</Form>

In addition to having autocomplete for the name prop, non-existing fields show TS errors.

Named Fields

Automatically populated, typed, field components. <FirstName /> below is equivalent to <Field name="firstName" />.

<Form {...{ schema, validator }}>
  <Field name="fieldName" />
  {({ FirstName, LastName, Password }) => (
    <>
      <div className="flex">
        <FirstName />
        <LastName />
      </div>
      <div>
        <Password />
      </div>
    </>
  )}
</Form>

Typed formData

You receive a typed formData object. Type is inferred from the incoming schema.

<Form {...{ schema, validator }}>
  <Field name="fieldName" />
  {({ formData, FirstName, LastName }) => (
    // ^
    <>
      <h1>
        Hello, {formData.firstName} {formData.lastName}!
      </h1>
      <div className="flex">
        <FirstName />
        <LastName />
      </div>
    </>
  )}
</Form>

Expanded formData

Instead of formData, you can pick individual variables. These are automatically populated, with proper types, based on your schema!

<Form {...{ schema, validator }}>
  <Field name="fieldName" />
  {({ $firstName, $lastName }) => (
    // ^
    <>
      <h1>
        Hello, {$firstName} {$lastName}!
      </h1>
      ...
    </>
  )}
</Form>

More Goodies

Embedded FieldTemplate

Say you have a boolean done field where you want to have a custom toggle-style checkbox. You can simply do this:

<Done>
  {({ $done, setDone }) => (
    // $done is a typed boolean, and setDone takes a boolean arguments — as inferred from the `schema`
    <span
      onClick={() => setDone(!$done)}
      style={{
        color: $done ? "green" : "gray",
      }}
    >
      {$done ? "🗹" : "☐"}
    </span>
  )}
</Done>

Complex Nested Schemas? We've Got You Covered.

const schema = {
  type: "object",
  properties: {
    name: {
      type: "object",
      properties: {
        first: { type: "string" },
        last: { type: "string" },
      },
    },
    languages: {
      type: "array",
      items: {
        type: "object",
        properties: {
          language: {
            type: "string",
            enum: [
              "arabic",
              "chinese",
              "english",
              "french",
              "russian",
              "spanish",
            ],
          },
          level: {
            type: "number",
            enum: [1, 2, 3],
          },
        },
      },
    },
  },
} as const satisfies JSONSchemaObject;

<Form schema={schema} validator={validator}>
  {({ $name, $languages, Name, Languages }) => (
    <>
      {$name &&
        `Hello ${$name.first || ""} ${$name.last || ""}. You know ${
          $languages?.length || 0
        } languages!`}
      <Name />
      {/* ^ If you do not pass in a nested layout, it will fallback to RJSF default rendering of that field*/}
      <Languages>
        {({ Language, Level, $level }) => (
          // Custom array item layout
          <div
            className="flex"
            style={{
              backgroundColor:
                // $level is typed to be `1 | 2 | 3 | undefined`
                $level === 1
                  ? "lightgreen"
                  : $level === 2
                  ? "lightblue"
                  : "pink",
            }}
          >
            <Language />
            <Level />
          </div>
        )}
      </Languages>
    </>
  )}
</Form>;

Custom themes?

import { Theme } from "@rjsf/mui"; // Or any other

<Form theme={Theme} {...{ otherProps }}>
  ...
</Form>;

Custom Submit Button?

Usually, custom submit buttons are passed in as children of the default RJSF Form implementation. Since we repurpose that to provide layout, you can pass any custom submit button in the submitter prop:

<Form
  {...{ otherProps }}
  submitter={
    <div>
      <CustomSubmit />
    </div>
  }
>
  ...
</Form>

Background, (Brief) History & Acknowledgement

An earlier private version of this was created for a personal (production stage) project around a year ago (RJSF v4). It was more of an exploration/proof of concept. It helped great deal in expressing complex forms and managing them, and reducing development and maintenance time.

Ever since RJSF v5 was released I intended to rewrite what I have in a more-reusable fashion. I had first working version a few months ago and used it in another personal project, but still wanted to achieve more elaborate features before declaring it public-ready. And here we are!

Last feature I included was typing the data variables. I tried with ajv's JTDDataType before settling on the excellent json-schema-to-ts.