Git Product home page Git Product logo

react-native-lcrew-forms's Introduction

react-native-lcrew-forms

Getting started

$ npm install react-native-lcrew-forms

Usage

import {
    Form,
    EmailFormField,
    TextFormField,
    HiddenFormField,
    PasswordFormField,
    PickerFormField,
    KeyboardShift,
    SubmitFormField,
    ErrorFormField,
    useFormContext,
    AutoCompleteFormField,
} from 'react-native-lcrew-forms'

const emailRegex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i
const validators = {
    requiredEmail: {
        func: value => !!value && emailRegex.test(String(value).toLowerCase()),
        message: 'Invalid email',
    },
    required: {
        func: value => !!value,
        message: 'Required',
    },
    password: {
        func: value => !!value && String(value).length >= 4,
        message: 'Password must be 4 length long',
    },
    passwordEqual: {
        func: (value, values) => !!value && String(value).length >= 4 && values['password'] === value,
        message: 'Password must be confirmed correctly',
    },
}

class SomeComponent extends React.Component {
    ...

    formSubmit = async (values) => {
        // await sleep(1000)
        return { null: 'Global error', email: 'Email already exists' }
    }

    onBlur = (name, value, error) => {
        console.log('Field on blur: ' + name + ', value=' + value + ', error=' + error)
    }

    render() {
        const pickerItems = [
            { label: 'First', value: 1 },
            { label: 'Second', value: 2 },
            { label: 'Third', value: 3 },
        ]
        const autoCompleteItems = [
            { id: 1, name: 'John' },
            { id: 2, name: 'Mario' },
            { id: 3, name: 'Donkey Kong' },
            { id: 4, name: 'Zelda' },
            { id: 5, name: 'Nintendo' },
            { id: 6, name: 'Charlie Sheen' },
        ]
        ...
        return (
            <ScrollView>
                <KeyboardShift>
                    <FormRN
                        onSubmit={this.formSubmit}
                        clearFieldsAfterSubmit={true}>
                    >
                        <ErrorFormField /> 
                        <HiddenFormField name="id" value="5" />
                        <TextFormField name="name" label="Name" validators={validators.required} value="Unknown" />
                        <TextFormField name="description" multiline={true} />
                        <EmailFormField value="" validators={validators.requiredEmail} onBlur={this.onBlur} />
                        <PasswordFormField name="password" label="Password" validators={validators.password} />
                        <PasswordFormField name="password_confirm" label="Password Confirm"
                            validators={validators.passwordEqual} validateOnBlur={true} dependencies={['password']} 
                            hideIfNotEnabled={true} />
                        <PickerFormField name="picker" items={pickerItems} value={pickerItems[0].value} />
                        <SwitchFormField name="switchControl" />
                        <AutoCompleteFormField
                            name="autocomplete" 
                            maxItemsCount={5} maxSelectedItemsCount={0}
                            filterItems={(items, value) => 
                                !value ? [] : items.filter(x => x.name.includes(value.toLowerCase()))} 
                            items={autoCompleteItems} />
                        <SubmitFormField buttonTitle='Save' />
                    </FormRN>
                </KeyboardShift>
            </ScrollView>
        )
    }

Tests

TODO:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.