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

https://github.com/shrijan00003/mero-react-form


https://github.com/shrijan00003/mero-react-form

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# Mero React Form

> Simple react form

[![NPM](https://img.shields.io/npm/v/mero-react-form.svg)](https://www.npmjs.com/package/mero-react-form) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Install

```bash
npm install --save mero-react-form
# Or
yarn add mero-react-form
```

## Usage

```tsx
import React from 'react'
import * as yup from 'yup'

import { InputField, useMeroForm } from 'mero-react-form'

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))

const schema = yup.object().shape({
name: yup.string().required('Name required'),
address: yup.string().min(18, 'Must be at least 18 len')
})

const App = () => {
const { values, handleSubmit, errors, removeError } = useMeroForm({
initialValues: {
name: '',
address: ''
},
validationSchema: schema,
onSubmit: async ({ values }) => {
console.log({ name: values.name, address: values.address })
await sleep(1000)
alert(JSON.stringify(values, null, 2))
}
})

return (
<>



{errors.name ? errors.name : null}




{errors.address ? errors.address : null}


Submit

>
)
}

export default App
```

## Completed

- [x] Minimal input field with usePropState (That will be updated if props change)
- [x] useMeroForm hook with `values, handleSubmit, handleBlur, errors, removeError`
- [x] yup schema validation.
- [x] Validation on submit by default
- [x] Errors will have error values asynchronously

## Todo

- [ ] Handle change if user want and does not care about re rendering.
- [ ] custom validation function for real time validation
- [ ] `setFieldValue, setFieldError, setValues, setErrors` utilities.
- [ ] reinitialize functionality

## License

MIT © [shrijan00003](https://github.com/shrijan00003)