0.1.45 • Published 3 years ago

@codegateinc/react-form-builder v0.1.45

Weekly downloads
7
License
MIT
Repository
github
Last release
3 years ago

@codegateinc/react-form-builder

Supports both React and React native

@codegateinc/react-form-builder is a javascript library that helps you create your own form in the simple way. It can be used in React and React Native without any worry about compatibility.

Installation

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

Usage

1. First step is to wrap your app in FormProvider

    import { FormProvider } from 'react-form-builder'

    const App = () => (
        <FormProvider>
            {routes/components/children}
        </FormProvider>
    )

2. Second step is to implement your form

You can take formConfig from here and move it somewhere else, same as rendered components, you can just simply create helpers that will render input, checkbox or picker for you.

Important

Just remember that you need to render Field.TYPES directly to Form, so helpers must be functions that returns JSX

    import { Form, useForm } from '@codegateinc/react-form-builder'

    useForm({
        formName: "uniqueFOrmName",
        formConfig={
            inputName: {
                type: FormTypes.FormFieldType.Input,
                value: ''
            },
            checkboxName: {
                type: FormTypes.FormFieldType.CheckBox,
                value: false
            },
            pickerName: {
                type: FormTypes.FormFieldType.Picker,
                options: [
                    {
                        value: 0,
                        label: 'first'
                    },
                    {
                        value: 1,
                        label: 'second'
                    }
                ]
            }
        }
    })

    const form = (
        <Form formName="uniqueFormName">
            <Field.Input
                formFieldName="inputName"
                component={({ value, onChangeText }) => (
                    <input
                        value={value}
                        onChange={({ target }) => onChangeText(target.value)}
                    />
                )}
            />
            <Field.Checkbox
                formFieldName="checkboxName"
                component={({ value, onChange }) => (
                    <input
                        type="checkbox"
                        checked={value}
                        onClick={onChange}
                    />
                )}
            />
            <Field.Picker
                formFieldName="pickerName"
                component={({ options, onChange }) => (
                    <select
                        value={options.filter(option => option.isSelected)[0]?.value}
                        onChange={({ target }) => {
                            const selectedOption = options.filter(option => option.value === target.value)

                            onChange(selectedOption)
                        }}
                    >
                        {options.map(({ value, label }) => (
                            <option
                                value={value}
                                key={value}
                            >
                                {label}
                            </option>
                        ))}
                    </select>
                )}
            />
        </Form>
    )

Props

Form

propstypedescription
formNameformName: stringunique formName
FormConfig
FormConfig is an Object with FieldConfig assigned to each key
FieldConfig
propstypedescription
valuevalue?: string / number / booleanvalue defines initial values of input and checkbox
typetype: FormFieldTypetype is required prop that defines type of field
isRequiredisRequired?: booleanprop that defines if field should be validated, if true, you must provide validationRules
validationRulesvalidationRules?: Array<FormValidationRule>defines validation rules and error message to each rule
optionsoptiions?: Array<FormOption>options defines initial values of picker field
disableddisabled?: booleandefines if field is disabled or not
liveParserliveParse?: (value: ValidationValue) => ValidationValueif defined, it is called on each field change, which the function is related to
forceLiveValidateforceLiveValidate?: booleanprop that defines if field is validated on each change

Field

componentpropsdescription
InputformFieldName: stringfield name the same as in formConfig
component(props: InputComponentProps): React.ReactNoderender function
CheckboxformFieldName: stringfield name the same as in formConfig
component(props: CheckboxComponentProps): React.ReactNoderender function
PickerformFieldName: stringfield name the same as in formConfig
component(props: PickerComponentProps): React.ReactNoderender function

FormTypes

nametypevalue
FormFieldTypeenumInput, Picker, CheckBox
FormValidationRuletype{ errorMessage: string, validationFunction: (value: ValidationValue, formState: FormState) => boolean}
ValidationValuetypestring \| number \| boolean \| Array<FormOption>
FormOptiontype{ value: FormOptionValue, label: string, isSelected?: boolean }
FormOptionValuetypenumber \| string

useForm hook

useForm hook provides submit function along with some useful functions that allow to for example get value of single field or subscribe to it

useForm hooks returns
functiontypedescription
submitForm() => voidcall to this function validates every field that was defined with validationRule, calls Form's onError if errors occurs or onSuccess
hasChangesbooleanvalue that informs about any change done to form
setField(formFieldName: string, config: Omit<FieldConfig, 'type'>) => voidsets new config for selected field
isFormValidbooleanvalue that tells if form is valid
getField(formFieldName: string) => FormInputField \| FormCheckboxField \| FormPickerFieldreturns selected field
restoreToInitial() => voidrestores form config to initial values
clearForm() => voidclears all values of form
subscribe(formFieldName: string) => { onChange: <T>((value: T) => void) => void }subscribes to field and returns value from form after it changes (this particular field)
setFieldError(formFieldName: string, errorMessage: string) => voidsets custom error message to field
useForm hooks props
prop nametypedescription
formNameformName: stringunique formName
formConfigformConfig: KeyValuePair<FormConfig>form config that includes initial values, options, validators, and types of fields
onSuccessonSuccess?: form => voidoptional function that provides parsed form after it has been submitted and is valid
onErroronError?: Functionoptional function that is invoked when form is not valid and has been submitted
onUpdateonUpdate?: form => voidoptional function that calls on each form change

Form fields

Input

types

type InputComponentProps = {
    value: string,
    disabled: boolean,
    isPristine: boolean,
    errorMessage?: string,
    onBlur: VoidFunction,
    onChangeText(text: string): void
}

type InputProps = {
    formFieldName: string,
    component(props: InputComponentProps): React.ReactNode
}

render example

<Field.Input
    formFieldName={formFieldName}
    component={({
        value,
        onChangeText,
        onBlur
    }) => {
        return (
            <input
                value={value}
                onChange={({ target }) => onChangeText(target.value)}
                onBlur={onBlur}
            />
        )
    }}
/>

Checkbox

props

type CheckboxComponentProps = {
    value: boolean,
    disabled: boolean,
    isPristine: boolean,
    errorMessage?: string,
    onChange: VoidFunction
}

type CheckBoxProps = {
    formFieldName: string,
    component(props: CheckboxComponentProps): React.ReactNode
}

render example

<Field.Checkbox
    formFieldName={formFieldName}
    component={({
        value,
        onChange
    }) => (
        <input
            type="checkbox"
            checked={value}
            onClick={onChange}
        />
    )}
/>

Picker

props

type PickerOnChange = (options: Array<FormOption>) => void

type PickerComponentProps = {
    disabled: boolean,
    isPristine: boolean,
    errorMessage?: string,
    onChange: PickerOnChange,
    options: Array<FormOption>
}

type PickerProps = {
    formFieldName: string,
    component(props: PickerComponentProps): React.ReactNode
}
0.1.45

3 years ago

0.1.44

3 years ago

0.1.43

3 years ago

0.1.41

3 years ago

0.1.42

3 years ago

0.1.40

3 years ago

0.1.39

4 years ago

0.1.38

4 years ago

0.1.36

4 years ago

0.1.37

4 years ago

0.1.35

4 years ago

0.1.32

4 years ago

0.1.28

4 years ago

0.1.26

4 years ago

0.1.25

4 years ago

0.1.24

4 years ago

0.1.23

4 years ago

0.1.21

4 years ago

0.1.20

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.18

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.2

4 years ago

0.1.7

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.0.38

4 years ago