4.1.1 • Published 4 years ago

another-use-form-hook v4.1.1

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

Build Status Code Coverage

version downloads MIT License

PRs Welcome Code of Conduct

Watch on GitHub Star on GitHub


⚠ WARNING! This is still in development! ⚠

I am new to many things here... semantic-release, npm publishing and TypeScript is all the stuff I haven't tried before, so please excuse me for the "messy" start. I am hoping to keep everything clean from now on... The documentation is still missing, and there are some rough edges on the inside, so please use with caution! Other than that, please enjoy! ✨💖🚀

Table of Contents

Installation

This should be installed as one of your project dependencies:

yarn add another-use-form-hook

or

npm install --save another-use-form-hook

Usage

NOTE: This section is a work in progress.

This hook is intended to give a full solution for handling forms. From interdependent field value validations (meaning if a field value is dependent on anothers' values), to submitting the form, and providing information about when the UI should be unresponsive (loading of some kind of asnyc-like operation), in addition to notification "hooks" to be able to inform the users the most efficient way.

Let's see a complex example, to understand how it works, and what are its capabilities:

import React from "react"
import ReactDOM from "react-dom"
import useForm, {FormProvider} from "another-use-form-hook"

/**
 * NOTE: We are using date-fns for this example,
 * but it is absolutly not a requirement.
 */
import {addDays, isAfter, differenceInDays} from "date-fns"


const TODAY = new Date()


const App = () => {
  const form = useForm({
    name: "form",
    validators: {
      // Earliest arrival tomorrow
      arrival: ({arrival}) => isAfter(arrival, TODAY),
      // Earliest departure day after tomorrow
      departure: ({departure}) => isAfter(addDays(departure, 1), TODAY),
      // Arrival and departure depends on each other to be valid.
      minOneNight: ({arrival, departure}) => differenceInDays(departure, arrival) >= 1
    }
  })

  return (
    <form onSubmit={form.handleSubmit}>

      {/* With current (2.x) API */}

      <label htmlFor="arrival">
        { form.fields.arrival.error ? "Invalid" : "" } arrival
      </label>
      <input 
        id="arrival"
        name="arrival"
        value={form.fields.arrival.value}
        onChange={e => form.handleChange(e, ["minOneNight"])}
      />

      <label htmlFor="departure">
        { form.fields.departure.error ? "Invalid" : "" } departure
      </label>
      <input 
        id="departure"
        name="departure"
        value={form.fields.arrival.value}
        onChange={e => form.handleChange(e, ["minOneNight"])}
      />

      {/* With new API (available from 3.0) */}
      
      <label htmlFor="departure">
        { form.errors.departure ? "Invalid" : "" } departure
      </label>
      <input
        { ...form.inputs.date("departure")
        onChange={e => form.handleChange(e, ["minOneNight"])}
      />

      <label htmlFor="email">{ form.errors.email ? "Invalid" : "" } email</label>
      <input { ...form.inputs.email("email") }/>

    </form>
  )
}

ReactDOM.render(
  <FormProvider initialState={{
    form: {
      arrival: TODAY,
      departure: addDays(TODAY, 1)
    }
  }}>
    <App/>
  </FormProvider>
  , document.querySelector("#root"))

LICENSE

MIT

4.2.0-alpha.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.2.0

5 years ago

3.1.5

5 years ago

3.1.4

5 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

3.0.0-beta.3

5 years ago

3.0.0-beta.2

5 years ago

3.0.0-alpha.21

5 years ago

3.0.0-alpha.20

5 years ago

3.0.0-alpha.19

5 years ago

3.0.0-alpha.18

5 years ago

3.0.0-alpha.17

5 years ago

3.0.0-alpha.16

5 years ago

3.0.0-alpha.15

5 years ago

3.0.0-alpha.14

5 years ago

3.0.0-alpha.13

5 years ago

3.0.0-alpha.12

5 years ago

3.0.0-alpha.11

5 years ago

3.0.0-alpha.10

5 years ago

3.0.0-alpha.9

5 years ago

3.0.0-alpha.8

5 years ago

3.0.0-alpha.7

5 years ago

3.0.0-alpha.6

5 years ago

3.0.0-alpha.5

5 years ago

3.0.0-alpha.4

5 years ago

3.0.0-alpha.3

5 years ago

2.8.0

5 years ago

2.7.0

5 years ago

3.0.0-alpha.2

5 years ago

3.0.0-alpha.1

5 years ago

3.0.0-alpha.0

5 years ago

3.0.0-beta.1

5 years ago

3.0.0-beta.0

5 years ago

2.6.1

5 years ago

2.6.0

5 years ago

2.5.2

5 years ago

2.5.1

5 years ago

2.5.0

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago