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-peasy-form

v0.2.1

Published

Simple and (mostly) familiar react form components

Downloads

12

Readme

Peasy Form

npm install react-peasy-form

<Form />

The base form component and context provider for all enclosed form fields.

<Form
    defaults={{ 
        title: 'hello', 
        name: { first: 'John', last: 'Doe'  } 
    }}
    onSubmit={values => console.log(values)}
>
    <div>
        <h2>Inputs</h2>

        <Input 
            name="title" 
            transform={v => v.toUpperCase()}
            type="text" 
        />

        <Input 
            name="name.first" 
            type="text" 
            validate={v => {
                if (v === 'bob') {
                    const err = (
                        <span className="error">
                            No Bobs allowed!
                        </span>
                    )

                    return [err]
                }

                return true
            }}
        />

        <Input name="name.last" type="text" />
    </div>

    <div>
        <h2>Values</h2>
        <ul>
            <li><Value name="title" /></li>
            <li>
                <Value 
                    name={['name.first', 'name.last']}
                    render={([firstName, lastName]) => {
                        return (
                            <span>
                                <strong>Name:</strong>
                                {firstName} {lastName}
                            </span>
                        )
                    }} 
                />
            </li>
        </ul>
    </div>

    <div>
        <h2>Errors</h2>
        <Error name="name.first" />
    </div>

    <Status render={status => {
        const disabled = status === FormStatus.VALIDATING || 
            status == FormStatus.INVALID
        return (
            <button disabled={disabled} type="submit">Submit</button>
        )
    }} />
</Form>

Props

defaults?: { [key: string]: any }

Default form values in the form of key/value pairs where the key is a form field name, and the value is the form field value. Values in nested objects can be accessed through dot notation.

validateOn?: 'change' | 'blur' | 'submit'

The default setting describing when to trigger field validation. This can be overridden on a per-field basis

validationStrategy?: (value: any, validation: any) => true | Array<string | Error | JSX.Element>

Applies a field validation to a field value. This can be used to support various third party validation libraries.

onSubmit(values: { [key: string]: any }): void

Executed when the form is submitted. onSubmit gets passed an object mapping field names to transformed field values. Nested values are supported through dot notation.

preValidate?(): void

Executed before a form's onSubmit validation occurs.

onError?(errors: { [key: string]: Array<string | Error | JSX.Element> }): void

Executed when a form's onSubmit validation fails

Form Fields

Supported Input Fields

  • <Input />
  • <Select />
  • <Textarea />

Props

Each field supports all the props of the field it is shadowing, plus:

name: string

The key associated with a form field's value. Supports nested objects through dot notation.

transform?: (value: any) => any

Used to transform a fields before validation occurs. The transformed value is returned by onSubmit

validate?: any

A function used to validate a field's value. The function signature when using the default validation strategy is (value: any) => true | Array<string | Error | JSX.Element>

validateOn?: 'submit' | 'blur' | 'change'

Override the form's default validateOn


<Value />

Outputs the transformed value of a form field.

Props

name: string | string[]

The field name or names to render

render?: (value: any | any[], status: FormStatus | FormStatus[]) => JSX.Element

enum FormStatus {
  PRISTINE,
  DIRTY,
  INVALID,
}

<Status />

Outputs the status of the form or a specific field.

Props

name?: string

If defined, only this field's status will be passed to render

render: (status: FormStatus) => JSX.Element


<Err />

Outputs any errors associated with a field.

Props

name: string

The desired field's name

render?: (errors: Array<string | Error | JSX.Element>) => JSX.Element


TODO:

  • Add live examples