0.1.48 • Published 5 years ago

react-super-useform v0.1.48

Weekly downloads
52
License
MIT
Repository
github
Last release
5 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

5 years ago

0.1.47

5 years ago

0.1.45

5 years ago

0.1.46

5 years ago

0.1.43

5 years ago

0.1.44

5 years ago

0.1.42

5 years ago

0.1.41

5 years ago

0.1.40

5 years ago

0.1.37

5 years ago

0.1.38

5 years ago

0.1.39

5 years ago

0.1.31

5 years ago

0.1.32

5 years ago

0.1.33

5 years ago

0.1.34

5 years ago

0.1.35

5 years ago

0.1.30

5 years ago

0.1.27

5 years ago

0.1.28

5 years ago

0.1.29

5 years ago

0.1.26

5 years ago

0.1.25

5 years ago

0.1.24

5 years ago

0.1.20

5 years ago

0.1.21

5 years ago

0.1.22

5 years ago

0.1.23

5 years ago

0.1.18

5 years ago

0.1.19

5 years ago

0.1.14

5 years ago

0.1.15

5 years ago

0.1.16

5 years ago

0.1.17

5 years ago

0.1.12

5 years ago

0.1.10

5 years ago

0.1.11

5 years ago

0.1.8

5 years ago

0.1.9

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago