Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
docs: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Dec 8, 2020
1 parent 5f680f4 commit 5b2347c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 255 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-pandas-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

docs: update readme
268 changes: 13 additions & 255 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
> 🚨 Under developing the API may be changed fast, **don't use it in production atm**. Please note any changes via [release](https:/wellyshen/react-cool-form/releases). Here's the [milestone](#milestone).
> 🚨 Under developing the API may be changed fast, **DON'T USE IT ATM**. Please note any changes via [release](https:/wellyshen/react-cool-form/releases). Here's the [milestone](#milestone).
# <b>REACT COOL FORM</b>

Expand All @@ -22,19 +22,26 @@ React hooks for forms state and validation, less code more performant.
- ☁️ Server-side rendering compatibility.
- 🦔 Tiny size ([~ 4.7KB gzipped](https://bundlephobia.com/result?p=react-cool-form)) but powerful.

## Getting Started
## [Docs](https://react-cool-form.netlify.app)

To use `react-cool-form`, you must use `react@16.8.0` or greater which includes hooks.
See the documentation at [react-cool-form.netlify.app](https://react-cool-form.netlify.app) for more information about using `react-cool-form`!

You can install this package via [npm](https://www.npmjs.com/package/react-cool-form).
Frequently viewed docs:

- [Getting Started](https://react-cool-form.netlify.app/docs)
- API Reference

## Quick Start

To use `react-cool-form`, you must use `[email protected]` or greater which includes hooks. This package is distributed via [npm](https://www.npmjs.com/package/react-cool-form).

```sh
$ yarn add react-cool-form
# or
$ npm install --save react-cool-form
```

Here's the basic example of how does it works, full documentation will be provided soon. If you have any question, feel free to [ask me](https:/wellyshen/react-cool-form/issues/new?template=question.md).
Here's the basic example of how does it works:

[![Edit RCF - Basic](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/rcf-basic-jq93g?fontsize=14&hidenavigation=1&theme=dark)

Expand Down Expand Up @@ -84,256 +91,7 @@ const App = () => {
};
```

The form state of the above example will look something like this:

```json
{
"values": {
"name": "Welly",
"email": "[email protected]",
"password": "12345"
},
"touched": {
"name": true,
"email": true,
"password": true
},
"isValidating": false,
"isValid": false,
"errors": {
"password": "Please lengthen this text to 8 characters or more"
},
"isDirty": true,
"dirtyFields": {
"name": true,
"email": true,
"password": true
},
"isSubmitting": false,
"isSubmitted": false,
"submitCount": 1
}
```

Super easy right? The above example is just the tip of the iceberg. `react-cool-form` is a lightweight and powerful form library. you can understand the **API** by its type definition atm to see how it rocks 🤘🏻.

```ts
import { FocusEvent, RefObject, SyntheticEvent } from "react";

type FormValues = Record<string, any>;

type DeepProps<V, T = any> = {
[K in keyof V]?: V[K] extends T ? T : DeepProps<V[K]>;
};

type Errors<V> = DeepProps<V>;

type FormState<V = FormValues> = Readonly<{
values: V;
touched: DeepProps<V, boolean>;
errors: Errors<V>;
isDirty: boolean;
dirtyFields: DeepProps<V, boolean>;
isValidating: boolean;
isValid: boolean;
isSubmitting: boolean;
isSubmitted: boolean;
submitCount: number;
}>;

type Options<V> = Omit<Return<V>, "form" | "field" | "submit" | "controller">;

interface OnReset<V = FormValues> {
(
values: V,
options: Omit<Options<V>, "reset">,
event?: Event | SyntheticEvent<any>
): void;
}

interface OnSubmit<V = FormValues> {
(
values: V,
options: Options<V>,
event?: Event | SyntheticEvent<any>
): void | Promise<void>;
}

interface OnError<V = FormValues> {
(
errors: Errors<V>,
options: Options<V>,
event?: Event | SyntheticEvent<any>
): void;
}

interface Debug<V> {
(formState: FormState<V>): void;
}

interface FormValidator<V = FormValues> {
(values: V): Errors<V> | void | Promise<Errors<V> | void>;
}

interface FieldValidator<V = FormValues> {
(value: any, values: V): any | Promise<any>;
}

interface FieldRef<V> {
(
validateOrOptions:
| FieldValidator<V>
| {
validate?: FieldValidator<V>;
valueAsNumber?: boolean;
valueAsDate?: boolean;
parse?: Parse;
}
): (
field: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null
) => void;
}

interface GetState {
(
path: string | string[] | Record<string, string>,
options?: {
target?: string;
watch?: boolean;
filterUntouchedErrors?: boolean;
}
): any;
}

interface SetErrors<V> {
(
errors?: Errors<V> | ((previousErrors: Errors<V>) => Errors<V> | undefined)
): void;
}

interface SetFieldError {
(name: string, error?: any | ((previousError?: any) => any)): void;
}

type ValuesArg<V> = V | ((previousValues: V) => V);

interface SetValues<V> {
(
values: ValuesArg<V>,
options?: {
shouldValidate?: boolean;
touchedFields?: string[];
dirtyFields?: string[];
}
): void;
}

interface SetFieldValue {
(
name: string,
value: any | ((previousValue: any) => any),
options?: {
[k in "shouldValidate" | "shouldTouched" | "shouldDirty"]?: boolean;
}
): void;
}

interface ValidateForm<V> {
(): Promise<Errors<V>>;
}

interface ValidateField<V> {
(name: string): Promise<Errors<V>>;
}

interface Reset<V> {
(
values?: ValuesArg<V> | null,
exclude?: (keyof FormState<V>)[] | null,
event?: SyntheticEvent<any>
): void;
}

interface Submit<V> {
(event?: SyntheticEvent<any>): Promise<{ values?: V; errors?: Errors<V> }>;
}

interface Parse<V = any, R = any> {
(value: V): R;
}

type Format<V = any, R = any> = Parse<V, R>;

interface OnChange<E = any> {
(event: E, value?: any): void;
}

interface OnBlur {
(event: FocusEvent<any>): void;
}

interface Controller<V = FormValues, E = any> {
(
name: string,
options?: {
validate?: FieldValidator<V>;
value?: any;
defaultValue?: any;
parse?: Parse;
format?: Format;
onChange?: OnChange<E>;
onBlur?: OnBlur;
}
): {
name: string;
value: any;
onChange: (event: E) => void;
onBlur: OnBlur;
} | void;
}

interface Config<V = FormValues> {
defaultValues: V;
validate?: FormValidator<V>;
validateOnChange?: boolean;
validateOnBlur?: boolean;
ignoreFields?: string[];
onReset?: OnReset<V>;
onSubmit?: OnSubmit<V>;
onError?: OnError<V>;
debug?: Debug<V>;
}

interface Return<V = FormValues> {
form: RefObject<HTMLFormElement>;
field: FieldRef<V>;
getState: GetState;
setErrors: SetErrors<V>;
setFieldError: SetFieldError;
setValues: SetValues<V>;
setFieldValue: SetFieldValue;
validateForm: ValidateForm<V>;
validateField: ValidateField<V>;
reset: Reset<V>;
submit: Submit<V>;
controller: Controller<V>;
}

const useForm: <V extends FormValues = FormValues>(
config: Config<V>
) => Return<V>;

const get: (object: any, path: string, defaultValue?: unknown) => any;

const set: (
object: any,
path: string,
value: unknown,
immutable?: boolean
) => any;

const unset: (object: any, path: string, immutable?: boolean) => any;
```
✨ Pretty easy right? `react-cool-form` is more powerful than you think. Let's [explore it](https://react-cool-form.netlify.app)!

## Milestone

Expand Down

0 comments on commit 5b2347c

Please sign in to comment.