npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-formr

v1.3.3

Published

Form managing component for React & React Native

Downloads

3

Readme

react-formr

react-formr

npm version npm MIT styled with prettier

Centralised Solution for managing values, validations & input focusing in react native.

Features

  1. Form validation on given rules (regex) or predefined types(email, phone, etc).
  2. Input binder function includes almost everything TextInput required to handle form.
  3. Auto focus next available input on submit press, triggering onFinishFocus on last input submit.
  4. Input blur validation & validate on change of invalid input.
  5. Listen to live changes in form using onChange props.

Detailed blog post Easy React Native Form management with react-formr

Installation

Yarn

yarn add react-formr

NPM

npm i --save react-formr

Example Usage

Import

import Formr to use with Formr wrapping component OR import {useFormr} to use Formr Hook.

import Formr, { useFormr } from 'react-formr';

Using useFormr Hook.

export const App = () => {
    const {
        onChangeHandler,
        onBlurHandler,
        onSubmitEditingHandler,
        onSubmitHandler,
        inputBinder,
        refsHandler,
        values,
        touched,
        valid
    } = useFormr({
        formFields: { email: '', phone: '' },
        validation: {
            email: { required: true, type: 'email' },
            phone: { type: 'phone' }
        }
    });
    return (
        <View>
            <TextInput
                style={{
                    borderBottomColor: 'black',
                    borderWidth: 1,
                    width: '100%'
                }}
                onChangeText={(e) => onChangeHandler('email', e)}
                onBlur={() => onBlurHandler('email')}
                value={values.email}
                ref={(ref) => refsHandler('password', ref)}
            />
            {touched.email && !valid.email && <Text>Not valid</Text>}
            {/* Using input binder, No need to take any other function than inputBinder from formr to work with input*/}
            <TextInput
                style={{
                    borderBottomColor: 'black',
                    borderWidth: 1,
                    width: '100%'
                }}
                {...inputBinder('phone')}
            />
            {touched.phone && !valid.phone && <Text>Not valid</Text>}
            <Button
                onPress={() => onSubmitHandler(console.log)}
                title="Submit"
                color="#841584"
            />
        </View>
    );
};

Using Formr wrapping component.

export const App = () => {
    return (
        <View>
            <Formr
                formFields={{ email: '', phone: '' }}
                validation={{
                    email: { required: true, type: 'email' },
                    phone: { type: 'phone' }
                }}
            >
                {({
                    onChangeHandler,
                    onBlurHandler,
                    onSubmitEditingHandler,
                    onSubmitHandler,
                    inputBinder,
                    refsHandler,
                    values,
                    touched,
                    valid
                }) => {
                    <>
                        <TextInput
                            style={{
                                borderBottomColor: 'black',
                                borderWidth: 1,
                                width: '100%'
                            }}
                            onChangeText={(e) => onChangeHandler('email', e)}
                            onBlur={() => onBlurHandler('email')}
                            value={values.email}
                            ref={(ref) => refsHandler('password', ref)}
                        />
                        {touched.email && !valid.email && (
                            <Text>Not valid</Text>
                        )}
                        {/* Using input binder, No need to take any other function than inputBinder from formr to work with input*/}
                        <TextInput
                            style={{
                                borderBottomColor: 'black',
                                borderWidth: 1,
                                width: '100%'
                            }}
                            {...inputBinder('phone')}
                        />
                        {touched.phone && !valid.phone && (
                            <Text>Not valid</Text>
                        )}
                        <Button
                            onPress={() => onSubmitHandler(console.log)}
                            title="Submit"
                            color="#841584"
                        />
                    </>;
                }}
            </Formr>
        </View>
    );
};

Options

Formr props

| Name | Type | Default | Description | Example | | --------------- | -------------------------- | ----------------------------- | ------------------------------------------------------------------------------------ | -------------------------------------- | | formFields | StringObject (Object) | {} | Form fields values | {email:""} | | validation | FormrValidation (Object) | {} | Form fields for validation | {email:{required:true,type:"email"}} | | onChange | Function | (values:StringObject)=>void | Function for observing fields changes | | onFinishFocus | Function | (values:StringObject)=>void | Function to trigger on all input focus finished on hitting return key on last input. | |

Form control functions

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

| Name | Type | Usage | Descripion | Example | | ------------------------ | ------------------------------- | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------- | | inputBinder | Function | inputBinder( key:string ) | Which includes almost everything of TextInput: value, onChangeText, onBlur, ref, onSubmitEditing also valid & touched if you are making custom input component with these props | <TextInput {...inputBinder('email')} /> | | onChangeHandler | Function | onChangeHandler( 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) } /> | | onBlurHandler | Function | onBlurHandler( key:string ) | To set which field is blurred, call this function with key on blurrEvent | <TextInput onBlur={ ()=> onBlurHandler("email") } /> | | refsHandler | Function | refsHandler( key:string, ref:any ) | To set which field is blurred, call this function with key on blurrEvent | <TextInput ref={ (ref)=> refsHandler("email",ref) } /> | | onSubmitEditingHandler | Function | onSubmitEditingHandler( key:string ) | To set which field is blurred, call this function with key on blurrEvent | <TextInput onSubmitEditing={ ()=> onSubmitEditingHandler("email") } /> | | onSubmitHandler | Function | onSubmitHandler( callback:(values)=>{} ) | This handle submit button & validation flow. This is used to submit form. | <Button title="Submit" onPress={ ()=> onSubmitHandler( (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>} |

Todo

  • [ ] Add testing
  • [ ] To add more validation types / Yup
  • [ ] To remove validator dependancy / Yup
  • [ ] Other elements & values support