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-t-shirt-forms

v0.0.1

Published

Simple forms, simple code.

Downloads

19

Readme

👕 React T-Shirt Forms

Sometimes life calls for more than a t-shirt. But t-shirts are easy, and comfortable - a good default until you need more.

Another Form Library?

Making a form in React is hard. Needlessly hard, with a lot of boilerplate - even when using any of the popular React form libraries.

T-Shirt Forms is intended to make creating simple forms simple.

It is powerful enough to handle complex and irregular cases, but it is more heavily geared towards reducing the boilerplate around regular forms.

If your app has lots of complex and irregular forms, then this library is probably not going to be the best solution. I would recommend looking at Formik instead.

Installing

npm install --save-dev react-t-shirt-forms

or

yarn add -D react-t-shirt-forms

Usage

API

See the API docs.

Examples

We show some basic examples on this page.

But you can see some more common flows in the examples.

Bare Bones Form

import TShirtForm from "react-t-shirt-forms";

// by default no styling is included in your bundle
// but you can pull in a basic CSS file if you like
import "react-t-shirt-forms/dist/stylesheets/basic.min.css";

const formSchema = {
  name: {
    type: "string",
    label: "Name",
    initialValue: "Bruce Wayne"
  },
  email: {
    type: "email",
    label: "Email"
  }
};

const onFormSubmit = async ({ formArgs }) => {
  console.log(JSON.stringify(formArgs, null, 2));
};

const App = () => (
  <TShirtForm schema={formSchema} handleSubmit={onFormSubmit} />
);

Validation

T-Shirt Forms allows integration with any validation library, but comes with out of the box hooks for yup.

import * as yup from "yup";
import { addValidation, yupSupport } from "react-t-shirt-forms";

// this tells T-Shirt Forms to use `yup` for any validations
// it also adds default validations for common fields
addValidation(yup, yupSupport);

Now you can use yup validations in your form schema:

const formSchema = {
  name: {
    type: "string",
    label: "Name",
    validation: yup()
      .string()
      .min(10)
  }
};

Custom Fields

If you have a custom React component that you need to use for all password inputs, you can specify a global rule for this.

import { setSchemaDefaults } from "react-t-shirt-forms";

const MyCustomPasswordInput = ({ onChange, onBlur, value, label }) => (
  <label>
    {label}
    <input
      type="password"
      value={value}
      onChange={e => onChange(e.target.value)}
      onBlur={e => onBlur(e)}
    />
  </label>
);

setSchemaDefaults({
  type: {
    password: {
      component: MyCustomPasswordInput
    }
  }
});

and now whenever you use a password input field

const formSchema = {
  myPasswordField: {
    type: "password",
    label: "My custom password field"
  }
};

you'll see that it's using your MyCustomPasswordInput component.

Bundle Size

T-Shirt Forms makes an effort to be minimal and modular.

Here's a look at its size, after tree-shaking, in different contexts:

  • bare bones (21.19 KB / 5.71 KB gzipped)
  • with CSS stylings (29.40 KB / 8.51 KB gzipped)
  • with yup validation (29.45 KB / 8.53 KB gzipped)

Contributing

Contributors are welcome! 😊

Check out the CONTRIBUTING.md.

License

MIT