1.0.114 • Published 2 years ago

react-with-native-form v1.0.114

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

react-with-native-form

Plug-and-play yet unopinionated and extensible forms constructor for React and React Native apps

Installation

  1. make sure you install react-with-native

  2. yarn add react-with-native-form

Usage

Choose the packages you want to include for the different input types:

yarn add react-with-native-text-input react-with-native-password-input react-with-native-toggle-input react-with-native-select-input react-with-native-textarea-input react-with-native-date-input react-with-native-phone-input react-with-native-number-input

Then create a wrapper like this:

import DataForm, {
    setConfig,
    makeInputField,
    Field,
    Keys,
    DataFormProps,
    Plugin,
  } from "react-with-native-form";

  import TextInput, { TextInputType } from "react-with-native-text-input";
  import PasswordInput, {
    PasswordInputType,
  } from "react-with-native-password-input";
  import ToggleInput, { ToggleInputType } from "react-with-native-toggle-input";
  import ImageInput, { ImageInputType, ImageValue } from "./ImageInput";

  import SelectInput, { SelectInputType } from "react-with-native-select-input";
  import TextAreaInput, {
    TextAreaInputType,
  } from "react-with-native-textarea-input";
  import DateInput, { DateInputType } from "react-with-native-date-input";
  import PhoneInput, { PhoneInputType } from "react-with-native-phone-input";
  import NumberInput, { NumberInputType } from "react-with-native-number-input";
  import { getEnv } from "react-with-native";

  export const isValidImage = (value: ImageValue | null) =>
    !!value?.base64 && !!value?.url;

  const text = {
    component: TextInput,
  };


  const phone = {
    component: PhoneInput,
  };
  const date = {
    component: DateInput,
  };
  const password = {
    component: PasswordInput,
  };

  const toggle = {
    component: ToggleInput,
  };

  const image = {
    component: ImageInput,
  };


  const select = {
    component: SelectInput,
  };


  const textArea = {
    component: TextAreaInput,
  };

  const number = {
    component: NumberInput,
  };

  const plugins = {
    text,
    password,
    toggle,
    image,
    select,
    textArea,
    date,
    phone,
    number,
  };

  export const makeField = <T extends Keys<AllInputs>>(
    type: T,
    config: Omit<Field<AllInputs, T>, "type">
  ) => makeInputField<AllInputs, T>(type, config);

  export interface AllInputs {
    text: TextInputType;
    password: PasswordInputType;
    toggle: ToggleInputType;
    image: ImageInputType;
    select: SelectInputType;
    textArea: TextAreaInputType;
    date: DateInputType;
    phone: PhoneInputType;
    number: NumberInputType;
  }

  export const Form = <TState extends { [key: string]: any } = any>(
    props: DataFormProps<AllInputs, TState>
  ) =>
    setConfig<AllInputs, TState>(DataForm, {
      plugins,
    })(props);

Now you can create a form like this:

#import this
import { Form, makeField } from "../components/form";

# define your fields
const fields =
      [
          makeField("text", {
            field: "username",
            title: "Username",
            startSection: true,
            sectionTitle: "Account",
            extra: {
              placeholder: "Username",
            },
            hasError: (value) =>
              !isValidVarchar(value as string, false)
                ? "Fill in a username that's less than 256 characters"
                : false,
          }),

          makeField("text", {
            field: "email",
            title: "Email",
            extra: {
              placeholder: "Email",
            },
            hasError: (value) =>
              !isValidVarchar(value as string, false)
                ? "Fill in an email that's less than 256 characters"
                : false,
          }),

          makeField("password", {
            field: "password1",
            title: "Password",
            extra: {
              placeholder: "password",
            },
            hasError: (value) =>
              !isValidVarchar(value as string, false, 1024)
                ? "Fill in a password shorter than 1024 characters"
                : false,
          }),

          makeField("password", {
            field: "password2",
            title: "Repeat Password",
            extra: {
              placeholder: "password",
            },
            hasError: (value) =>
              !isValidVarchar(value as string, false, 1024)
                ? "Fill in a password shorter than 1024 characters"
                : false,
          }),

          makeField("text", {
            field: "name",
            title: "Name",
            startSection: true,
            sectionTitle: "Personal Information",
            extra: {
              placeholder: "Name",
            },
            hasError: (value) =>
              !isValidVarchar(value as string, false)
                ? "Fill in a name that's less than 256 characters"
                : false,
          }),

          makeField("text", {
            field: "phone",
            title: "Phone number",
            extra: {
              placeholder: "Phone number",
            },
            hasError: (value) =>
              !isValidVarchar(value as string, false)
                ? "Fill in a phone number"
                : false,
          }),

          makeField("text", {
            field: "country",
            title: "Country",
            startSection: true,
            sectionTitle: "Geography",
            extra: {
              placeholder: "Country",
            },
            hasError: (value) =>
              !isValidVarchar(value as string, false)
                ? "Fill in a country that's less than 256 characters"
                : false,
          }),

          makeField("text", {
            field: "city",
            title: "City",
            extra: {
              placeholder: "City",
            },
            hasError: (value) =>
              !isValidVarchar(value as string, false)
                ? "Fill in a city that's less than 256 characters"
                : false,
          }),
        ];

# render this
 <Form
    title="Become a host"
    fields={fields}
    onSubmit={(values,resolve,reject) => {

        //do something with those values
    }}
    />

And you're done!

1.0.109

2 years ago

1.0.110

2 years ago

1.0.112

2 years ago

1.0.111

2 years ago

1.0.114

2 years ago

1.0.113

2 years ago

1.0.107

2 years ago

1.0.106

2 years ago

1.0.108

2 years ago

1.0.105

2 years ago

1.0.104

2 years ago

1.0.101

2 years ago

1.0.100

2 years ago

1.0.103

2 years ago

1.0.102

2 years ago

1.0.98

2 years ago

1.0.97

2 years ago

1.0.96

2 years ago

1.0.95

2 years ago

1.0.94

2 years ago

1.0.93

2 years ago

1.0.92

2 years ago

1.0.91

2 years ago

1.0.90

2 years ago

1.0.89

2 years ago

1.0.88

2 years ago

1.0.87

2 years ago

1.0.86

2 years ago

1.0.85

2 years ago

1.0.84

2 years ago

1.0.83

2 years ago

1.0.82

2 years ago

1.0.81

2 years ago

1.0.80

2 years ago

1.0.79

2 years ago

1.0.78

2 years ago

1.0.77

2 years ago

1.0.76

2 years ago

1.0.75

2 years ago

1.0.74

2 years ago

1.0.73

2 years ago

1.0.72

2 years ago

1.0.71

2 years ago

1.0.70

2 years ago

1.0.69

2 years ago

1.0.68

2 years ago

1.0.67

2 years ago

1.0.66

2 years ago

1.0.65

2 years ago

1.0.64

2 years ago

1.0.63

2 years ago

1.0.62

2 years ago

1.0.61

2 years ago

1.0.60

2 years ago

1.0.59

2 years ago

1.0.58

2 years ago

1.0.57

2 years ago

1.0.56

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.53

2 years ago

1.0.52

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.49

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.40

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.21

2 years ago

1.0.17

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago