1.0.0 • Published 5 years ago

gsd-form v1.0.0

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

GsdForm

Formik-based React library to create forms from plain JS objects (or JSON!)

Demo

Single fields object

Multiple field sets

Installing

yarn add gsd-form

Usage

import GsdForm from 'gsd-form'
import 'gsd-form/style.css' // Optional

class App extends Component {
  render() {
    return <GsdForm data={data} />
  }
}

Form data example

data = {
  form: {
    submitButton: {
      text: 'Send', // Optional. Default : Submit
      component: CustomComponent,
    },
    fields: { ... }, // Inline form
    // For forms sections you can use an array with nested fields
    // P.S.: These fields will be flattened on submit
    fields: [
      {
        name: 'String',
        fields: { ... } // Equal inline form
      }
    ],
  },
  recaptcha: {
    size: 'invisible', // one of compact, normal, invisible
    sitekey: 'SITEKEY_CODE',
  },
  showFormState: Boolean // Optional
  honeypot: Boolean // Optional
}

Field props

PropertyTypeDescription
labelStringLabel text
nameStringField (HTML input) name
componentStringOptions - input, textarea, select
fieldClassString-
disabledStringdisabled
valueStringInitial (default) value
formatStringOptions - numeric, phone, date
validateArray<String>[Yup validator, Optional custom error message]ex.: validate: [ 'string', ['email', 'Invalid email format'], 'required', ]More
Field Select--
optionsArray<Object>Options for select inputs - [{ value: 'One', label: 'One' }]
placeholderStringselect placeholder text'
noOptionsMessageStringFallback text for empty select options

You can also pass any other ReactSelect prop to select fields, such as menuIsOpen.

Method props

class App extends Component {
  handleSubmit (value, setters) {
    // send to a REST API ...
    console.log('onSubmit', value)
    setters.setSubmitting(false)
  }

  handleChanges (name, value) {
    // Called when each field changes
    console.log('handleChanges', name, value)
  }

  buttonProps (form) {
    // Called when each field changes
    console.log('buttonProps', form)
  }

  render() {
    return (
      <GsdForm
        data={data}
        handleSubmit={this.handleSubmit}
        handleChanges={(name, value) => this.handleChanges(name, value)}
        buttonProps={form => this.buttonProps(form)}
      />
    )
  }
}

export default App

See that the handleSubmit function has both parameters passed by Formik: values and a series of setters (errors, values, submitting, ...). You could use those to use backend validation or manage the loading/submitting state, for example.

You can use the handleChanges prop to, for example, change the available options for interdependent select inputs, such as country/state/city.

Another useful GsdForm prop is values, which can be used to pass dynamic values to Formik and make whatever desired inputs fully controlled by your parent component.

Dependencies

Contributing and developing

The projectwas created using create-react-library, so it should have its] basic structure updated soon. Right now, all the lib content should reside inside src, everything else can be used when developing or as examples.

You can start the dev server running:

yarn start

A production version can be generated using:

yarn build
1.0.0

5 years ago

0.1.23

5 years ago

0.1.22

5 years ago

0.1.21

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12-alpha

6 years ago

0.1.11-alpha

6 years ago

0.1.10-alpha

6 years ago

0.1.9-alpha

6 years ago

0.1.8-alpha

6 years ago

0.1.7-alpha

6 years ago

0.1.6-alpha

6 years ago

0.1.5-alpha

6 years ago

0.1.4-alpha

6 years ago

0.1.3-alpha

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago