1.5.2 • Published 5 years ago

ez-formbecauseican v1.5.2

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

NPM downloads NPM downloads NPM downloads

License: MIT semantic-release

EzForm Component

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

Component props:

PropTypeRequiredDescription
onSubmitFunctionfalsetriggers on submit button click and returns all values
onBlurFunctionfalsetriggers on input blur and returns all values and event
onChangeFunctionfalsetriggers on input change and returns all values and event
schemaObjecttrueschema based off what form is generated from
initialValuesObjectnullmaps the initial values to schema
featureFlagsObjectnulljust a object of feature flags
validateInitialValuesbooleanfalsevalidates initial values once form loads
schemaModifierArrayfalseCustom 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
viewModeBoolean || JSXfalsetoggles viewMode in schema
groupClassNameStringfalsepasses css classes down to the group component which contains the input, label, and error message
classNameStringfalsepasses css classes down to the main form component
errorClassStringfalsepasses css class to error message
viewModeDefaultTextString"N/A"passes default viewMode text when value is null or undefined
multiFormBooleanfalseallows you to create a multi form

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>
// More complex example
<EzForm
  schema={schema}
  onSubmit={values => {
    console.log(values);
  }}
>
  {({ form, clearForm, addFields, removeFields }) => 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
featureFlagStringnullfeature flag input is associated with
requiredBoolean | Functionfalsesets 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
functionalComponentJSXnullallows you to put whatever Functional Component you'd like to put in form
viewModeComponentFunctionnullallows you to put whatever Component below the label you'd like to render when viewMode is toggled value and values, get passed to function
viewModeClassFunctionnullclass put onto the viewMode Text
customViewModeFunctionnullallows you to put whatever Component you'd like to render when viewMode is toggled schema key value, value, and values, get passed to function
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 a 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"
  }
};
1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.9

5 years ago

1.4.8

5 years ago

1.4.7

5 years ago

1.4.6

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.9

5 years ago

1.3.8

5 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.9.99

5 years ago

0.9.9

5 years ago

0.9.8

5 years ago

0.9.6

5 years ago

0.9.5

5 years ago

0.9.0

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago