0.6.0 • Published 5 months ago

react-signal-forms v0.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

React Signal Forms · npm build github-pages

⚠️ This library is still new, so everything is still subject to change. You can follow its development in the project and releases. The docs will be updated as development progresses.

A forms library which aims to provide a high performance modular experience by leveraging signals with @preact/signals-react.

  • Easy to use, easy to extend. Built from the ground with an DX friendly plugin model.
    • Pick and choose from the built-in plugins that fit your needs.
    • Plug in your own.
  • Add built-in context aware and typesafe rules to your fields or create your own.
    • Like required(), requiredIf(...), applicableIf(...), computed(...), etc.
  • Only calculates and renders what is necessary without you needing to think about it.
  • Field and rule specifications are separated from presentation, so UI components don't get clogged with configuration and business rules.
  • Bring your own UI libraries and components.
  • All strongly typed with TypeScript.

Getting started

npm i react-signal-forms

Exploring the demo

For a quick first look you can check out the demo of react-signal-forms with Material UI, or run it yourself by cloning the repository and running:

npm run ci
npm run demo

If you want to explore the demo code, a good place to start would be the basics example.

Your first form

Start by initializing your form component and field hook, including the plugins you want to use:

// Add plugins, built-in or your own.
export const { SignalForm, useForm, useField } = createSignalForm(
  ...defaultPlugins, // the defaults, includes validation rules and touched field signals.
  plugins.applicabilityRules // adds applicability rules and field signals.
)

ℹ️ A full list of the currently available plugins can be found in the plugins module.

Create field specifications for your forms:

interface IYourData {
  justText: string
  aFieldWithRules: string
  aSelectField: string
}

const fields = signalForm<IYourData>().withFields((field) => {
  //                      ^ All specifications and rules will be strongly
  //                        typed based on your data interface.

  ...field("justText", "Just a text field"),

  ...field("aFieldWithRules", "A field with some rules", {
    defaultValue: "Demo",

    // Add rules to your field. Some examples:
    rules: [
      required(),
      minLength(6),
      requiredIf(({ form }) => form.fields.otherField.value === true),
      applicableIf(({ form })) => form.field.otherField.value === true)
    ]
  })

  ...field("aSelectField", "Select field").as<SelectField>({
    //          Plug in any field type you need, ^
    //          built-in or your own.
    options: [
      /* ...items */
    ]
  })

})

Add the useField hook to your inputs:

interface TextInputProps {
  field: TextField // only accepts string fields.
}

const TextInput = ({ field }: TextInputProps) => {
  const {
    value,
    setValue,
    isValid,
    errors,
    isApplicable,
    ...otherSignals
    // ^ With intellisense matching your selected plugins.
  } = useField(field)

  if (!isApplicable) {
    return null
  }

  return (
    <input
      value={value}
      onChange={(e) => setValue(e.currentTarget.value)}
      {...otherProps}
    />
  )
}

You are now set to compose your form:

const MyForm = () => {
  return (
    <SignalForm
      fields={fields}
      initialValues={valuesFromStore}
      onSubmit={handleSubmit}
    >
      <TextInput field={fields.justText} />
      <TextInput field={fields.aFieldWithRules} />
      <SelectInput field={fields.aSelectField} />
    </SignalForm>
  )
}

Rules and signals

All internal state management is done with signals. An advantage of this approach is that rules automatically subscribe to the state they need, and are only re-evaluated when state used in the rules are updated. The results of these rules are in turn also saved in (computed) signals.

A simple example to illustrate what this means for performance: if field A is only applicable if field B has a specific value, then:

  • The applicability rule is only evaluated when the value of field B is updated. Updates on any other field do not trigger the evaluation of the rule.
  • Field A is only re-rendered when the result of the applicability rule changes, i.e. from true to false or vice versa.

Plugins

All form features other than the core - e.g. validation and applicability rules - are implemented as plugins. The goal behind this concept is to make the form implementation both scalable and extensible. In most simpler cases, the native plugins should be enough to get you going. If necessary though, plugins and rules can be added or replaced to fulfill on specialized requirements.

ℹ️ All native plugins use the methods described below, so you can use those as examples.

Creating field rules

Custom rules can be added to existing plugins. If it fits your needs, than this is the easier option. In general, rules can be created with the createFieldRule() helper function. This function can be used as is, or it can be wrapped for specific plugins. For example, the validation plugin has wrapped this function in createValidationRule().

Creating plugins

Plugins can be replaced and you can create and plug in your own to better fit your needs. To do this you can use the createPlugin() and createFieldRule() methods. To get started you can have a look at the readonlyRules plugin, which is one of the simpler plugins.

Array fields

The implementation of forms with one or more arrays of items is supported by array fields. You can create the specifications for an array field with ...field("yourArrayField").asArray(...).

For example:

type ExampleData = {
  arrayField: Array<{
    booleanField: boolean
    textField: string
  }>
}

const fields = signalForm<ExampleData>().withFields((field) => ({
  ...field("arrayField").asArray({
    fields: (field) => ({
      ...field("booleanField", "Toggle field"),
      ...field("textField", "Text field"),
    }),
  }),
}))

The array field itself and all fields in an array field support the same features and plugins as other fields. Note that field rules in an array form also have access to the parent form.

For example:

...field("textFieldInArray", "Text field in array", {
  rules: [
    applicableIf(
      ({ form }) => form.parent.fields.fieldInParent.value === "some value"
    )
  ]
})

Adding array fields to your form can then be done with the useArrayField() hook and the <ArrayItem /> component. The hook provides a description of the items in the array, which can then be mapped to the ArrayItem component.

For example:

const YourForm = () => (
  <SignalForm fields={yourFields}>
    {/* ... */}
    <YourArrayField />
    {/* ... */}
  </SignalForm>
)

const YourArrayField = () => {
  const { items, itemFields, add } = useArrayField(yourFields.arrayField)
  //             ^ can also be accessed with `yourFields.arrayField.fields`.

  return (
    <>
      {items.map((item) => (
        <YourLayout key={item.id}>
          {/*       ^ make sure to set `key` to `item.id` */}

          <ArrayItem item={item}>
            <TextInput field={itemFields.textField}>

            {/* Other layout and input components */}

            <Button onClick={item.remove}>Remove item</Button>
          </ArrayItem>
        </YourLayout>
      ))}

      <Button onClick={add}>Add item</Button>
    </>
  )
}

The demo includes an example for array fields, and you can find the code in ArrayFieldsDemo.

ℹ️ For better performance when adding and removing items, wrap your array items in React.memo(). In the example above this could be done on the <YourLayout /> component, and you can also find it used in the demo.

Nested forms

Planned.

0.6.0

5 months ago

0.5.0

6 months ago

0.4.0

6 months ago

0.3.0

6 months ago

0.2.4

6 months ago

0.2.4-alpha.3

6 months ago

0.2.4-alpha.2

6 months ago

0.2.4-alpha.1

6 months ago

0.2.3

6 months ago

0.2.2

6 months ago

0.2.0

6 months ago

0.1.3

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago