New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@codegateinc/react-form-builder

Package Overview
Dependencies
Maintainers
3
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@codegateinc/react-form-builder

## Supports both React and React native

  • 0.1.45
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
decreased by-69.23%
Maintainers
3
Weekly downloads
 
Created
Source

@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
}

FAQs

Package last updated on 27 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc