0.0.0 • Published 12 months ago

zed-form v0.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

Zed Form

A radically different, yet familiar, approach to forms in React: just let the browser do it

zero re-renders by default

Why?

Perhaps the most frustrating part of React is dealing with forms. The standard React approach to forms is both slow and filled with boilerplate. Some libraries deal with the boilerplate by adding complexity and are still slow.

Zed Form takes a radically different, yet familiar, approach to forms: just let the browser do it.

The eventing system that is already built into the browser is incredibly fast and efficient for dealing with form inputs. React layering a bunch of unnecessary rendering and re-rendering on top of that flow really slows down pages with many form elements as everything is re-rendered on every user keystroke.

With Zed Form, every <input> is an "uncontrolled" input. Instead of forcing the input to have a certain value, and re-rendering the input on every keystroke to force it to have an updated value, Zed Form just let's the user type and keeps track of what they've typed.

On top of that, Zed Form supports all the features you've come to expect form other form libraries:

  • Schema error validation
    • Built in support for zod (but you can supply an adapter for any schema validation library
  • Setting initial values
  • "Touched" tracking, so errors by default aren't shown until the user has interacted with a particular input
  • Opt-in support for tracking a form value via React state (useful for when you need to hide or show form inputs based on a previous input value)

Install

npm i zed-form     # npm
pnpm i zed-form    # pnpm
yarn add zed-form  # yarn

Example

import {Form, useCreateForm} from 'zed-form'

function App() {
  const form = useCreateForm()
  return (
    <Form form={form}>
      <input type="text" {...form.register('foo')} />
      <input type="text" {...form.register('bar')} />
    </Form>
  )
}

Example with Schema Validation

import {Form, useCreateForm, useFormValue} from 'zed-form'
import zod from 'zod'

const schema = zod.object({
  foo: zod.enum(['foo', 'baz']),
})

function App() {
  const form = useCreateForm({
    initialValues: {foo: 'foo'},
    validationSchema: schema,
  })

  return (
    <Form form={form}>
      <TextField type="text" {...form.register('foo')} />
      <TextField type="text" {...form.register('bar')} />
    </Form>
  )
}

function TextField(props: React.ComponentProps<'input'>) {
  const error = useFormValue(`error:${props.name}`)

  return (
    <div>
      <input {...props} className="w-full" />
      {error && <p style={{color: 'red'}}>{error}</p>}
    </div>
  )
}