Socket
Socket
Sign inDemoInstall

react-formr

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-formr

Form managing component for React & React Native


Version published
Weekly downloads
15
increased by114.29%
Maintainers
1
Weekly downloads
 
Created
Source

react-formr

Centralised Solution for managing values & validation in react native

Installation

Yarn
yarn add react-formr
NPM
npm i --save react-formr

Example Usage

  1. Import Formr
import Formr from "react-formr";
  1. Use it
export const App = () => {
  return (
    <View>
      <Formr
        formFields={{ email: "", phone: "" }}
        validation={{
          email: { required: true, type: "email" },
          phone: { type: "phone" },
        }}>
        {({
          onHandleChange,
          values,
          onHandleBlur,
          touched,
          valid,
          handleSubmit,
        }) => {
          <>
            <TextInput
              style={{
                borderBottomColor: "black",
                borderWidth: 1,
                width: "100%",
              }}
              onChangeText={(e) => onHandleChange("email", e)}
              onBlur={() => onHandleBlur("email")}
              value={values.email}
            />
            {touched.email && !valid.email && <Text>Not valid</Text>}

            <TextInput
              style={{
                borderBottomColor: "black",
                borderWidth: 1,
                width: "100%",
              }}
              onChangeText={(e) => onHandleChange("phone", e)}
              onBlur={() => onHandleBlur("phone")}
              value={values.phone}
            />
            {touched.phone && !valid.phone && <Text>Not valid</Text>}
            <Button
              onPress={() => handleSubmit(console.log)}
              title="Submit"
              color="#841584"
            />
          </>;
        }}
      </Formr>
    </View>
  );
};

Options

Formr props

NameTypeDefaultDescriptionExample
formFieldsStringObject (Object){}Form fields values{email:""}
validationFormrValidation (Object){}Form fields for validation{email:{required:true,type:"email"}}
onChangeFunction()=>{}Function for observing fields changes

Form control function args

To control form fields, The Formr component will provide a function that include

NameTypeUsageDescripionExample
onHandleChangeFunctiononHandleChange( key:string, value:string )To set value of the field, call this function with arguments: key - which input field to update. value to that field <TextInput onChangeText={ (text)=> onHandleChange("email":text) } />
onHandleBlurFunctiononHandleBlur( key:string )To set which field is blurred, call this function with key on blurrEvent<TextInput onBlur={ ()=> onHandleBlur("email") } />
handleSubmitFunctionhandleSubmit( callback:(values)=>{} )This handle submit button & validation flow. This is used to submit form.<Button title="Submit" onPress={ ()=> handleSubmit( (values)=> submitFormToApi(values) ) } />
values{ [key:string]:string, ... }values={ values[key] }Objct of field values, can be used for value input for the TextInput<TextInput value={values.email} />
valid{ [key:string]:boolean, ... }Its is This object contains validation results,true:valid and false:validation fail.{!valid.email && <Text> This fields is invalid </Text>}
touched{ [key:string]:boolean, ... }Its is used to show error message on validation fail.{touched.email && !valid.email && <Text> This fields is invalid </Text>}

Keywords

FAQs

Package last updated on 20 Sep 2020

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