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

Commit

Permalink
Fix(useForm): conditional form not working
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Jan 19, 2021
1 parent 4eea46b commit 1d6d0ae
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 178 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-rivers-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

Fix(useForm): conditional form not working
32 changes: 27 additions & 5 deletions app/src/Playground/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
import { useState } from "react";
import { useForm } from "react-cool-form";

export default () => {
const Form = () => {
const [show, setShow] = useState(true);
const { form } = useForm({
defaultValues: { test: "test" },
onSubmit: (values, { formState }) =>
console.log("onSubmit: ", formState.values),
});

return (
<form ref={form}>
<input name="test" />
<input type="submit" />
</form>
<>
<button type="button" onClick={() => setShow(!show)}>
Internal Toggle
</button>
{show && (
<form ref={form}>
<input name="test" />
<input type="submit" />
</form>
)}
</>
);
};

export default () => {
const [show, setShow] = useState(true);

return (
<>
<button type="button" onClick={() => setShow(!show)}>
External Toggle
</button>
{show && <Form />}
</>
);
};
4 changes: 2 additions & 2 deletions docs/api-reference/use-form.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ An `object` with the following methods:

### form

`React.RefObject`
`(element: HTMLFormElement) => void`

A React [ref](https://reactjs.org/docs/hooks-reference.html#useref) that allows you to [integrate a form with React Cool Form](./../getting-started/integration-an-existing-form).
This method allows us to [integrate a form with React Cool Form](./../getting-started/integration-an-existing-form).

### field

Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/integration-an-existing-form.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: Integration an Existing Form

## Hook into A Form

To use React Cool Form, we just need to attach the [form](../api-reference/use-form#form) to the target element via the `ref` attribute. It acts as a **delegator** that will take care of all the values of [input](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input), [select](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select), and [textarea](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea) for us.
To use React Cool Form, we just need to attach the [form](../api-reference/use-form#form) method to the target element via the `ref` attribute. It acts as a **delegator** that will take care of all the values of [input](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input), [select](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select), and [textarea](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea) for us.

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

Expand Down
1 change: 0 additions & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as useState } from "./useState";
export { default as useLatest } from "./useLatest";
export { default as useIsoLayoutEffect } from "./useIsoLayoutEffect";
13 changes: 0 additions & 13 deletions src/hooks/useIsoLayoutEffect-ssr.test.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/hooks/useIsoLayoutEffect.test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/hooks/useIsoLayoutEffect.ts

This file was deleted.

23 changes: 19 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FocusEvent, MutableRefObject, RefObject, SyntheticEvent } from "react";
import { FocusEvent, MutableRefObject, SyntheticEvent } from "react";

// Common
export type Map = Record<string, boolean>;
Expand Down Expand Up @@ -46,6 +46,17 @@ export interface FormStateReturn<V> {
// Form
export type FormValues = Record<string, any>;

interface Handler {
(event: Event): void;
}

export type Handlers = Partial<{
change: Handler;
blur: Handler;
submit: Handler;
reset: Handler;
}>;

export type FieldElement =
| HTMLInputElement
| HTMLTextAreaElement
Expand Down Expand Up @@ -107,11 +118,15 @@ interface FormValidator<V> {
(values: V): FormErrors<V> | void | Promise<FormErrors<V> | void>;
}

export interface RegisterForm {
(element: HTMLFormElement | null): void;
}

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

export interface FieldRef<V> {
export interface RegisterField<V> {
(
validateOrOptions:
| FieldValidator<V>
Expand Down Expand Up @@ -231,8 +246,8 @@ export type Config<V> = Partial<{
}>;

export interface Return<V> {
form: RefObject<HTMLFormElement>;
field: FieldRef<V>;
form: RegisterForm;
field: RegisterField<V>;
getState: GetState;
setValue: SetValue;
setTouched: SetTouched;
Expand Down
12 changes: 8 additions & 4 deletions src/types/react-cool-form.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module "react-cool-form" {
import { FocusEvent, RefObject, SyntheticEvent } from "react";
import { FocusEvent, SyntheticEvent } from "react";

type DeepProps<V, T = any> = {
[K in keyof V]?: V[K] extends T ? T : DeepProps<V[K]>;
Expand Down Expand Up @@ -54,7 +54,11 @@ declare module "react-cool-form" {
(name?: string | string[]): void;
}

interface FieldRef<V> {
interface RegisterForm {
(element: HTMLFormElement | null): void;
}

interface RegisterField<V> {
(
validateOrOptions:
| FieldValidator<V>
Expand Down Expand Up @@ -200,8 +204,8 @@ declare module "react-cool-form" {
}>;

export interface Return<V = FormValues> {
form: RefObject<HTMLFormElement>;
field: FieldRef<V>;
form: RegisterForm;
field: RegisterField<V>;
getState: GetState;
setValue: SetValue;
setTouched: SetTouched;
Expand Down
Loading

0 comments on commit 1d6d0ae

Please sign in to comment.