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

react-ux-form

v1.5.0

Published

A simple, fast and opinionated form library for React & React Native focusing on UX.

Downloads

286

Readme

react-ux-form

mit licence npm version bundlephobia

A simple, fast, and opinionated form library for React & React Native focusing on UX. 👉 Take a look at the demo website.

Setup

$ npm install --save react-ux-form
# --- or ---
$ yarn add react-ux-form

Features

  • Subscription-based field updates (avoid re-render the whole form on each keystroke 🔥)
  • Validation strategies ✨
  • Field sanitization
  • Mounted-only fields validation
  • Advanced focus handling
  • Best-in-class TypeScript support
  • Sync and async field validation
  • Sync and async form submission

Motivation

Why another React form library 🤔? Because, as silly as it seems, we couldn't find any existing library which fits our existing needs:

  • We want validation strategies per field because we fell in love with them when we read the re-formality documentation (which is unfortunately only available for ReScript).
  • It should be able to handle huge forms without a single performance hiccup.
  • Validation should be simple, reusable, and testable (aka just functions).
  • It shouldn't even try to validate unmounted fields.
  • It should have built-in focus management (to improve the keyboard flow of our React Native forms).

Validation strategies ✨

The key of good UX is simple: validation should be executed in continue, feedback should be provided when it makes sense.

Quick example: A credit card field 💳

Let's say we want to display a valid state icon (✔) when the input value is a valid credit card number but don't want to display an error until the user blurs the field (and lets the value in an invalid state).

Something like this:

Valid credit card Invalid credit card

How do we easily achieve such magic? With the onSuccessOrBlur strategy 🧙‍♂️

const {} = useForm({
  cardNumber: { initialValue: "", strategy: "onSuccessOrBlur" },
});

Of course, onSuccessOrBlur will not fit perfectly every use-case! That's precisely why every field config could declare its own strategy:

| Strategy | When feedback will be available? | | ----------------- | ------------------------------------------------------------- | | onChange | On first change (as the user types or update the value) | | onSuccess | On first validation success | | onBlur | On first field blur | | onSuccessOrBlur | On first validation success or first field blur (default) | | onSubmit | On form submit |

Note that:

  • The strategies will only be activated after the field value update / the form submission.
  • Once the first feedback is given (the field is valid or should display an error message), the field switches to what we call "talkative" state. After that, feedback will be updated on each value change until this field or the form is reset.

API

⚠️ The API is described using TypeScript pseudocode.These types are not exported by the library / are not even always valid.

useForm

useForm takes one argument (a map of your fields configs) and returns a set of helpers (functions, components, and values) to manage your form state.

import { useForm } from "react-ux-form";

const {
  formStatus,
  Field,
  FieldsListener,
  getFieldState,
  setFieldValue,
  setFieldError,
  focusField,
  resetField,
  sanitizeField,
  validateField,
  listenFields,
  resetForm,
  submitForm,
} = useForm({
  // Keys are used as fields names
  fieldName: {
    initialValue: "",
    // Properties below are optional (those are the default values)
    strategy: "onSuccessOrBlur",
    debounceInterval: 0,
    equalityFn: (value1, value2) => Object.is(value1, value2),
    sanitize: (value) => value,
    validate: (value, { focusField, getFieldState }) => {},
  },
});

Field config

type fieldConfig = {
  // The initial field value. It could be anything (string, number, boolean…)
  initialValue: Value;

  // The chosen strategy. See "validation strategies" paragraph
  strategy: Strategy;

  // An amount of time (in ms) to wait before triggering validation
  debounceInterval: number;

  // When performing async validation, it might happen that the value has changed between the start and the end of its execution
  // That's why we compare the two values: to ensure that the feedback given to the user is correct
  equalityFn: (value1: Value, value2: Value) => boolean;

  // Will be run on value before validation and submission. Useful from trimming whitespaces
  sanitize: (value: Value) => Value;

  // Used to perform field validation. It could return an error message (or nothing)
  // It also handle async: simply return a Promise that resolves with an error message (or nothing)
  validate: (value: Value) => ErrorMessage | void | Promise<ErrorMessage | void>;
};

formStatus

type formStatus =
  | "untouched" // no field has been updated
  | "editing"
  | "submitting"
  | "submitted";

<Field />

A component that exposes everything you need locally as a children render prop.

<Field name="fieldName">
  {
    (props: {
      // A ref to pass to your element (only required for focus handling)
      ref: MutableRefObject;
      // The field value
      value: Value;
      // Is the field validating? (only happen on async operations)
      validating: boolean;
      // Is the field valid?
      valid: boolean;
      // The field is invalid: here its error message.
      error?: ErrorMessage;
      // The onBlur handler (required for onBlur and onSuccessOrBlur strategies)
      onBlur: () => void;
      // The onChange handler (required)
      onChange: (value: Value) => void;
      // Focus the next field (uses the field config declaration order in useForm)
      focusNextField: () => void;
    }) => /* … */
  }
</Field>

<FieldsListener />

A component that listens for fields states changes. It's useful when a part of your component needs to react to fields updates without triggering a full re-render.

<FieldsListener names={["firstName", "lastName"]}>
  {
    (states: Record<"firstName" | "lastName", {
      // The field value
      value: Value;
      // Is the field validating? (only happen on async operations)
      validating: boolean;
      // Is the field valid?
      valid: boolean;
      // The field is invalid: here its error message.
      error?: ErrorMessage;
    }>) => /* … */
  }
</FieldsListener>

getFieldState

By setting sanitize: true, you will enforce sanitization.

type getFieldState = (
  name: FieldName,
  options?: {
    sanitize?: boolean;
  },
) => {
  value: Value;
  validating: boolean;
  valid: boolean;
  error?: ErrorMessage;
};

setFieldValue

By setting validate: true, you will enforce validation. It has no effect if the field is already talkative.

type setFieldValue = (
  name: FieldName,
  value: Value,
  options?: {
    validate?: boolean;
  },
) => void;

setFieldError

Will make the field talkative.

type setFieldError = (name: FieldName, error?: ErrorMessage) => void;

focusField

Will only work if you forward the Field provided ref to your input.

type focusField = (name: FieldName) => void;

resetField

Hide user feedback (the field is not talkative anymore). If feedbackOnly is not set to true, value will also be resetted to initialValue.

type resetField = (
  name: FieldName,
  options?: {
    feedbackOnly?: boolean;
  },
) => void;

sanitizeField

Sanitize the field value.

type sanitizeField = (name: FieldName) => void;

validateField

Once you manually call validation, the field automatically switches to talkative state.

type validateField = (name: FieldName) => Promise<ErrorMessage | void>;

listenFields

A function that listen for fields states changes. Useful when you want to apply side effects on values change.

React.useEffect(() => {
  const removeListener = listenFields(
    ["firstName", "lastName"],
    (states: Record<"firstName" | "lastName", {
      // The field value
      value: Value;
      // Is the field validating? (only happen on async operations)
      validating: boolean;
      // Is the field valid?
      valid: boolean;
      // The field is invalid: here its error message.
      error?: ErrorMessage;
    }>) => /* … */
  );

  return () => {
    removeListener();
  }
}, []);

resetForm

Hide user feedback for all fields (they are not talkative anymore). If feedbackOnly is not set to true, values will also be resetted to their corresponding initialValue and formStatus will be resetted to untouched.

type resetForm = (options?: { feedbackOnly?: boolean }) => void;

submitForm

Submit your form. Each callback could return a Promise to keep formStatus in submitting state.

type submitForm = (
  onSuccess: (values: Partial<Values>) => Promise<unknown> | void,
  onFailure?: (errors: Partial<ErrorMessages>) => Promise<unknown> | void,
  options?: {
    // by default, it will try to focus the first errored field (which is a good practice)
    avoidFocusOnError?: boolean;
  },
) => void;

combineValidators

As it's a very common case to use several validation functions per field, we export a combineValidators helper function that allows you to chain sync and async validation functions: it will run them sequentially until an error is returned.

import { combineValidators, useForm } from "react-ux-form";

const validateRequired = (value: string) => {
  if (!value) {
    return "required";
  }
};

const validateEmail = (email: string) => {
  if (!/.+@.+\..{2,}/.test(email)) {
    return "invalid email";
  }
};

const MyAwesomeForm = () => {
  const { Field, submitForm } = useForm({
    emailAddress: {
      initialValue: "",
      // will run each validation function until an error is returned
      validate: combineValidators(
        isEmailRequired && validateRequired, // validation checks could be applied conditionally
        validateEmail,
      ),
    },
  });

  // …
};

toOptionalValidator

Very often, we want to execute validation only if a value is not empty. By wrapping any validator (or combined validators) with toOptionalValidator, you can bypass the validation in such cases.

import { toOptionalValidator, Validator } from "react-ux-form";

// This validator will error if the string length is < 3 (even if it's an empty string)
const validator: Validator<string> = (value) => {
  if (value.length < 3) {
    return "Must be at least 3 characters";
  }
};

// This validator will error if value is not empty string and if the string length is < 3
const optionalValidator = toOptionalValidator(validator);

This function also accept a second param (required for non-string validators) to specify what is an empty value.

import { toOptionalValidator, Validator } from "react-ux-form";

const validator: Validator<number> = (value) => {
  if (value < 10) {
    return "Must pick at least 10 items";
  }
};

// This validator will also accept a value of 0, as we consider it "empty"
const optionalValidator = toOptionalValidator(validator, (value) => value === 0);

hasDefinedKeys

As some of your fields might be unmounted on submit, the submitForm method could not guarantee that every field value is defined and valid. We export hasDefinedKeys helper function that allows you to test if some object keys are defined.

import { hasDefinedKeys, useForm } from "react-ux-form";

const MyAwesomeForm = () => {
  const { Field, submitForm } = useForm({
    firstName: { initialValue: "" },
    lastName: { initialValue: "" },
  });

  const handleSubmit = () => {
    submitForm((values) => {
      if (hasDefinedKeys(values, ["firstName", "lastName"])) {
        // values.firstName and values.lastName are defined (the fields are mounted)
      }
    });
  };

  // …
};

Quickstart

import { useForm } from "react-ux-form";

const MyAwesomeForm = () => {
  const { Field, submitForm } = useForm({
    firstName: {
      initialValue: "",
      strategy: "onSuccessOrBlur",
      sanitize: (value) => value.trim(), // we trim value before validation and submission
      validate: (value) => {
        if (value === "") {
          return "First name is required";
        }
      },
    },
  });

  return (
    <form
      onSubmit={(event: React.FormEvent) => {
        event.preventDefault();

        submitForm(
          (values) => console.log("values", values), // all fields are valid
          (errors) => console.log("errors", errors), // at least one field is invalid
        );
      }}
    >
      <Field name="firstName">
        {({ error, onBlur, onChange, valid, value }) => (
          <>
            <label htmlFor="firstName">First name</label>

            <input
              id="firstName"
              onBlur={onBlur}
              value={value}
              onChange={({ target }) => {
                onChange(target.value);
              }}
            />

            {valid && <span>Valid</span>}
            {error && <span>Invalid</span>}
          </>
        )}
      </Field>

      <button type="submit">Submit</button>
    </form>
  );
};

More examples

A full set of examples is available on the demo website or in the /website directory project. Just clone the repository, install its dependencies and start it!

Acknowledgements