Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

form-provider

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

form-provider

React helpers for building forms.

  • 1.0.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 02 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc