Skip to content

A field-first form library for React and React Native

License

Notifications You must be signed in to change notification settings

bokelley/fielder

 
 

Repository files navigation

Fielder logo

Fielder

A field-first form library for React and React Native.

build version size coverage licence docs

About

Fielder has been built from the ground up with a field-first approach to validation.

What does this mean?

  • Validation can easily be added and removed to a form
  • Only validate what the user can see (see cross form validation below)
  • No need for a large set of upfront domain knowledge

Features

⚡️Immediate validation

Synchronous validation will update state immediately in the event of a change/blur.

Fewer renders, better performance and no weird "intermediary states".

🔍 Optimized for flexibility

While Yup is supported, you're not limited to using a single large Yup schema. Validation functions receive the form state as well as the field value.

(value, state) => {
  if (state.otherField.value === 'yes' && !value) {
    throw Error('This field is required');
  }
},

🤓User focused API

Users don't want to find out that the value they entered on a previous page is invalid. This is why Fielder encourages field-level validation.

If the field isn't mounted, the value won't be validated. Simple!

💁‍♂️ One way to do things

Fielder has been built with hooks since day one. There aren't any clunky APIs to learn, only useField, useForm and useFormContext.

Your data doesn't need to be coupled to your components (and likely shouldn't be), that's why Fielder doesn't include a component API.

Usage

Install Fielder

Install using your package manager of choice.

npm i fielder

Setting up a form

useForm is where you initiate your form. In order to expose the form to any child components (and subsequently useField), you'll want to expose it via context.

const myForm = useForm();

return <FielderProvider value={myForm}>{children}</FielderProvider>;

Declaring fields

useField is where you harness the power of Fielder.

const [usernameProps, usernameMeta] = useField({
  name: 'username',
  initialValue: 'fielder-user',
  validate: validateUsername,
});

return (
  <>
    <input type="text" {...usernameProps} />
    {usernameMeta.hasChanged && usernameMeta.error && (
      <ErrorMsg>{usernameMeta.error}</ErrorMsg>
    )}
  </>
);

There are a whole number of additional arguments which can be passed to useField which allow you to:

  • Set validation
  • Set when validation is triggered (e.g. on blur, change, etc)
  • Set initial value, error, valid and touched states
  • Set unmount behaviour

Note: Unlike other popular form libraries, Fielder allows you to change config options (such as validation) at any time.

Additional resources

For more info, tutorials and examples, visit the official docs site!

Also check out:

About

A field-first form library for React and React Native

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 98.6%
  • JavaScript 1.4%