0.1.48 • Published 4 years ago

react-super-useform v0.1.48

Weekly downloads
52
License
MIT
Repository
github
Last release
4 years ago

Warning

Lib is in development. This is an alpha version. ⚠️

Usage

Installation

npm i --save react-super-useform
import useForm from 'react-super-useform'

const form = useForm(formSchema, initData)

Schema

const formSchema = {

	comment: text_field(),
	informations: {
		fistname: text_field(),
		lastname: text_field()
	}
}

Connect form

<Input {...form.get('comment')}/>
<Input {...form.get('informations.firstname')}/>
<Input {...form.get('informations.lastname')}/>

provide 3 props:

  • value
  • setValue
  • error

you can make your own like that :

const Input = ({value, setValue, error}) => (
	<input value={value} onChange={e=>{
		setValue(e.target.value)
	}} className={error?'error':''}/>
)

Arrays

const formSchema = {
	list:{
		type: Array,
		children: {
			name: text_field()
		},
	}
}

Add element to list

form.get('list').push()

Get elements

form.get('list').map((item,i)=>(
	<Input {...item.get('name')}>
))

Get one element

<Input {...form.get('list.0.name')}>

Remove an element

form.get('list').remove(i)

or

form.get('list').map((item,i)=>(
	<>
		<button onClick={()=>item.remove()}>
			remove
		</button>
	</>
))

Event

You can subscribe to form event :

const myEvent = e => {
	console.log('form has been changed')
}

form.addEventListener('change',myEvent)

And unsubscribe

form.removeEventListener('change',myEvent)

Error checking

You can know if data are valid in form by reading :

form.isValid

You can make a check, that set errors on each field list this :

form.checkErrors()

Output

form.toJSON()
0.1.48

4 years ago

0.1.47

4 years ago

0.1.45

4 years ago

0.1.46

4 years ago

0.1.43

4 years ago

0.1.44

4 years ago

0.1.42

4 years ago

0.1.41

4 years ago

0.1.40

4 years ago

0.1.37

4 years ago

0.1.38

4 years ago

0.1.39

4 years ago

0.1.31

4 years ago

0.1.32

4 years ago

0.1.33

4 years ago

0.1.34

4 years ago

0.1.35

4 years ago

0.1.30

4 years ago

0.1.27

4 years ago

0.1.28

4 years ago

0.1.29

4 years ago

0.1.26

4 years ago

0.1.25

4 years ago

0.1.24

4 years ago

0.1.20

4 years ago

0.1.21

4 years ago

0.1.22

4 years ago

0.1.23

4 years ago

0.1.18

4 years ago

0.1.19

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.12

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.8

4 years ago

0.1.9

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago