2.0.2-beta • Published 5 years ago

react-plain-form v2.0.2-beta

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

React plain form

React simple form for everyday usage 😜

Demo

Example

import React from 'react';
import { render } from 'react-dom';
import useForm from 'react-plain-form';

function Form({ schema }) {
    const {
        fields,
        values,
        isValidating,
        errors
    } = useForm(schema);
    const isErrors = !!Object.keys(errors).length;
    const handleSubmit = e => {
        e && e.preventDefault();
        console.log(values);
    };

    return (
        <form onSubmit={handleSubmit}>
            <label>
                Name
                <input {...fields.name} />
                { errors.name && <span>{errors.name}</span> }
            </label>
            <br/>
            <button
                type="submit"
                disabled={isValidating || isErrors}
            >
                Submit
            </button>
        </form>
    );
}

const formSchema = {
    name: {
        type: 'text',
        onValidate: values => new Promise((res, rej) =>
            values.name.length
                ? res()
                : rej(new Error('Name required'))
        )
    }
};

render(
    <Form schema={formSchema} />,
    document.querySelector('#root')
);

API

useForm({ schema })

NameTypeDefaultsDescription
schemaObjectundefinedConfiguration object for you form
schema[key]Stringundefinedname prop for future input field
schema[value].[any]ObjectundefinedAny valid html5 attributes
schema[value].onValidateFunctionundefinedvalidation function. Get values as argument and should return Promise.
schema[value].validateOnString or ArraychangeValidation run on this events. Variants: change focus blur
schema[value].defaultValueString''Default value

const {...} = useForm({ schema })

NameTypeDescription
fieldsObjectSame as schema above, but enriched with aditions methods like onChange etc. To make input controllable.
valuesObjectKey, value pairs with name of your input and it current value.
errorsObjectKey, value pairs with name of your input and it current value.
isValidatingBooleanIndicator, Does form is in validating state right now or not.
setErrorFunctionSet error for field. setError(name, value).
setValueFunctionSet value for field. setValue(name, value).
validateAllFunctionRun all onValidate concurrently. Returns promise. Usefull to run validation on submit
updateFieldsFunctionWant to add fields dynamically, pass as argument to this function new created or extended schema
2.0.2-beta

5 years ago

2.0.0-beta

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

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