1.2.14 • Published 1 year ago

strooks v1.2.14

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

strooks

A fine collection of Hooks, Components and Utils for your React application

Hooks

useForm()

useForm is the simplest, easiest and most versatile way to manage form state in React applications. It's the form manager that works closest to default HTML behavior so the only requirement is that you use the nameproperty in the form elements, so they will become the keys to your state

import { useRef } from 'react'
import { useForm } from 'strooks'

const MyComponent = () => {
  const formRef = useRef()
  const [formState, update, clear] = useForm(formRef, initialState)

  return <form ref={formRef} onSubmit={onSubmit}></form>
}
No need to:
  • pass values into each input, everything is managed for you - the inputs are immediately filled according to the initial state
  • setting up onChange method for the form or each input unless you want to run realtime validation
  • ev.preventDefault() inside your onSubmit button
  • worry about checked property for radio-buttons or checkboxes
out of the box:

Easy Typing

using the attribute type="number" or data-type="number" the value in the state will always be a number:

<form>
  <input type="text" name="firstName" />
  <input type="number" name="age" />
  <select name="yearsOfExperience" data-type="number">
    {new Array(50).fill(null).map(idx => (
      <option value={idx}>{idx} years</option>
    ))}
  </select>
</form>

results in a state typed like:

{ "firstName": "John", "age": 22, "yearsOfExperience": 15 }

Values as Arrays

whether using a multi-select or a set of checkboxes with the same name, the resulting state will be an array with the values:

<form>
  <select name="cars" multiple>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
  </select>
  <div>
    <h5>Hobbies</h5>
    <label>
      <input name="hobbies" type="checkbox" value="swimming" />
      <span>Swimming</span>
    </label>
    <label>
      <input name="hobbies" type="checkbox" value="running" />
      <span>Running</span>
    </label>
    <label>
      <input name="hobbies" type="checkbox" value="tennis" />
      <span>Tennis</span>
    </label>
  </div>
</form>

the resulting state will be:

{ "cars": ["volvo", "opel"], "hobbies": ["swimming", "tennis"] }

Boolean Fields:

getting a true or false from a checkbox or a pair of radio-buttons can be cumbersome, having to parse the checked property of several elements. With useForm is just a matter of "following the HTML"

  • for checkboxes, not passing a value property, turns the property into true or false
  • for radios, whatever value you pass into it, is going to be stored in the state
<form>
  <input type="checkbox" name="agree" />
  <div>
    <input type="radio" name="useColor" value={true} />
    <input type="radio" name="useColor" value={false} />
    {formState.useColor && (
      <div>
        <input type="radio" name="color" value="blue" />
        <input type="radio" name="color" value="red" />
        <input type="radio" name="color" value="green" />
      </div>
    )}
  </div>
</form>

will result in the state:

{ "agree": true, "useColor": true, "color": "red" }

Nested Fields

It's super easy to create forms with nested fields, take the following example:

<form ref={formRef} onSubmit={onSubmit}>
  <input type="text" name="name" />
  <input type="text" name="address.street" />
  <input type="number" name="address.zipCode" />
  <input type="text" name="address.city" />
</form>

will result in the state:

{ "name": "...", "address": { "street": "...", "zipCode": 12345, "city": "..." } }

Custom Components

a lot of the components we find on NPM won't work out of the box with useForm because they don't mimic the default behavior of HTML form elements but it's very easy to circumvent that, using the update method. For example, we're using a custom Select, that:

  • receives an optionsarray in the form of [{ value, label }]
  • needs to receive the selected value as a prop
  • needs an onChange eventHandler that receives as parameter the whole option object
import { useRef } from 'react'
import { useForm } from 'strooks'
import { CustomSelect } from 'some-ui-library'

const App = () => {
  const formRef = useRef()
  const [formState, update] = useForm(formRef)

  const onSelectChange = (option) => {
    update(state => ({...state, flavor: option.value })
  }
  return (
    <form ref={formRef}>
      <CustomSelect options={options} value={formState.flavor} onChange={onSelectChange} />
    </form>
  )
}

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
]

MIT © andrepadez

1.2.0

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.12

1 year ago

1.2.13

1 year ago

1.2.10

1 year ago

1.2.11

1 year ago

1.2.14

1 year ago

1.2.9

1 year ago

1.1.70

1 year ago

1.1.71

1 year ago

1.1.67

1 year ago

1.1.69

1 year ago

1.1.68

1 year ago

1.1.66

2 years ago

1.1.65

2 years ago

1.1.64

2 years ago

1.1.29

2 years ago

1.1.30

2 years ago

1.1.34

2 years ago

1.0.66

2 years ago

1.1.33

2 years ago

1.1.32

2 years ago

1.1.31

2 years ago

1.1.38

2 years ago

1.1.37

2 years ago

1.0.69

2 years ago

1.1.36

2 years ago

1.0.68

2 years ago

1.1.35

2 years ago

1.0.67

2 years ago

1.1.39

2 years ago

1.1.41

2 years ago

1.0.73

2 years ago

1.1.40

2 years ago

1.0.72

2 years ago

1.0.71

2 years ago

1.0.70

2 years ago

1.1.45

2 years ago

1.0.77

2 years ago

1.1.44

2 years ago

1.0.76

2 years ago

1.1.43

2 years ago

1.0.75

2 years ago

1.1.42

2 years ago

1.0.74

2 years ago

1.1.49

2 years ago

1.1.48

2 years ago

1.1.47

2 years ago

1.0.79

2 years ago

1.1.46

2 years ago

1.0.78

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.23

2 years ago

1.1.22

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.27

2 years ago

1.1.26

2 years ago

1.1.25

2 years ago

1.1.24

2 years ago

1.0.80

2 years ago

1.1.52

2 years ago

1.0.84

2 years ago

1.1.51

2 years ago

1.0.83

2 years ago

1.1.50

2 years ago

1.0.82

2 years ago

1.0.81

2 years ago

1.1.56

2 years ago

1.0.88

2 years ago

1.1.55

2 years ago

1.0.87

2 years ago

1.1.54

2 years ago

1.0.86

2 years ago

1.1.53

2 years ago

1.0.85

2 years ago

1.1.59

2 years ago

1.1.58

2 years ago

1.1.57

2 years ago

1.0.90

2 years ago

1.1.63

2 years ago

1.1.62

2 years ago

1.1.61

2 years ago

1.1.60

2 years ago

1.0.65

2 years ago

1.0.64

2 years ago

1.0.63

2 years ago

1.0.62

2 years ago

1.0.61

2 years ago

1.0.60

2 years ago

1.0.59

2 years ago

1.0.58

2 years ago

1.0.57

2 years ago

1.0.56

2 years ago

1.0.55

2 years ago

1.0.53

2 years ago

1.0.52

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.49

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.40

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

0.1.0

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago