0.6.0 • Published 5 years ago

@amad27/ez-form v0.6.0

Weekly downloads
6
License
-
Repository
-
Last release
5 years ago

EzForm Component

Easily generates a form from a schema. With on the fly customizability!

Inspired from using Formik!

Im tired of typing so much - Ware2Go Team 2019

Component props:

PropTypeRequiredDescription
onSubmitFunctionfalsetriggers on submit button click and returns all values
onBlurFunctionfalsetriggers on input blur and returns all values and event
schemaObjecttrueschema based off what form is generated from
initialValuesObjectnullmaps the initial values to schema
formSchemaArrayfalseCustom schema that 'overwrites' what you have in the regular schema. Allowing you to customize on the fly
showSubmitButtonBooleanfalsefalse hides submit button
disabledBoolean || JSXfalsedisables inputs if true
classNameStringfalsepasses css classes down to the main form component

Component Example:

import { EzForm } from 'ez-form'
const schema = {
  name: {
    label: 'name',
    required: false,
    type: 'text',
    placeholder: 'Whats your name',
  },
  age: {
    label: 'Age',
    required: true,
    type: 'number',
    placeholder: 'Whats your age?',
  },
  favoriteColor: {
    label: 'favoriteColor',
    placeholder: 'Favorite color',
    options: [{ value: "green", label: "green" }, { value: "red", label: "red" }, {value: "blue", label: "blue"}],
  },
}

<EzForm
  schema={schema}
  onSubmit={(values) => {
    console.log(values)
  }}
>
  {(form) => form}
</EzForm>

Schema Format:

KeyTypeDefaultDescription
validateFunctionnulluses this function to validate input
visibleIfFunctionnulladds ability to check if certain criteria is met and then make the input visible if it is
onSubmitFunctionnullon submit run mutation function and return value (Will not run function if value is empty)
labelStringnulltext in input label
initialValueanynullsets initial value of input
typeStringnullinput type: https://www.w3schools.com/html/html_form_input_types.asp
placeholderStringnullsets input placeholder
requiredBooleanfalsesets input as required
trackedBooleantrueallows you to disable value tracking on the input
customComponentJSXnullallows you to put whatever JSX you'd like to put in form
prependHtmlJSXnullappend html after the input
optionsArraynullconverts input to select box options rendered from Array [{value: true, label: "yes"}, { value: false, label: 'no' }]
...ETCANYnullAnything else you add to schema get passed to the inputs as props

Schema Example:

import {EzValidation} from "util/ezValidation"
schema = {
  name: {
    label: 'name',
    initialValue: 'Whats your name',
    validate: (val, vals) => EzValidation(val).isString().minLength(2).errorMessage,
    required: false,
    onSubmit: (val, vals) => {
      if (val.length > 10) {
        return val + " Thats a long Name"
      }
    },
    type: 'text',
    placeholder: 'name',
  },
  length: {
    label: 'length',
    required: true,
    type: 'number',
    placeholder: 'length of package',
  },
  moneyMoney: {
    label: 'Money Money',
    placeholder: 'How much you got?',
    type: 'text',
    prependHtml: (
      <div className="input-group-prepend">
        <span className="input-group-text" id="basic-addon1">
          $
        </span>
      </div>
    ),
  },
  lengthIfOne: {
    label: 'length if one',
    visibleIf: values => values['length'] === '1',
    required: true,
    type: 'number',
    placeholder: 'length',
  },
  selectMulti: {
    label: 'selectMulti',
    type: 'multiSelect',
    placeholder: 'Favorite Color',
    isMulti: true,
    required: true,
    customComponent: Select,
    options: [{ value: 1, label: 'One' }, { value: 2, label: 'Two' }, { value: 3, label: "Three" }],
  },
   favoriteColor: {
    label: 'favoriteColor',
    placeholder: 'Favorite color',
    options: [{ value: "green", label: "green" }, { value: "red", label: "red" }, {value: "blue", label: "blue"}],
  },
  masked: {
    label: 'masked',
    placeholder: 'masked',
    required: true,
    customComponent: InputMask,
    mask: "9999-99-99",
    maskChar: null,
  },
  description: {
    label: 'description',
    placeholder: 'description',
    required: false,
    type: 'textarea',
  },
}
0.6.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago