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

form-provider

v1.0.11

Published

React helpers for building forms.

Downloads

6

Readme

form-provider

npm Build Status

A set of React helpers to help with building forms. State is managed with a Redux store that is local to your component. This promotes keeping your ui state separate from your global application state while still being able to use the redux ecosystem.

Demos:

Installation

npm install --save react react-dom redux react-redux # peer dependencies
npm install --save form-provider

Basic Usage

// BasicForm.js
import { withForm, FormProvider, Field } from 'form-provider'

function createForm(props) {
  return {
    field1: props.field1,
    obj: {
      field2: 4
    }
  }
}

function BasicForm({ form, onSubmit }) {
  return (
    <FormProvider form={form} onSubmit={onSubmit}>
      <form onSubmit={preventDefault(form.submit)}>
        <h3>Basic Form</h3>
        <Field path='field1' validate={[isRequired('Field1'), isNotNumber('Field1')]}>
          {({ value, setValue, error }) =>
            <div>
              <label>Field1*</label>
              <input type='text' value={value} onChange={target(setValue)} />
              { error && <div>{ error.message }</div> }
            </div>
          }
        </Field>
        <Field path='obj.field2'>
          {({ value, setValue }) =>
            <div>
              <label>Field2</label>
              <input type='number' value={value} onChange={target(toNumber(setValue)))} />
            </div>
          }
        </Field>
        <button type='submit'>Save</button>&nbsp;
        <button type='button' onClick={form.reset}>Reset</button>
      </form>
    </FormProvider>
  )
}

export default withForm(createForm)(BasicForm)

Check out the basic form example for the entire source.

Initial state

Setting initial form state is done by passing it into withForm

...

export default withForm({
  user: {
    firstName: 'john'
  }
})(BasicForm)

// or as a function of props
export default withForm(props => ({
  user: props.user
}))(BasicForm)

Validation

This lib currently doesn't provide any validation functions out of the box, only an API to provide your own. Validators are functions that accept the current value and return a promise. Pass in a single validator or an array to the <Field> component. The form won't submit until all validators are resolved.

// validators.js

export const isRequired = (name) => (value) => new Promise((resolve, reject) => {
  if (!value) return reject(new Error(`${name} is required`))
  resolve()
})

export const isNotNumber = (name) => (value) => new Promise((resolve, reject) => {
  if (!isNaN(value)) return reject(new Error(`${name} must not be a number`))
  resolve()
})

Binding to form state

Use the connectForm function to map form state to props. This function has the exact same API as react-redux's connect function. You can use this to conditionally display fields or other rendering logic based on the current form's state.

import { compose } from 'redux'
import { withForm, FormProvider, Field, connectForm } from 'form-provider'

function mapFormStateToProps(formState) {
  return {
    userFormState: formState.user,
    allErrors: formState.errors
  }
}

function BasicForm({ userFormState, allErrors, form, onSubmit }) {
  ...
})

export default compose(
  withForm(createForm)
  connectForm(mapFormStateToProps)
)(withForm)

Alternatives