1.3.5 • Published 8 days ago

@codegateinc/react-form-builder-v2 v1.3.5

Weekly downloads
-
License
MIT
Repository
github
Last release
8 days ago

React-form-builder-v2 is a library that allows you to create highly customizable forms by rendering your own components and storing the state. It works perfectly with ReactJS, React Native, and monorepo with React Native Web.

Create your own components and simply pass callbacks, errors, and values. You can store any type of value in the useField. It can be a string, boolean, number, array, or even an object.

Features

  • Fully working on hooks
  • Relies only on react and ramda
  • Highly customizable components
  • Well typed (Typescript)

Live demo

Install

yarn add @codegateinc/react-form-builder-v2 or npm install --save @codegateinc/react-form-builder-v2

Hooks

useField

type ValidationRule<T> = {
    errorMessage: string,
    validate(value: T): boolean
}

useField(config)

useField config

PropertyTypeDescription
keystringDefault: undefined. This field is required, must be a unique within whole form.
labelstringDefault: undefined. The label value that will be passed through the field.
initialValuegeneric (T)Default: undefined. This field is required. This will define the initial value of the field.
isRequiredbooleanDefault: false. Defines if the field is required and should be mandatory. With validation rules it can be additionally validated.
placeholderstringDefault: undefined. Placeholder value that will be passed via the field
validateOnBlurbooleanDefault: false. Defines if the field should be validated when blurred. If false, validation will be checked on each change.
validationRulesArray<ValidationRule<T>>Default: undefined. Array of validation objects that will define if the field is valid
liveParser(value: T) => TDefault: undefined. Function that, if defined, will be invoked every time the value changes.
submitParser(value: T) => TDefault: undefined. Function that, if defined, will be invoked after the submit function is invoked.
hasErrorbooleanDefault: false. This value indicates if the field has an error.

validationRules example

validationRule: [
    {
        errorMessage: 'this field should be at least 2 chars',
        validate: (value: string) => value.length >= 2
    }
]

useForm

const statesAndFunctions = useForm(config, callbacks)

States and functions

PropertyTypeDescription
formRecord<string, T>Form with all values.
hasErrorbooleanIndicates if the form has an error.
isFilledbooleanDetermines whether or not all fields have changed.
formHasChanges() => booleanReturns a boolean indicating whether any fields have changed.
setError(key: string, errorMessage: string) => voidSets an error to a field, even if the field is valid. After revalidation, this error disappears. It can be useful for server-side validation.
setFieldValue(key: string, value: T) => voidSets the field value.
setFieldInitialValue(key: string, initialValue: string) => voidSets a field's initial value. The value of the field will change if it is pristine.
addFields(fields: Array<Field<any>>) => voidAdds new fields to the form. It allows users to create recurring forms or add new fields during runtime.
removeFieldIds(ids: Array<string>) => voidRemoves fields from the form at runtime. Deletes only fields that were added with the "addFields" function. Fields passed in by configuration cannot be removed.
resetForm() => voidResets all fields and forms to their initial values and states.
submit() => voidSubmits the form, validating all the fields that are required.
validateAll() => booleanValidates all fields. Returns if any field has error

Usage

Basic

Let's create our first component

    import React from 'react'
    import { ReactSmartScroller } from '@codegateinc/react-form-builder-v2'
    
    const getConfig = () => {
        const name = useField({
            key: 'name',
            initialValue: '',
            isRequired: true,
            placeholder: 'name'
        })
        const surname = useField({
            key: 'surname',
            initialValue: '',
            isRequired: true,
            placeholder: 'surname'
        })
    
        return {
            name,
            surname
        }
    }
    
    export const FormBuilder: React.FunctionComponent = () => {
        const { form, submit } = useForm(getConfig(), {
            onSuccess: validForm => console.log(validForm)
        })

        return (
            <Container>
                <Input {...form.name}/>
                <Input {...form.surname}/>
                <Button onClick={submit}>
                    save
                </Button>
            </Container>
        )
    }

    const Container = styled.div`
        display: flex;
        flex-direction: column;
        padding: 100px;
        align-items: center;
        input {
            margin-bottom: 20px;
        }
    `
    
    const Button = styled.div`
        background-color: green;
        padding: 10px 15px;
        color: white;
        border-radius: 20px;
        cursor: pointer;
    `

Here is a sandbox with 3 simple types of different components to show how it works https://codesandbox.io/embed/fragrant-bush-4gpqkl?fontsize=14&hidenavigation=1&theme=dark

Below you can find some usage examples

setError

<Button onClick={() => setError('input', 'custom error')}>
    set error
</Button>

setInitialValue

<Button onClick={() => setInitialValue('input', 'this is new initial value')}>
    set initial value
</Button>

setFieldValue

<Button onClick={() => setFieldvalue('input', 'this is new value')}>
    set field value
</Button>

addFields

<Button
    onClick={() => addFields([{
        key: 'second input',
        placeholder: 'second input'
    }])}
>
    add field
</Button>

removeFields

<Button
    onClick={() => addFields([{
        key: 'second input',
        placeholder: 'second input'
    }])}
>
    add field
</Button>
<Button
    onClick={() => removeFieldIds(['second input'])}
>
    remove field
</Button>

Contribution

Library created by Jacek Pudysz and Grzegorz Tarnopolski

1.3.5

8 days ago

1.3.4

8 days ago

1.3.3

14 days ago

1.3.2

3 months ago

1.3.1

6 months ago

1.3.0

6 months ago

1.2.23

8 months ago

1.2.24

8 months ago

1.2.21

9 months ago

1.2.22

8 months ago

1.2.20

1 year ago

1.2.18

1 year ago

1.2.19

1 year ago

1.2.17

1 year ago

1.2.8

1 year ago

1.2.9

1 year ago

1.2.12

1 year ago

1.2.13

1 year ago

1.2.10

1 year ago

1.2.11

1 year ago

1.2.16

1 year ago

1.2.14

1 year ago

1.2.15

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.20

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.2

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago