1.0.4 • Published 3 years ago

native-x-form v1.0.4

Weekly downloads
68
License
MIT
Repository
github
Last release
3 years ago

native-x-form

semantic-release

This component helps you add and manage form in react native

Install

Yarn

yarn add native-x-form

NPM

npm install native-x-form

Usage

import { Form, FormItem, isEmpty, isInvalidEmail } from 'native-x-form'
import { Button } from 'native-x-button'
import { TextInput } from 'native-x-text-input'
import { Stack } from 'native-x-stack'

interface FormData {
  email: string
  password: string
}

const state: FormData = {
  email: '',
  password: '',
}

function MyComponent() {
  const onSubmit = useCallback(
    async ({ state: { email, password }, isValid }: { state: FormData; isValid: boolean }) => {
      if (!isValid) {
        return
      }
      // your logic
    },
    [],
  )

  return (
    <Form<FormData> state={state} onSubmit={onSubmit}>
      {({ submitForm }) => (
        <Stack>
          <FormItem name='email' validators={[isEmpty('Email is required'), isInvalidEmail()]}>
            <TextInput />
          </FormItem>
          <Button onTap={submitForm}>Submit</Button>
        </Stack>
      )}
    </Form>
  )
}

Any component can be placed inside FormItem as long as the props are extended from FormChildProp

export type AcceptableFormValue = string | boolean | number

type FormChildProp<T extends AcceptableFormValue> = {
  value?: T
  onChange?: (value: T) => void
  onChangeText?: (value: T) => void
  onBlur?: () => void
  error?: string | Error | null
  isLoading?: boolean
}

Sample implementation:

import { FormChildProps } from 'native-x-form'

interface InputProps extends FormChildProps<string> {
  ...
}

function Input(props: InputProps) {
  const {
    value,
    onChange,
    onChangeText,
    onBlur,
    error,
    isLoading
  } = props
  return (
    ...
  )
}

API - Form

PropertyDefault ValueUsage
state?: TanyState of the form
submitIfValid?: booleantrueCall onSubmit only if form is valid
children?: ReactNode[]Content of the form
onSubmit?: (props: { state: T, isValid: boolean}) => PromiseHandler for submitting the form
onChange?: (props: { state: T, isValid: boolean}) => PromiseEvent handler for change

API - Form-Item

PropertyDefault ValueUsage
name: stringanyName of the item in state. This input is mandatory
children?: ReactNode[]Content of the form
validators: Validator[]An array of validators

Validators

You can build a custom validator function by implementing Validator<T> type

export type Validator<T> = (input: T) => string | undefined

The return value of Validator function must be the error message.

Few validator function creators are provided with this module.

isEmpty(errorMessage: string): Validator<T>
isInvalidEmail(errorMessage: string): Validator<T>
isNonAlphaNumeric(errorMessage: string): Validator<T>

Automatic Release

Here is an example of the release type that will be done based on a commit messages:

Commit messageRelease type
fix: commentPatch Release
feat: commentMinor Feature Release
perf: commentMajor Feature Release
doc: commentNo Release
refactor: commentNo Release
chore: commentNo Release