1 | # `@shopify/react-form-state`
|
2 |
|
3 | [![Build Status](https://travis-ci.org/Shopify/quilt.svg?branch=master)](https://travis-ci.org/Shopify/quilt)
|
4 | [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md) [![npm version](https://badge.fury.io/js/%40shopify%2Freact-form-state.svg)](https://badge.fury.io/js/%40shopify%2Freact-form-state.svg) [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@shopify/react-form-state.svg)](https://img.shields.io/bundlephobia/minzip/@shopify/react-form-state.svg)
|
5 |
|
6 | Manage React forms tersely and type-safely with no magic.
|
7 |
|
8 | This library is now superseded by [@shopify/react-form](https://github.com/Shopify/quilt/tree/master/packages/react-form) as it allows you to write the preferred, functional, and hooks-driven React components over class-based ones.
|
9 |
|
10 | ## Installation
|
11 |
|
12 | ```bash
|
13 | $ yarn add @shopify/react-form-state
|
14 | ```
|
15 |
|
16 | ## Usage
|
17 |
|
18 | ### `<FormState />`
|
19 |
|
20 | The default component exported by this library is `<FormState />`.
|
21 |
|
22 | ```typescript
|
23 | import FormState from '@shopify/react-form-state';
|
24 | ```
|
25 |
|
26 | ```typescript
|
27 | // Fields here refers to the inferred type of your initialValues object
|
28 | interface Props<Fields> {
|
29 | initialValues: Fields;
|
30 | validators?: Partial<ValidatorDictionary<Fields>>;
|
31 | onSubmit?: SubmitHandler<Fields>;
|
32 | validateOnSubmit?: boolean;
|
33 | children(form: FormDetails<Fields>): React.ReactNode;
|
34 | }
|
35 | ```
|
36 |
|
37 | Its only mandatory props are `initialValues` and `children`. The `initialValues` prop is used to infer all the types for the rest of the component, and to generate handlers and field state objects. The `children` prop expects a function of the current state of the form, which is represented by a `FormDetails` object.
|
38 |
|
39 | ```typescript
|
40 | <FormState initialValues={myInitialValues}>
|
41 | {({fields, dirty, valid, submitting, errors, reset, submit}) => {
|
42 | return /* some cool ui */;
|
43 | }}
|
44 | </FormState>
|
45 | ```
|
46 |
|
47 | For detailed explanations of how to use `<FormState />` check out [the guide](https://github.com/Shopify/quilt/tree/master/packages/react-form-state/docs/building-forms.md).
|
48 |
|
49 | ### `validators`
|
50 |
|
51 | The library also makes a number of validation factory functions available out of the box that should help with common use cases, as well as some tools to make building reusable custom validators easy.
|
52 |
|
53 | ```typescript
|
54 | import {validate, validators} from '@shopify/react-form-state';
|
55 | ```
|
56 |
|
57 | For detailed explanations of the validation utilities, check out [the validation docs](https://github.com/Shopify/quilt/tree/master/packages/react-form-state/docs/validators.md).
|