0.5.2 • Published 5 years ago

forms-builder v0.5.2

Weekly downloads
14
License
MIT
Repository
github
Last release
5 years ago

npm.io

Forms builder

npm Travis license

Overview

Imagine that validating and submitting form can be so simple

Installation

npm install forms-builder --save

Samples

Custom input

One way to write a field component:

interface OwnProps {
  // Your input props
}

type ComponentProps =
  OwnProps &
  FieldData<string>

export class TextField extends React.Component {
  render() {
    const { value, label } = this.props

    return <input
      type="text"
      onFocus={this.props.onFocus}
      onBlur={this.props.onBlur}
      onChange={this.onChange}
      value={value != null ? value : ''}
    />
  }

  onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    this.props.onChange(e.currentTarget.value)
  }
}

Simple form

interface LoginDTO {
  login: string
  password: string
}

class ComponentWithForm extends React.Component {
  form = formBuilder<LoginDTO>(
    {
      login: {
        validate: value => !value && 'required'
      },

      password: {
        validate: value => !value && 'required'
      }
    })
    .configure({
      submit: values => {
        // TODO...
      }
    })
    .build(this)

  render() {
    const { fields, handleSubmit } = this.form

    return <form onSubmit={handleSubmit}>
      <TextField {...fields.login} />
      <TextField {...fields.password} />
      <Button type="submit">Login</Button>
    </form>
  }
}

Api

Project written in TypeScript. So all api have intellisense!

0.5.2

5 years ago

0.5.1

5 years ago

0.4.4

5 years ago

0.5.0

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago