2.0.21 • Published 5 years ago

react-smart-form v2.0.21

Weekly downloads
80
License
MIT
Repository
github
Last release
5 years ago

react-smart-form

A form component that containes all the functionality you will need.


Installation

Examples

Documentation


Installation

Install with yarn or npm

yarn add react-smart-form
        #or
npm install react-smart-form --save

Examples

Basic

import React,{ Component } from 'react';
import { render } from 'react-dom';
import {Form, Input, Submit} from 'react-smart-form';

class MyComponent extends Component {
    onSubmit=(values)=>{
           console.log(`Username: ${values.username}`);
           console.log(`Password: ${values.password}`);
    }
    render() {
        return (
            <Form onSubmit={this.onSubmit}>
                <Input name="username" label="Username" />
                <Input name="password" label="Password" type="password" />
                <Submit>
                    Login
                </Submit>
            </Form>
        );
    }
}

render(
    <MyComponent/>,
    document.getElementById('app'),
);

Validation

import React,{ Component } from 'react';
import { render } from 'react-dom';
import {Form, Input, Submit} from 'react-smart-form';
import {required, length, email} from 'react-smart-form/validators';


/* 
* the react-smart-form asks if the field has error 
* so you must return false if it doesn't 
* or true (or a string message) if it does
*/
const numberCustomValidator = message=>(value)=>{
    if(typeof value !== 'number'){
        return message || true;
    }
    return false;
}

class MyComponent extends Component {
    onSubmit=(values)=>{
           console.log(`Username: ${values.username}`);
           console.log(`Password: ${values.password}`);
    }
    render() {
        return (
            <Form onSubmit={this.onSubmit}>
                <Input 
                    name="username" 
                    label="Username" 
                    validators={[required('Email is required'),email('This is not correct email.')]}
                />
                <Input 
                    name="password" 
                    label="Password" 
                    type="password" 
                    // silent validation
                    validators={[required(),numberCustomValidator('This must be a number')]}

                    />
                <Submit>
                    Login
                </Submit>
            </Form>
        );
    }
}

render(
    <MyComponent/>,
    document.getElementById('app'),
);

Handle custom errors and loading state

import React,{ Component } from 'react';
import { render } from 'react-dom';
import {Form, Input, Submit} from 'react-smart-form';

const fakeRequest = ()=>{
  return new Promise((resolve)=>{
      setTimeout(resolve,1000);
  })  
};

class MyComponent extends Component {
    onSubmit=(values)=>{
           console.log(`Username: ${values.username}`);
           console.log(`Password: ${values.password}`);
           // Returning a promise react-smart-form can handle loading state of the form.
           return fakeRequest().then((resp)=>{
               if(!resp.data.success){
                   throw {
                       username:'This username doesn\'t exist.'
                   }    
               }
               
           });
    }
    render() {
        return (
            <Form onSubmit={this.onSubmit}>
                <Input name="username" label="Username" />
                <Input name="password" label="Password" type="password" />
                <Submit>
                    Login
                </Submit>
            </Form>
        );
    }
}

render(
    <MyComponent/>,
    document.getElementById('app'),
);

Custom input custom errors and loading state

import React,{ Component } from 'react';
import { render } from 'react-dom';
import {smartForm, smartInput, withFormState} from 'react-smart-form';


let CustomForm = (props)=>{
    const {
        smartForm, // include loading(bool):the loading state of the form, disabled(bool):the disabled state of the form
        children,
        ...formPros
    } = props;
    return (
        <div>
            <form {...formPros}>
            {children}
            </form>
        </div>
  )};

CustomForm=smartForm()(CustomForm); 

let CustomField = (props)=>{
    const {
        smartForm, // include error(string || bool): the error of the field
        label,
        ...inputProps
    }=props;
    return (
        <div>
             <label>Custom field: {label}</label>
             <input {...inputProps} />
             <div>{smartForm.error}</div>
        </div>
  )};

CustomField=smartInput()(CustomField);


let CustomButton = (props)=>{
    const {
        smartForm, // include loading(bool):the loading state of the form, disabled(bool):the disabled state of the form
        ...buttonProps
    } = props;
    return (
        <div>
            <button {...buttonProps} disabled={smartForm.disabled}>
            {smartForm.loading ? 'loading...':children}
            </button>
        </div>
  )};

CustomButton=withFormState(CustomButton);

class MyComponent extends Component {
    render() {
        return (
            <CustomForm onSubmit={this.onSubmit}>
                <CustomField name="email" label="Email" validators={[required('This is required')]}/>
                <CustomButton>Press</CustomButton>
            </CustomForm>
        );
    }
}

render(
    <MyComponent/>,
    document.getElementById('app'),
);

withValidator only

import React,{ Component } from 'react';
import { render } from 'react-dom';
import {withValidator} from 'react-smart-form';
import {required} from 'react-smart-form/validators';

let CustomField = (props)=>{
    const {
        error
        label,
        ...inputProps
    }
    return (
        <div>
             <label>Custom field: {label}</label>
             <input {...inputProps} />
             <div>{error}</div>
        </div>
  )};

CustomField=withValidator(CustomField);

class MyComponent extends Component {
    render() {
        return (
            <form>
                 <CustomField name="email" label="Email" validators={[required('This is required')]}/>
            </form>
        );
    }
}

render(
    <MyComponent/>,
    document.getElementById('app'),
);

Reset form

import React,{ Component } from 'react';
import { render } from 'react-dom';
import {Form, Input, Submit} from 'react-smart-form';

class MyComponent extends Component {
    render() {
        return (
            <div>
                <Form formRef={(form)=>{ this.form=form; }}>
                    <Input name="username"/>
                </Form>
                <button onClick={()=>{this.form.reset('username')}}>Reset Username</button>
                <button onClick={()=>{this.form.reset()}}>Reset All</button>
            </div>
        );
    }
}

render(
    <MyComponent/>,
    document.getElementById('app'),
);

Match password

import React,{ Component } from 'react';
import { render } from 'react-dom';
import {Form, Input, Submit} from 'react-smart-form';
import {required} from 'react-smart-form/validators';


class MyComponent extends Component {
    onSubmit=(values)=>{
           console.log(`Username: ${values.username}`);
           console.log(`Password: ${values.password}`);
    }
    matchPasswordValidator = message=>(value)=>{
        if(value !== this.form.getValues('password')){
            return message || true;
        }
        return false;
    }
    render() {
        return (
            <Form 
            formRef={(form)=>{this.form=form}}
            onSubmit={this.onSubmit}
            >
                <Input 
                    showPassword={false}
                    name="password" 
                    label="Password" 
                    type="password" 
                    // silent validation
                    validators={[required()]}
                    />
                <Input 
                    showPassword={false}
                    name="repassword" 
                    label="Re enter password" 
                    type="password" 
                    validators={[required(),this.matchPasswordValidator('Password does not match')]}
                    />
                    
                <Submit>
                    Register
                </Submit>
            </Form>
        );
    }
}

render(
    <MyComponent/>,
    document.getElementById('app'),
);

Documentation

Form
propTyperequireddefaultdescription
onValidate(func(hasError))no-A function that runs every time the overall validation status change
onSubmit(func(FieldValues))no-the function that handles the form submission
onChange(func(FieldValues))no-A function that runs every time a field changes
disabled(bool)no-set the form in a disabled state
loading(bool)no-set the form in a loading state
formRef(func)no-returns the Form instance. With form instance you can user reset, setErrors and setValue functions
Input
propTyperequireddefaultdescription
type(input type or 'textarea')no-The type of the Input. Takes all the valid Input type except file and submit.
name(string))yes-The name of the input
label(string)no-The value of the label
focusedLabel(string)no-the value of the label when it is focused
icon(react Component)no-sets and icon in the right side of the input
validators(func(value))no-An array of functions that validates the inputs value. Should return false if the validation pass and string or true if it dont
onChange(func(value))no-A function that runs every time the field changes
focused(bool)no-initialize the input in focus state
showPassword(bool)true-if the type of the field is password appends a icon button that toggles the input display state
defaultValue(string)no-sets a default value for the field
inputRef(func)no-returns the input reference
debounce(number)no300set the input's debounce for validation
disabled:readOnly (bool)no-set the input in a readOnly state
Submit
propTyperequireddefaultdescription
size(number)no18-sets size of the button
color(string)no'#44A8FF'set the button color
loading(bool)no-set the button in a loading state
disabled(bool)no-set the form in a disabled state
children(string)no-The button label
Validators
namedescription
email(errorMessage)checks if the input value is valid email
length(errorMessage,{min:number,max:number})validates the value length
required(errorMessage)checks if a has been set
Form instance function
namedescription
reset('fieldName')reset the specified field. If no field name, all fields will be reset.
getValues({inputName:value})Returns the requested value. If no input name an object with all values will be returned
setValues({inputName:value})Sets values to the specified fields
setErrors({inputName:errorMessage})Sets errors to the specified fields
3.0.0-beta.1

5 years ago

3.0.0

5 years ago

3.8.3-beta

5 years ago

3.8.2-beta

5 years ago

3.8.1-beta

5 years ago

3.8.0-beta

5 years ago

3.7.9-beta

5 years ago

3.7.8-beta

5 years ago

3.7.7-beta

5 years ago

3.7.5-beta

5 years ago

3.7.4-beta

5 years ago

3.7.3-beta

5 years ago

3.7.2-beta

5 years ago

3.7.1-beta

5 years ago

3.7.0-beta

5 years ago

3.6.0-beta

5 years ago

3.5.0-beta

5 years ago

3.4.0-beta

5 years ago

3.3.0-beta

5 years ago

3.2.0-beta

5 years ago

3.1.7-beta

5 years ago

3.1.6-beta

5 years ago

3.1.5-beta

5 years ago

3.1.4-beta

5 years ago

3.1.3-beta

5 years ago

3.1.2-beta

5 years ago

3.1.1-beta

5 years ago

3.1.0-beta

5 years ago

3.0.22-beta

5 years ago

3.0.21-beta

5 years ago

3.0.20-beta

5 years ago

3.0.19-beta

5 years ago

3.0.18-beta

5 years ago

3.0.17-beta

5 years ago

3.0.16-beta

5 years ago

3.0.15-beta

5 years ago

3.0.14-beta

5 years ago

3.0.13-beta

5 years ago

3.0.12-beta

5 years ago

3.0.11-beta

5 years ago

3.0.10-beta

5 years ago

3.0.9-beta

5 years ago

3.0.8-beta

5 years ago

3.0.7-beta

5 years ago

3.0.6-beta

5 years ago

3.0.5-beta

5 years ago

3.0.4-beta

5 years ago

3.0.3-beta

5 years ago

3.0.2-beta

5 years ago

3.0.1-beta

5 years ago

3.0.0-beta

5 years ago

2.0.21

6 years ago

2.0.19

6 years ago

2.0.17

6 years ago

2.0.15

6 years ago

2.0.15-beta

6 years ago

2.0.14-beta

6 years ago

2.0.13-beta

6 years ago

2.0.12-beta

6 years ago

2.0.11-beta

6 years ago

2.0.10-beta

6 years ago

2.0.9-beta

6 years ago

2.0.8-beta

6 years ago

2.0.7-beta

6 years ago

2.0.6-beta

6 years ago

2.0.5-beta

6 years ago

2.0.4-beta

6 years ago

2.0.3-beta

6 years ago

2.0.2-beta

6 years ago

2.0.1-beta

6 years ago

2.0.0-beta

6 years ago

2.0.0

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.0

6 years ago