1.0.10 • Published 4 years ago

react-free-forms v1.0.10

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

REACT FREE FORMS 🌊🔥🔥🔥

React component that will make your forms quick and simple

Overview

React free forms is a React library that will help you implement form into your project easy and quick way. It has no DOM structure restrictions so you can create any form you like.

Suppoted input types:

  • text
  • number
  • password
  • select
  • radio
  • checkbox
  • email
  • tel

Instalation

npm i react-free-forms

How to use

First you have to import Form and Input Components.

import { Form, Input } from 'react-free-forms'

In all cases Form component should be wrapping all Input components you would like to be validated together. Basic structure looks like this:

<Form>
    <Input />
</Form>

Form component

Gathers all Input components and validates them by given rules. All props it can receive are optional.

Prop NameProp TypeDescription
onSubmitFunctionTriggers upon successfully validated form with a result as a first argument
onFailureFunctionTriggers upon failed validation while submitting a form
onFocusFunctionTriggers when any form input is focused
onBlurFunctionTriggers when any form input is blurred
requiredErrorTextStringPassed as an error text for any required input that has no value
classNameStringAdditional class for <form> tag rendered within this component
idStringAdditional id for <form> tag rendered within this component
disabledBooleanDetermines whether whole form should be disabled
recaptchaStringReceives ReCaptcha sitekey as its value. If present it will protect form submitting with ReCaptcha service and pass a ReCaptcha token in its result
recaptchaInfoRefRefThis element will have its content replaced with obligatory information about recaptcha protection

Input component

Renders <input> tag and decides about validation rules. Should be inserted inside <Form></Form> component. Only two props are required, others are optional.

Prop NameProp TypeDescription
requiredBooleanRequired - tells whether input value is required
nameStringRequired - it will be a key of that input value in result object
idStringCustom id for <input> tag
typeStringSupported types
ruleStringInput Rules
inputOptionsArrayArray of options for radio and select inputs
inputClassStringCustom class for <input> tag
errorClassStringCustom class for tag holding error
labelClassStringCustom class for <label> tag
placeholderStringPlaceholder for input
maxNumberMaximum value length (only for string values)
minNumberMinimum value length (only for string values)
readonlyBooleanReadonly attribute
disabledBooleanDisable attribute
onBlurFunctionTriggers when input is focused blurred
onFocusFunctionTriggers when input is focused focused
onBeforeChangeFunctionTriggers when input value is about to be changed
onAfterChangeFunctionTriggers when input value has been changed
titleStringTitle block value above the input
errorTextStringCustom error text when validation has failed
groupStringAssigns this input value into given group inside result object
minErrorTextStringCustom error text when value is too short
requirederrortextStringCustom error text for submitted required input with no value
customRegexStringCustom regex for validating input value
defaultValueString/NumberInput initial value

Input rules

Rules define the way input should be validated. Most rules are self explanatory. For custom rule use customRegex prop.

  • text - any string
  • number - any number
  • select - any truhy
  • radio - any truhy
  • checkbox - checked
  • email
  • zip-code
  • phone
  • email
  • password - default password rule checks for:
    • a digit
    • a lowercase letter
    • an uppercase letter
    • at least 6 characters in total

Examples

Basic input example:

    <Form onSubmit={this.submitForm}>
        <Input name='text' required={true} />
    </Form>

Will render:

    <form>
        <label class="rff-inputComponent error" style="position: relative;">
            <input name="text" class="rff-inputComponent__input" value="">
            <p class="rff-inputComponent__error">input is required</p>
        </label>
    </form>

Radio and Select:

These types requires inputOptions prop that receives an array (or custom prop.children structure).

    <Form onSubmit={this.submitForm}>
        <Input name='radio' required={true} type='radio' inputOptions={[['test1', 'value1'], 'test2']} />
    </Form>

Will render:

<form>
    <label class="rff-inputComponent rff-inputComponent--radio" style="position: relative;">
        <div value="">
            <label>
                <input name="radio" type="radio" class="rff-inputComponent__input" value="value1">
                <span>test1</span>
            </label>
            <label>
                <input name="radio" type="radio" class="rff-inputComponent__input" value="test2">
                <span>test2</span>
            </label>
        </div>
    </label>
</form>

Custom Input children structure 😻

You are free to create your own structure inside Input component. When using Input component as a wrapper you can include only one <input>/<select> tag inside (or one set of radio inputs)

<Form onSubmit={this.submitForm}>
    <Input required={true} name='select' defaultValue='test2'>
        <select defaultValue='test2'>
            <option value='test'>test</option>
            <option value='test2'>test2</option>
        </select>
    </Input>
    <Input required={false} name='text'>
        <p>Input can even be wrapped another div</p>
        <div>
            <input type="text" />
        </div>
    </Input>
    <div>
        <div>
            <Input required={true} name='nested_input_component' />
        </div>
    </div>
    <button type='submit'>submit</button>
</Form>
1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago