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-uforms

v3.7.1

Published

Simple and elegant forms for your React application

Downloads

346

Readme

react-uforms

npm version build status TypeScript NPM

Simple and elegant forms for your React application.

Installation

Using Yarn

yarn add react-uforms

Or NPM

npm install react-uforms --save

Usage

1. Simple example

import { Form, Text } from 'react-uforms';

const example = (
  <Form onSubmit={(formApi, values) => console.log(values)}>
    <label htmlFor="email">Email</label>
    <Text type="text" id="email" name="email" />
    
    <label htmlFor="password">Password</label>
    <Text type="password" id="password" name="password" />
    
    <button type="submit">Submit</button>
  </Form>
);

2. Validation

import { Form, Text } from 'react-uforms';
import * as yup from 'yup';
    
const example = (
  <Form
    validation={yup.object({
      email: yup.string().required('Email is required').email(),
      password: yup
        .string()
        .required('Password is required')
        .matches(/^(?=.*[a-z]).+$/, 'At least 1 lowercase alphabetical character')
        .matches(/^(?=.*[A-Z]).+$/, 'At least 1 uppercase alphabetical character')
        .matches(/^(?=.*\d+).+$/, 'At least 1 numeric character'),
    })}
    onSubmit={(formApi, values) => console.log(values)}
    onError={(formApi, errors) => console.log(errors)}
  >
    <label htmlFor="email">Email</label>
    <Text type="text" id="email" name="email" />
  
    <label htmlFor="password">Password</label>
    <Text type="password" id="password" name="password" />
  
    <button type="submit">Submit</button>
  </Form>
);

3. Pre-filled form

import { Form, Text, TextArea } from 'react-uforms';
import * as yup from 'yup';
    
const example = (
  <Form
    defaultValues={{
      id: 1,
      email: '[email protected]',
      profile: {
        firstName: 'Foo',
        lastName: 'Bar',
        bio: 'Travel blogger',
      },
      createdAt: '2018-04-25T23:36:02+00:00'
    }}
    validation={yup.object({
      email: yup.string().required('Email is required').email(),
      profile: yup.object({
        firstName: yup.string().required(),
        lastName: yup.string().required(),
        bio: yup.string(),
      }),
    })}
    onSubmit={(formApi, values) => console.log(values)}
    onError={(formApi, errors) => console.log(errors)}
  >
    <label htmlFor="email">Email</label>
    <Text type="text" id="email" name="email" />

    <label htmlFor="firstName">First Name</label>
    <Text type="text" id="firstName" name="profile.firstName" />

    <label htmlFor="lastName">Last Name</label>
    <Text type="text" id="lastName" name="profile.lastName" />

    <label htmlFor="bio">Bio</label>
    <TextArea id="bio" name="profile.bio" />

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

4. Errors customization

import { Form, Text, FieldError } from 'react-uforms';
import * as yup from 'yup';
    
const example = (
  <Form
    validation={yup.object({
      email: yup.string().required('Email is required').email(),
      profile: yup.object({
        firstName: yup.string().required(),
        lastName: yup.string().required(),
        bio: yup.string(),
      }),
    })}
    classes={{
      field: {
        error: "your-error-class",
        invalid: "your-invalid-input-class",
      },
    }}
    onSubmit={(formApi, values) => console.log(values)}
    onError={(formApi, errors) => console.log(errors)}
  >
    <label htmlFor="email">Email</label>
    <Text type="text" id="email" name="email" hideError={true} />
    <strong><FieldError name="email" /></strong>

    <label htmlFor="firstName">First Name</label>
    <Text type="text" id="firstName" name="profile.firstName" hideError={true} />
    <FieldError name="profile.firstName" className="add-some-error-class" />

    <label htmlFor="lastName">Last Name</label>
    <Text type="text" id="lastName" name="profile.lastName" />

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

5. Other fields

import { Form, Text, Select, TextArea, RadioGroup, RadioGroupItem, Checkbox } from 'react-uforms';
    
const example = (
  <Form
    onSubmit={(formApi, values) => console.log(values)}
    onError={(formApi, errors) => console.log(errors)}
  >
    <label htmlFor="email">Email</label>
    <Text id="email" name="email" disabled={true} />

    <label htmlFor="password">Password</label>
    <Text type="password" id="password" name="password" />

    <label htmlFor="country">Country</label>
    <Select
      id="country"
      name="country"
      options={[
        { value: null, name: 'Select country' },
        { value: 'US', name: 'United States' },
        { value: 'CA', name: 'Canada' },
        { value: 'UK', name: 'United Kingdom', disabled: true }
      ]}
    />

      <div className="radio-group">
        <RadioGroup name="gender">
          <div className="radio">
            <RadioGroupItem value="male" id="e7_gender_male" />
            <label htmlFor="e7_gender_male">Male</label>
          </div>
          <div className="radio">
            <RadioGroupItem value="female" id="e7_gender_female" />
            <label htmlFor="e7_gender_female">Female</label>
          </div>
        </RadioGroup>
      </div>

    <label htmlFor="bio">Bio</label>
    <TextArea id="bio" name="bio" emptyValue={null} />

    <div className="checkbox-group">
      <div className="checkbox">
        <Checkbox name="newsletter" onValue={1} offValue={0} id="newsletter" />
        <label htmlFor="newsletter">Receive Weekly Updates</label>
      </div>
    </div>
    
    <button type="submit">Submit</button>
  </Form>
);

6. Custom field

import { Form, Field } from 'react-uforms';

const example = (
  <Form
    onSubmit={(formApi, values) => console.log(values)}
    defaultValues={{
      email: '[email protected]',
      profile: {
        isPublic: true,
        firstName: 'John',
        lastName: 'Brown',
      },
    }}
  >
    <Field name="profile.isPublic">
      {({ setValue, getValue }) => (
        <button type="button" onClick={() => setValue(!getValue())}>
          {getValue() ? 'on' : 'off'}
        </button>
      )}
    </Field>
    <button type="submit">Submit</button>
  </Form>
);

Authors

License

MIT License - see the LICENSE file for details