1.0.10 • Published 3 years ago

ez-form-hook v1.0.10

Weekly downloads
56
License
-
Repository
-
Last release
3 years ago

EzForm Hook

EzFormly generates a form from a schema with on the fly customizability!

NPM downloads NPM downloads NPM downloads

License: MIT semantic-release

Why?

  • helps save time writing complex forms
  • allows you to easily share inputs with other forms
  • allows teams to stay consistent with forms
  • handles the repetitive dull parts of creating a form for you!

Hook props:

PropTypeRequiredDescription
submitButtonJSXfalsesubmit button to submit form EX: <button type="submit">custom submit</button>
onSubmitFunctionfalsetriggers on submit button click and returns all values
onUpdateFunctionfalsetriggers formValues update
onChangeFunctionfalsetriggers on input change and returns all values
onBlurFunctionfalsetriggers on input blur and returns all values
nestedErrorsManagerFunctionfalseused when using a nested form
schemaObjecttrueschema based off what form is generated from
validationObjectfalseoverwrites the schema's validation function for an input (extremely useful when wanting to share a validation schema with other things)
initialValuesObjectfalsemaps the initial values to schema
featureFlagsObjectfalsejust a object of feature flags
validateInitialValuesBooleanfalsevalidates initial values once form loads
validateOnChangeBooleanfalsevalidates input on change (default is true)
disabledBooleanfalsedisables inputs if true
multiFormBooleanfalseallows you to create a multi form
clearFormOnSubmitBooleanfalseclear form on submit
viewModeToggledBooleanfalsetoggles viewMode in schema on initial render
showSubmitButtonBooleanfalsedisables the automatic render of the submit button
viewModeFallbackTextStringfalsepasses fallback viewMode text when value is null or undefined. Default is "N/A"
submitButtonTextStringfalsereplaces text on the submit button
submitButtonClassStringfalsepasses css classes down to the submit button
classNameStringfalsepasses css classes down to the main form component
errorClassStringfalsepasses css class to error message

Component Example:

import { EzFormHook } from "ez-form-hook";
const schema = {
  inputs: {
    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" }
      ]
    }
  }
};

const Form = () => {
  const onSubmit = values => {
    console.log(values);
  };

  const { form } = EzFormHook({ schema, onSubmit });

  return <div>{form}</div>;
};
// More complex example

const Form = () => {
  const onSubmit = values => {
    console.log(values);
  };
  const customSubmitButton = <button type="submit">My Custom Submit</button>;

  const { form, clearForm, addFields, removeFields } = EzFormHook({
    schema,
    onSubmit,
    submitButton: customSubmitButton
  });

  return (
    <div>
      {form}
      <button onClick={clearForm}>Clear Form</button>
      <button onClick={addFields}>Add Fields</button>
      <button onClick={removeFields}>Remove Fields</button>
    </div>
  );
};

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
clearFormOnSubmitBooleantrueset to false if you don't want inputs to clear after submit
trackedBooleantrue,allows 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
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 "ez-validation";
schema = {
  inputs: {
    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.0.10

3 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.41

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago