0.8.0 • Published 4 years ago

@airglow/prefab-form v0.8.0

Weekly downloads
27
License
Apache-2.0
Repository
github
Last release
4 years ago

Prefab-Form

A custom Prefab for dealing with forms and form elements. The basic Value Prefab let's you set and get the value. The FormField prefab adds in bells and whistles like validation.

Installation

npm install --save @airglow/prefab @airglow/prefab-form

Getting Started

To add Form Prefabs, you simply need to import the prefab somewhere in your code:

import '@airglow/prefab-form';

Now you're ready to go.

FormField Prefabs

You may choose to use the FormField Prefabs directly:

import prefab from '@airglow/prefab';

export default prefab({
  toppings: {
    type: 'formField',
    defaultValue: 'cheese',
    required: true,
    whenToValidate: 'blur'
  },
  size: {
    type: 'formField',
    fieldType: 'number',
    defaultValue: 2,
    max: 15,
    autocorrect: 'immediate'
  }
});

Here we're creating two FormField Prefabs. One is a text field and one is a number field. Let's go into details on the various configuration options for the form fields.

fieldType

The following lists the supported values for fieldType:

TypeDescriptionExample
text (default)A text stringHello World
numberA number7.2
blobCan be any type. Doesn't really have validation{ a: 'b' }
emailAn email addressabc@xyz.com
alphanumA combination of numbers and lettersABC123
ipA valid ip address21.34.106.99
uriA complete URLhttp://www.abc.com/index.html

whenToValidate

You may use the whenToValidate option when configuring your form fields to determine when you want validation to occur.

The following are valid options for whenToValidate:

  • always: will always show validation errors, even before editing
  • focus: will show validation errors as soon as an element is clicked on
  • blur: will show validation errors after clicking outside the elemeent
  • submit: only starts showing validation errors once the user submits the form
  • never: never show validation errors

Errors will be passed when selecting your form fields if there is a validation error and the whenToValidate event has occured.

autocorrect

When enabled, your form fields can attept to autocorrect invalid values. For example, if the maximum number for a field is 30 and a user enters 35, the value will change to 30 if autocorrect is enabled.

The following are valid options for autocorrect:

  • disabled: never autocorrect
  • enabled: autocorrects on a blur event
  • immediate: autocorrects as the field recieves input.

sourceSelector

Defines a selector that should be called to get the current (undirty) data for the form. The selector will get the current state passed in:

export default prefab({
  quantity: {
    type: 'formField',
    sourceSelector: state => state.originalData.quantity,
  }
});

Aditional Options

OptionDescriptionAutocorrects?fieldTypes
requiredis this value requiredNoall
defaultthe default valueN/Aall
minthe min value (number fieldType) or min string length(for numbers)all
maxthe max value (number fieldType) or max string lengthYesall
greaternumber must be greater than the valueYesnumber
lessnumber must be lest than the valueYesnumber
lengthabsolute string lengthNotext
regexstring must match provided regular expressionNotext
integerenforces the number must be an integer (no decimals)Yesnumber
precisionthe number must have fewer decimal points than thisYesnumber
stepthe amount jump when clicking the up and down chevronsN/Anumber

Selectors

The a FormField prefab provdies a bunch of different selectors you can pass into your views:

SelectorInputDescription
valuestateReturns the current value for the field.
errorstateReturns an error if the data is invalid and the whenToValidate event has triggered.
isInvalidstateIs the data currently invalid (reguardless of the whenToValidate flag)
isDirtystatehas the value changed from the source/default value
changeActionvalue to change toan action that can be dispatched to update the field's value
resetActionnoneaction to dispatch to reset the stored value of the field
statestatepulls all the data needed to display the field into an object
handlersdispatchpulls all the event handlers needed to react to the field into an object

Form

The Form Prefab is a convientient way to group a set of FormField Prefabs together:

import prefab from '@airglow/prefab';

export default prefab({
  pizzaOrder: {
    fields: {
      toppings: {
        defaultValue: 'cheese',
        required: true,
        whenToValidate: 'blur'
      },
      size: {
        fieldType: 'number',
        defaultValue: 2,
        max: 15,
        autocorrect: 'immediate'
      }
    }
  }
});

Field Names

To avoid name conflicts, inside your store, the form field data will be stored at <FORM NAME>#<FIELD_NAME>.

When selecting data for the view, the fields will be <FIELD_NAME>Field, unless you provide a fieldName property in the FormField's config.

When exporting data, the field will just be <FIELD_NAME>, unless you provide an externalName property in the FormField's config.

Form Options

The whenToValidate, autocorrect, and sourceSelector options can be provided in the Form's config. All FormFields will default to these values unless an override is provided.

For sourceSelector you'll need to return the full source in an object like:

export default prefab({
  pizzaOrder: {
    fields: {
      toppings: { },
      size: {
        fieldType: 'number'
      }
    }
  },
  sourceSelector: state => ({ toppings: 'cheese', size: 'M' })
});

localKeys

When preparing the data for the server, all fields will be included in the resulting object. You may use the localKeys option to provide an array of fields to omit. For example: localKeys: [id].

Form Selectors

The Form provides a few selectors that map all of the child selectors:

SelectorInputDescription
statestatereturns the full state of all the form's fields
handlersdispatchreturns all the handlers of all the form's fields
exportstateprepares data for an external service, listing all the current values
isInvalidstateis true if any field is invalid (reguardless of the whenToValidate flag)
isDirtystateis true if any field is dirty
resetActionsthe full list of reset actions that should be triggered to reset the form

hasAnyDirty

There is a utility that let's you see if there are any dirty forms anywhere in the system. This is useful for latching onto browser navigation events and prompt with a "Unsaved Data! Are you sure want to leave?" sort of dialog.

import { hasAnyDirtyForms } from '@airglow/prefab-form';

if (hasAnyDirtyForms(state)) {
  console.alert("Unsaved Data! Are you sure want to leave?");
}
0.8.0

4 years ago

0.7.0

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.2

4 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago