0.2.7 • Published 6 years ago

react-formian v0.2.7

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

An insect-based module for creating controlled web forms for React.

  • Easy to write; easy to read
  • Pre-packed with front-end formatters and validators
  • Auto-injects styles (override without using !important)
  • Easy to customize
  • Mirrors HTML syntax

currently in alpha — use with caution

This package requires that your project uses React

Controlled form elements as React components

See Live Demo

Installation

React Formian is distributed via npm, with styles distributed on UNPKG

npm install react react-formian

Usage

import React from 'react';
import ReactDOM from 'react-dom';
import Formian from 'react-formian';

const submitHandler = function(formData) {
	console.log('hello world', formData)
}

ReactDOM.renderComponent(
  <Formian.Form onSubmit={submitHandler}>
  	<h1>My Form Title</h1>
	<Formian.Name />
	<Formian.Email />
	<Formian.Checkbox
		name="agree"
		labelText="Aren't ants amazing?"
		required={false}
	/>
	<Formian.Submit />
	<p>My legal text</p>
  </Formian.Form>,
  document.getElementById('main')
)

Styles

Styles for Formian elements are dynamically injected. Prevent their injection and write your own, simply by adding customStyles={true} to your Formian.Form element.

You can also pre-require Formain's styles by adding a stylesheet link to your HTML document and pointing the href to https://unpkg.com/react-formian@[your version]/build/style.css, and then setting customStyles to true, as above.

Each container is wrapped in an identical manner, with a div that has a class of input-container as well as the input type and subtype.

Props

Props in brackets ([ ]) are optional

Formian.Form Component API

All form input elements are children of Formian.Form. You can also put anything else you might need in as a child, as Formian.Form only manipulates form inputs.

PropTypeDefaultDescription
onSubmitfunctionundefinedRequired: function that will utilize one argument: formData
classNamestring""Set a CSS class for custom styles
submitOnChangebooleanfalseRuns the onSubmit function after every change, with a two second timeout. Use for auto-saving the form.
customStylesbooleanfalseUsed to prevent Formain's native styles from being injected on the page

Common Child Component API

Formian elements are intended to mirror native HTML elements as closely as possible, just with a lot of useful built-in structure and accessibility properties (e.g. tabindex for mouse-less navigation). The hope is you will be able to trust that Formian has fully extended and utilized HTML's native functionality so these docs may feel (mostly) redundant.

PropTypeDefaultDescription
namestringundefinedRequired: Key name for the formData object (some child types have a default for ease of use but it’s recommended to declare names manually)
classNamestring""Set a CSS class for custom styles
labelTextstringnameLabel or Legend text
requiredbooleantrueSubmit button is disabled until all required fields have a valid input
formatterfunctionundefinedCustom formatter function; manipulates the field value during every onChange event

Formian.Text Component API

Shared by Formian.Email, Formian.Number, Formian.Password, Formian.Tel and Formian.TextArea

PropTypeDefaultDescription
defaultValuestring""Value that the form element should initially render with
placeholderstring""Placeholder text
errorTextstring or false"Please enter a valid input"Invalid input error message text (does not render if false)
validatorfunctionundefinedCustom validator function; returns a boolean that asserts an acceptable input. If required is set to false, the validator will not run

Common fields like Email, Text, Tel, etc ... are designed to be as easy to use as possible and (assuming no customization needs) require nothing besides being called as a child of Formian.Form. However, for the sake of readability, it's advised you always declare a name manually in your JSX, so coders can easily associate an input with its corresponding key in the formData object.

Formian.Checkbox Component API

Shared by Formian.OnOff, a stylized Checkbox

PropTypeDefaultDescription
defaultValuebooleanfalseValue that the form element should initially render with
childrenreact componentsundefinedComponent to be placed inline with the checkbox; supersedes labelText
errorTextstring or false"Please check to agree"Invalid input error message text (if false, does not render)
iconURL string or React Component'\u2714'Custom checkmark icon
tinyIntbooleanfalseField returns 1 or 0 instead of true or false

Formian.Radio Component API

While you are likely used to declaring Radio buttons one-by-one (like a series of Checkboxes), Formian.Radio creates all radio elements together, wrapped in a Fieldset. This means they're declared in your JSX very similarly to a Select, and should tip you off to always ask the question: should this input be a Radio, or a Select?

For lengthier text, options for bigger buttons or a vertical layout is in the works.

PropTypeDefaultDescription
optionsarray of stringsundefinedRequired: Array of values for this key
defaultValueinteger0Value as an index of the options array with which the form element should initially render

Formian.Select Component API

PropTypeDefaultDescription
optionsarray of stringsundefinedRequired: Array of values for this key
defaultValueinteger0Value as an index of the options array with which the form element should initially render
placeholderstring""Placeholder text
errorTextstringPlease select an optionInvalid input error message text

Formian.Range Component API

PropTypeDefaultDescription
defaultValueinteger0Value the slider will initially render on
mininteger0Minimum value for this Range
maxinteger100Maximum value for this Range
stepinteger1Step at which the Range increments or decrements

Formian.Submit Component API

Triggers your onSubmit function. This button is grayed out until all required fields have a valid value, and onSubmit is not called. If Submit is clicked when this is not true, all erroneous form inputs will be highlighted, and display their error text, until they're focused.

PropTypeDefaultDescription
labelTextstring""Text displayed on the button; supersedes value
valuestringSubmitText displayed on the button

Formian.Reset Component API

Resets all your fields to their default state. This button is grayed out and unclickable until the form is not in a default state. This button is a great addition to all kinds of user settings interfaces.

PropTypeDefaultDescription
labelTextstring""Text displayed on the button; supersedes value
valuestringRestore DefaultsText displayed on the button

Coming soon

Formian.Dataset, Formian.Date, rerender optimizations, easily customized colors, animated placeholders, and so much more are on the horizon