2.0.5 • Published 6 years ago

micro-form v2.0.5

Weekly downloads
83
License
MIT
Repository
github
Last release
6 years ago

micro-form

Hyper-minimal form building library for React. Easily hydrate values, validate fields, and handle error states. No magic. <3kb.

js-standard-style

Usage

import myApi from './myApi.js'
import { createForm, createFactory, createField } from 'micro-form'

const Form = createForm(({ state, valid, validateForm, getPayload, resetForm }) => (
  <form onSubmit={e => {
    e.preventDefault()

    if (validateForm()) {
      myApi(getPayload()).then(() => {
        resetForm()
      })
    }
  }}>
    {children}
  </form>
))

const Input = createFactory(({ name, value, valid, validateField, updateField, ...userDefinedProps }) => (
  <div>
    <label htmlFor={name}>{userDefinedProps.label}</label>
    <input name={name} value={value} type={userDefinedProps.type} onChange={e => {
      updateField(e.target.value)
      validate()
    }} />
  </div>
))

const Email = createField({
  name: 'email',
  initialValue: ''
})(({ name, value, valid, updateField, validateField, ...userDefinedProps }) => (
  <div>
    <label htmlFor={name}>Email</label>
    <input name={name} value={value} type='email' onChange={e => {
      updateField(e.target.value)
      validate()
    }} />
  </div>
))

const App = props => (
  <Form>
    <Input
      name='name'
      type='text'
      initialValue='Eric Bailey'
      validate={val => val !== ''}
      label='Name' />
    <Email />
  </Form>
)

Dependencies

MIT License

0.0.4

6 years ago

2.0.5

6 years ago

0.0.3

6 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.4

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.0

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago