0.1.11 • Published 6 years ago

form-pure v0.1.11

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

FormPure JavaScript component

npm version npm bundle size (minified) npm Build Status codecov

Installation

NPM

npm i form-pure --save

Yarn

yarn add form-pure

CDN

<script src="https://unpkg.com/form-pure@latest/lib/form-pure.min.js"></script>

Usage

import FormPure from "form-pure";

new FormPure(element, config);

`element` // Required. Either selector or HTML node.
`config` // Required. Configuration object.

Configuration

PropertyRequiredTypeDescription
fieldstrueArrayArray of fields for the form. Each field element contains type, and attributes.
formfalseObjectGeneral configuration of the form. Contains inputWrapper and attributes properties.
i18nfalseObjectInternalisation object with transition for error messages.
onSubmitfalseFunctionCallback that will be fired on form submit. Returns object with form values.
fields[].typetrueStringTagname of the field. Example: input, button, textarea.
fields[].attributestrueObjectObject of attributes that will be applied to the field. Example: name, placeholder, type, id. Field name is required.
fields[].labelfalseStringContent for label.
fields[].iconfalseStringIf specified - <i></i> will be inserted in the inputWrapper.
fields[].onChangefalseFunctionFires on input event. Returns current value of the field. In case of input with type file, returns current FileList.
form[].inputWrapperfalseObjectConfiguration of the possible wrapper element for field. Contains type and attributes. By default field won't be wrapped.
form[].attributesfalseObjectObject of attributes that will be applied to the form. Example: class, id.

i18n error codes

Error codeDefault valueDescription
valueMissingValue is missingShown when value is missing for the required field.

Example

const form = new FormPure(".form-wrapper", {
  fields: [
    {
      type: "input",
      attributes: {
        name: "name",
        placeholder: "Name",
        type: "text",
        id: "name",
        class: "custom-input",
        required: true,
      },
    },
    {
      type: "button",
      attributes: {
        type: "submit",
        textContent: "Send",
        id: "send",
        class: "custom-button",
      },
    },
  ],
  form: {
    inputWrapper: {
      type: "div",
      attributes: {
        class: "custom-input-wrapper",
      },
    },
    attributes: {
      id: "form",
      class: "custom-form",
    },
  },
  i18n: {
    valueMissing: "Value is missing",
  },
  onSubmit: () => {},
});

Structure

form-pure/
└── lib/
    └── form-pure.min.js

License

MIT

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago