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

@crossroads-loyalty-solutions/formaggio

v0.2.2

Published

The cheesy form library

Downloads

8

Readme

Formaggio Logo

npm bundle size Dependencies Build Status Codecov License npm Greenkeeper badge

Installation

npm i @crossroads-loyalty-solutions/formaggio

Forms

Example

import {
  Form,
  isEmail,
  required,
  rules,
  useFormField,
} from "@crossroads-loyalty-solutions/formaggio";

const TextInput = ({ name }) => {
  const { dirty, errors, submitted, inputProps } = useFormField(name);
  const className = (dirty || submitted) && errors.length ?
    "form__text-input form__text-input--error" :
    "form__text-input";

  return <input type="text" className={className} {...inputProps} />;
};

const validateMyForm = rules([
  required("name"),
  required("email"),
  isEmail("email")
]);

const MyForm = () => {
  const [data, setData] = useState({});
  const errors = validateMyForm(data);

  return (
    <Form
      noValidate
      name="my-form"
      value={data}
      onChange={setData}
      onSubmit={console.log}
    >
      <TextInput name="name" />
      <TextInput name="email" />

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

useFormField hook

The library provided React Hook useFormField is used to provide access to the state for the given field name which include:

  • dirty: boolean: True if the user has modified this field.
  • submitted: boolean: True if the user has tried to submit the form.
  • errors: Array<ValidationError>: List of errors which apply to this field.
  • inputProps: Object: Properties to apply to the <input /> element.

The inputProps object contains id, name, value and an onChange callback, the idea is to spread the properties into an <input />.

Form component

The <Form /> component provides the form state to any nested uses of useFormField. It will wrap all its children in a <form /> tag.

Properties:

  • errors?: Array<ValidationErrors>: Any validation errors for this form.
  • name?: string: This string will prefix any name and id properties in nested uses of useFormField.
  • onChange: (FormData) => void: Callback for when the form data changes, these changes are performed immutably.
  • onError?: (SyntheticEvent<HTMLFormElement>, Array<ValidationError>, FormData) => void: Callback fired instead of onSubmit if there is an error in the form when the user tries to submit the form.
  • onSubmit: (SyntheticEvent<HTMLFormElement>, FormData) => void: Callback fired with the current form data when the user submits the form.
  • value: FormData: The data for the form.

Any properties not listed above will be propagated to the <form /> element.

To properly handle validation and onChange events in nested <input /> elements it is recommended to set the property noValidate on <Form />.

Validation

Validation is done using functions which take a value to validate and return a list of errors. The validation functions are created using rule-constructors or combinators.

Example

import {
  conditional,
  isEmail,
  lengthGt,
  required,
  rules,
} from "@crossroads-loyalty-solutions/formaggio";

const validator = rules([
  isEmail("email"),
  required("firstname"), 
  required("lastname"),
  conditional(s => s.password, lengthGt("password", 6)),
]);

const data = {
  firstname: "foo",
  lastname: "bar",
  email: "foo@bar",
};

const errors = validator(data);

console.log(errors); // [{ error: "EMAIL", field: "email" }]

Rules

Validation rules are constructed in a declarative manner using rule constructors and rule combinators.

A basic validator has the following type, where T is the type to be validated:

type Validator<T> = (t: T) => Array<ValidationError>;

Errors

Errors are objects containing an error code and a field path. The field path is preserved through combinators and is used to determine which field caused the specific error. Additional properties are allowed but are error-specific.

type ValidationError = {
  error: string,
  field: string,
};