An open API service indexing awesome lists of open source software.

https://github.com/rintoj/native-x-form


https://github.com/rintoj/native-x-form

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

          

# native-x-form

[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

This component helps you add and manage form in react native

## Install

### Yarn

```sh
yarn add native-x-form
```

### NPM

```sh
npm install native-x-form
```

## Usage

```tsx
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 (
state={state} onSubmit={onSubmit}>
{({ submitForm }) => (




Submit

)}

)
}
```

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

```tsx
export type AcceptableFormValue = string | boolean | number

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

Sample implementation:

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

interface InputProps extends FormChildProps {
...
}

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

## API - Form

| Property | Default Value | Usage |
| ----------------------------------------------------------------- | ------------- | ----------------------------------- |
| state?: T | any | State of the form |
| submitIfValid?: boolean | true | Call onSubmit only if form is valid |
| children?: ReactNode[] | | Content of the form |
| onSubmit?: (props: { state: T, isValid: boolean}) => Promise | | Handler for submitting the form |
| onChange?: (props: { state: T, isValid: boolean}) => Promise | | Event handler for change |

## API - Form-Item

| Property | Default Value | Usage |
| -------------------------- | ------------- | ---------------------------------------------------- |
| name: string | any | Name 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` type

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

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

Few validator function creators are provided with this module.

```tsx
isEmpty(errorMessage: string): Validator
isInvalidEmail(errorMessage: string): Validator
isNonAlphaNumeric(errorMessage: string): Validator
```

## Automatic Release

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

| Commit message | Release type |
| ------------------- | --------------------- |
| fix: [comment] | Patch Release |
| feat: [comment] | Minor Feature Release |
| perf: [comment] | Major Feature Release |
| doc: [comment] | No Release |
| refactor: [comment] | No Release |
| chore: [comment] | No Release |