Detalhes do pacote

@emico/informed

joepuzzo37ISC2.7.11

A lightweight framework and utility for building powerful forms in React applications

form, react-form, forms, informed

readme (leia-me)

Informed

We have released V2!!!

Go to live examples, code and docs!

Informed-Chat Build Status Coverage Status

Introduction

Say hello to the best react form library you have ever used! Informed is an extensive, simple, and efficient solution for creating basic to complex forms in react. Out of the box you get the ability to grab and manipulate values, validate fields, create custom inputs, and much much more!!!

Oh and YES WE USE HOOKS!!

Getting Started

Install with npm
npm install --save informed

Create a Simple Form

import { Form, Text } from 'informed';

<Form>
  <label>
    First name:
    <Text field="name"/>
  </label>
  <button type="submit">Submit</button>
</Form>;

Create a Simple Form With Validation

import { Form, Text } from 'informed';

const validate = value => {
  return !value || value.length < 5
    ? 'Field must be at least five characters'
    : undefined;
};

<Form>
  <label>
    First name:
    <Text field="name" validate={validate}/>
  </label>
  <button type="submit">Submit</button>
</Form>;

Create a Complex Form

import { Form, Text, Option, Select, Checkbox, Scope, TextArea, RadioGroup, Radio } from 'informed';

<Form>
  <label>First name:<Text field="name"/></label>
  <Scope scope="favorite">
    <label>Favorite color:<Text field="color"/></label>
    <label>Favorite food:<Text field="food"/></label>
  </Scope>
  <label>Friend 1:<Text field="friends[0]" /></label>
  <label>Friend 2:<Text field="friends[1]" /></label>
  <label>Friend 3:<Text field="friends[2]" /></label>
  <label>Bio:<TextArea field="bio"/></label>
  <RadioGroup field="gender">
    <label>Male <Radio value="male"/></label>            
    <label>Female <Radio value="female"/></label>
  </RadioGroup>
  <label>
    Relationship status:
    <Select field="status">
      <Option value="" disabled>
        Select One...
      </Option>
      <Option value="single">Single</Option>
      <Option value="relationship">Relationship</Option>
      <Option value="complicated">Complicated</Option>
    </Select>
  </label>
  <label>
    Colors:
    <Select
      field="colors"
      multiple
    >
      <Option value="red">Red</Option>
      <Option value="green">Green</Option>
      <Option value="blue">Blue</Option>
      <Option value="yellow">Yellow</Option>
      <Option value="orange">Orange</Option>
      <Option value="purple">Purple</Option>
    </Select>
  </label>
  <label>Authorize: <Checkbox field="authorize"/></label>
  <button type="submit">Submit</button>
</Form>

Access Form State with Hooks!

import { Form, Text, useFormState } from 'informed';

const ComponentUsingFormState = () => {
  const formState = useFormState();
  return (
    <pre>
      <code>{JSON.stringify(formState, null, 2)}</code>
    </pre>
  );
};

<Form>
  <label>Name:<Text field="name" /></label>
  <button type="submit">Submit</button>
  <h5>Component using formState:</h5>
  <ComponentUsingFormState />
</Form>

Control Form via FormApi through the use of Hooks!!

import { Form, Text, useFormApi } from 'informed';

const ComponentUsingFormApi = () => {
  const formApi = useFormApi();
  return (
    <button type="button" onClick={()=>
      formApi.setValue(
        'name', 
        Math.floor(Math.random() * Math.floor(Number.MAX_SAFE_INTEGER)))}>
      Random
    </button>
  );
};

<Form>
  <div>
    <label>Name:<Text field="name"/></label>
    <button type="submit">Submit</button>
    <h5>Component using formApi:</h5>
    <ComponentUsingFormApi />
  </div>
</Form>

Create custom inputs with built in validation!!

import { Form, BasicText, asField } from 'informed';

const validate = value => {
  return !value || value.length < 5
    ? 'Field must be at least five characters'
    : undefined;
};

const ErrorText = asField(({ fieldState, ...props }) => (
  <React.Fragment>
    <BasicText
      fieldState={fieldState}
      {...props}
      style={fieldState.error ? { border: 'solid 1px red' } : null}
    />
    {fieldState.error ? (
      <small style={{ color: 'red' }}>{fieldState.error}</small>
    ) : null}
  </React.Fragment>
));

<Form>
  <label>
    First name:
    <ErrorText
      field="name"
      validate={validate}
      validateOnChange
      validateOnBlur
    />
  </label>
  <button type="submit">Submit</button>
</Form>;

Create dynamic forms

import { Form, Text, RadioGroup, Radio } from 'informed';

<Form>
  {({ formState }) => (
    <React.Fragment>
      <label>First name:<Text field="name"/></label>
      <label>Are you married?</label>
      <RadioGroup field="married">
        <label>Yes <Radio value="yes"/></label>
        <label>No <Radio value="no"/></label>
      </RadioGroup>
      {formState.values.married === 'yes' ? (
        <label >Spouse name:<Text field="spouse" /></label>
      ) : null}
      <button type="submit">Submit</button>
    </React.Fragment>
  )}
</Form>

WARNING: writing this in the above way is fine, it works they way we expect and gets us what we need... BUT! There is a better way!

import { Form, Text, RadioGroup, Radio, useFieldState } from 'informed';

const Spouse = () => {
  const { value: married } = useFieldState('married'); 
  return married === 'yes' ? <label >Spouse name:<Text field="spouse" /></label> : null;
};

<Form>
  <label>First name:<Text field="name"/></label>
  <label>Are you married?</label>
  <RadioGroup field="married">
    <label>Yes <Radio value="yes"/></label>
    <label>No <Radio value="no"/></label>
  </RadioGroup>
  <Spouse />
  <button type="submit">Submit</button>  
</Form>

Writing code the second way can typically save excess rendering! And, it looks much cleaner.

Create dynamic forms with dynamic arrays !!! Mind Blown!!!

```jsx import { Form, Text, ArrayField } from 'informed';

const DynamicArrays = () => {

return (

<Form> <ArrayField field="sibling"> {({ add, fields }) => ( <> <button onClick={add} type="button"> Add Sibling </button> {fields.map(({ field, key, remove }, i) => ( <label htmlFor={i} key={key}> Sibling {i}: <Text field={field} id={i} /> <button type="button" onClick={remove}> Remove </button> </label> ))} </> )} </ArrayField> <button type="submit">Submit</button> <FormState /> </Form>
); };

changelog (log de mudanças)

2.7.8 (May 31, 2019)

Fixed

  • issue where initial values were not being formatted

2.7.7 (May 30, 2019)

Fixed

  • issue where initial values were not being masked

2.7.6 (May 29, 2019)

Updated

  • Types file to support validate on formApi

2.7.5 (May 29, 2019)

Updated

  • Types file to support any type on form errors

2.7.4 (May 23, 2019)

Fixed

  • documentation for creating custom inputs

Added

  • the ability to pass your own ref to inputs

2.7.3 (May 22, 2019)

Fixed

  • issue with validation triggering when keep state and validate on blur

2.7.2 (May 21, 2019)

Added

  • made maskWithCursorOffset optional in types

2.7.1 (May 21, 2019)

Added

  • maskWithCursorOffset to the type defs

2.7.0 (May 21, 2019)

Added

  • maskWithCursorOffset prop to inputs

2.6.15 (May 20, 2019)

Added

  • maintainCursor to types

2.6.14 (May 16, 2019)

Added

  • render and userProps to field context types

2.6.13 (May 16, 2019)

Added

  • maskedValue to types

2.6.12 (May 14, 2019)

Added

  • getters to useFields field api

2.6.11 (May 11, 2019)

Added

  • useField hook to the docs

Updated

  • the interface for useField hook

2.6.10 (May 10, 2019)

Fixed

  • issue where initialization code in useform was in the effect and not in constructor

2.6.9 (May 10, 2019)

Fixed

  • attempting to fix issue that I think is caused by useEffect in useForm hook

2.6.8 (May 10, 2019)

Fixed

  • issues cause by using useMemo instead of useState for initial render stuff

2.6.7 (May 9, 2019)

Added

  • exists function to the field api types

2.6.6 (May 9, 2019)

Added

  • exists function to the field api so you can check to see if that field exists

2.6.5 (May 9, 2019)

Fixed

  • issue where inital render of useFieldApi would fail when field was not registered yet

2.6.4 (May 9, 2019)

Updated

  • useForm hook so that the event handlers can change

2.6.3 (May 7, 2019)

Removed

  • debug as a dependency and added my own :)

2.6.2 (May 3, 2019)

Fixed

  • issue with default register context missin getField function

2.6.1 (May 3, 2019)

Fixed

  • issue with useFieldApi hook and withFieldApi HOC where reset and validate were not there

Added

  • a few more tests to increase test coverage!!!! wooo

2.6.0 (May 2, 2019)

Added

  • useForm hook!!! and FormProvider component!!!

2.5.0 (April 30, 2019)

Updated

  • useField hook to useEffect instead of useLayoutEffect

2.4.0 (April 30, 2019)

Added

  • preventEnter prop to the form so users can prevent enter key form submission

2.3.2 (April 22, 2019)

Updated

  • Issue with dynamic arrays and initial values

2.3.1 (April 12, 2019)

Updated

  • Babel build

2.3.0 (April 3, 2019)

Added

  • allowEmtyStrings prop to the form
  • allowEmtyString prop to inputs

Fixed

  • issue where setValues would not allow empty strings

2.2.0 (March 29, 2019)

Added

  • setFormError function to the form api
  • validate function to the form api

2.1.15 (March 28, 2019)

Fixed

  • Issue with setValues missing from the default context

2.1.14 (March 27, 2019)

Fixed

  • Issue where reset would call validation

2.1.13 (March 18, 2019)

Updated

  • Allowing for optional generic on FormValue

2.1.12 (March 13, 2019)

Updated

  • ref to be any type in typings

2.1.11 (March 12, 2019)

Fixed

  • issue in typings for ref field on FieldContext

2.1.10 (March 12, 2019)

Fixed

  • issue when using a field NOT in the context of a form

2.1.9 (March 8, 2019)

Fixed

  • another issue where initial values did not work when keep state was passed

2.1.8 (March 8, 2019)

Fixed

  • issue where initial values did not work when keep state was passed

2.1.7 (March 3, 2019)

Fixed

  • issue where initial values did not work for ArrayFields

2.1.6 (Feb 21, 2019)

Updated

  • typing files for type script users

2.1.5 (Feb 19, 2019)

Fixed

  • Issue with text area input not setting typed value

2.1.4 (Feb 19, 2019)

Added

  • maskOnBlur prop to inputs

2.1.3 (Feb 14, 2019)

Added

  • fieldExists api function to check if field exists

2.1.2 (Feb 14, 2019)

Fixed

  • Issue where form that is submitted through enter key would try to prevent default

2.1.1 (Feb 14, 2019)

Fixed

  • Issue where form values would not get passed to validation function when touched

2.1.0 (Feb 14, 2019)

Updated

  • Format and parse to set maskedValue instead of value

2.0.5 (Feb 14, 2019)

Fixed

  • Issue where you could NOT set 0 null or false as initial values because they are falsey

2.0.4 (Feb 13, 2019)

Added

  • validateFields function to the form!!!

2.0.3 (Feb 13, 2019)

Fixed

  • Issue where reset would not work for scoped fields

Added

  • Set Values to the form api!!!

2.0.2 (Feb 13, 2019)

Fixed

  • Issue where ArrayField was prefixing all fields with 'field'
  • Issue where when input fields changed input did not rerender.

2.0.1 (Feb 10, 2019)

Fixed

  • Issue where render and component props were getting passed to the dom form

2.0.0 (Feb 7, 2019)

Added

  • useFieldApi
  • useFieldState
  • useFormApi
  • useFormState
  • useField
  • format
  • parse
  • maintianCursor ( fixes issue where cursor jumps to end on mask )
  • ArrayField ( Check out the docs! this is sick! )
  • Form Level validation ( function that can invalidate the form as a whole )
  • debug prop that allows you to visually view the rendering!

Changed

  • the field "siblings.1" now resolves to values.siblings[1], it used to resolve to values.siblings.1
  • the field "siblings['2']" now resolves to values.siblings[2], it used to resolve to values.siblings.2
  • withFormApi will no longer trigger a rerender if the fomrs state changes. This is a great optimization for those who want to modify but dont care about the form state!
  • the validate prop now expects the validation function to return undefined if there is no error. Any other returned value (including falsey null, 0, etc will be treated as an error for the field.

Removed

  • The Field Component
  • fieldExists not needed
  • setState will maybe add later but its complex and out of scope ATM
  • setValues will maybe add later but its complex and out of scope ATM ( as of V 2.0.3 its back! )
  • preSubmit was never needed.. developers can do this themselves
  • Async Validation. Async validation led to many issues that overcomplicated informed. We determined this is something that the developer could achive on there own for now but we may look into adding this in the future.

1.10.12 ( December 4, 2018 )

Updated

  • typescript definition file

1.10.11 & 1.10.10 I ran version patch twice lol ( December 4, 2018 )

Updated

  • Select forward ref

1.10.9 ( November 16, 2018 )

Updated

  • files in package json to include typeigs

1.10.8 ( November 8, 2018 )

Added

  • type file for typescript

1.10.7 ( October 11, 2018 )

Updated

  • Name of withFormSate

1.10.6 ( October 2, 2018 )

Fixed

  • Skipped test for select!!!

Updated

  • Select to use new ref interface

1.10.5 ( September 14, 2018 )

Added

  • License

1.10.4 ( August 13, 2018 )

Fixed

  • Issue with @babel/runtime was the dep when it should have ben @babel/runtime-corejs2

1.10.3 ( August 13, 2018 )

Fixed

  • Issue with @babel/runtime for real this time

1.10.2 ( August 13, 2018 )

Fixed

  • Issue with @babel/runtime

1.10.1 ( August 13, 2018 )

Removed

  • mistakenly added dev deps that were deps... oops

1.10.0 ( August 13, 2018 )

Added

  • submits to the form state

1.9.0 ( August 8, 2018 )

Rebuilt

  • Added prettier so all the files have been changed... so i rebuilt to have source maps match code

1.8.1 ( August 1, 2018 )

Rebuilt

  • Attempting to simply rebuild lib due to possible build issue

1.8.0 ( August 1, 2018 )

Updated

  • initialValue to get exposed as prop to custom fields

1.7.5 ( July 31, 2018 )

Added

  • Source maps

1.7.4 ( July 26, 2018 )

Fixed

  • Issue where Basic radio group was not getting exported

1.7.3 ( July 23, 2018 )

Fixed

  • issue where bind to field did not pass down the field prop.

1.7.2 ( July 20, 2018 )

Fixed

  • Issue where element wont get removed from array when deregistering field... this is used when dynamically removing value

1.7.1 ( July 20, 2018 )

Added

  • Field prop is now exposed to field elements and default inputs pass field as name to html inputs

1.7.0 ( July 18, 2018 )

Added

  • fieldExists method to the formApi

1.6.0 ( July 16, 2018 )

Added

  • onValueChange prop to inputs so you can tie into when values change!!

1.5.2 ( July 13, 2018 )

Fixed

  • Issue where prop changes to fields would not get recognized

1.5.1 ( July 13, 2018 )

Fixed

  • issue where i forgott to add @babel/runtime as dependency

1.5.0 ( July 12, 2018 )

Added

  • asyncValidation prop to inputs
  • asyncValidateOnBlur prop to inputs

1.4.0 ( July 5, 2018 )

Added

  • Basic input fields so users can more easily create custom inputs
  • Docs for creating custom inputs

1.3.11 ( July 9, 2018 )

Fixed

  • issue where you could not nest scope

1.3.10 ( July 5, 2018 )

Fixed

  • issue where you could not pass initialValue=false to checkbox

1.3.9 ( July 5, 2018 )

Fixed

  • issue where mutable values were getting passed to onSubmit and getState

1.3.8 ( July 2, 2018 )

Fixed

  • issue where path array was being build every get and set

1.3.7 ( July 2, 2018 )

Fixed

  • issue where onChange was getting passed to internal form element

1.3.6 ( June 28, 2018 )

Added

  • globalObject: 'this' to the webpack dist config to support SSR

1.3.5 ( June 28, 2018 )

Fixed

  • Issue with event emitter limit ( need to look into alternative solution )
  • Removed depricated sandbox sinon usage that was causing errors during tests

1.3.3 ( June 28, 2018 )

Updated

  • Webpack dist configuration to keep class names

1.3.2 ( June 28, 2018 )

Fixed

  • Issue were initialValue was getting passed all the way down to html input
  • Issue where form would not rerender when field was registered

1.3.1 ( June 28, 2018 )

Fixed

  • Issue were validateOnMount was getting passed all the way down to html input

1.3.0 ( June 28, 2018 )

Added

  • mask so you can mask values at field level. example value => value + '!!!'

1.2.1 ( June 27, 2018 )

Added

  • hook so you can add a button with type=reset and it will reset the form

1.2.0 ( June 27, 2018 )

Added

  • validateOnMount to input props

1.1.2 ( June 25, 2018 )

Moved

  • React and React-Dom to dev dependencies

1.1.1 ( June 18, 2018 )

Fixed

  • Bug where i did not do null check on event within on submit

1.0.1 ( June 15, 2018 )

Added

  • Notify prop to inputs that allows you to notify other fields when your error state changes ( see docs )

1.0.0 (June 12, 2018)

Added

  • Text
  • TextArea
  • Radio Group
  • Radio
  • Select
  • Select as Multiselect !!!
  • Checkbox
  • withRadioGroup
  • withFieldApi
  • withFieldState
  • withFormApi
  • withFormState
  • asField
  • Form
  • Field

Changed

Note: this was the first release but i wanted to include changes from react-form so here they are:

  • formApi was split into two parts formApi ( contains just functions ) formState ( contains just form state )
  • Form level validation is gone. You do all validation via field validation.
  • defaultValues Form prop is now called initialValues
  • onChange Form prop only recieves the formState. It used to retrieve the form Api as well.
  • preventDefault Form prop is now dontPreventDefault
  • getApi Form prop just returns the formApi, not the state and the api.
  • Form component now renders the form element internally. So you dont have to "hook it up" anymore!!

Removed

Note: this was the first release but i wanted to include things that were removed from react-form so here they are:

  • NestedField you can use Scope instead but all it does is scope internal fields to scope="your-scope"
  • validateOnSubmit was removed. Now the form always validates on submit by default and you can opt into sooner validation at field level.
  • defaultValues form prop is now called initialValues
  • pure Form prop. Its not needed anymore due to the use of React.PureComponent internally.
  • add, remove, and swap values. The developer can achive this on there own without the use of internal functionality.
  • Array Syntax. In order to keep things simple we now only support the string syntax for field names.
  • Async Validation. Async validation led to many issues that overcomplicated react-form. We determined this is something that the developer could achive on there own for now but we may look into adding this in the future.
  • Warning and Success have been removed for now to keep lib lean but we may add additional functions in the future.