Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/ApiPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import setMultipleErrors from "./codeExamples/setMultipleErrors"
import setAllErrors from "./codeExamples/setAllErrors"
import resetCodeControlled from "./codeExamples/resetCodeControlled"
import resetController from "./codeExamples/resetController"
import resetControllerTs from "./codeExamples/resetControllerTs"
import control from "./codeExamples/control"
import UseFieldArray from "./UseFieldArray"
import ValidationResolver from "./ValidationResolver"
Expand Down Expand Up @@ -699,6 +700,8 @@ function ApiPage({ formData, defaultLang, api }: Props) {
<CodeArea
rawData={resetController}
url="https://codesandbox.io/s/react-hook-form-controller-079xx"
tsRawData={resetControllerTs}
tsUrl="https://codesandbox.io/s/react-hook-form-controller-ts-wr80q"
/>
</TabGroup>
</section>
Expand Down
3 changes: 3 additions & 0 deletions src/components/GetStartedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
applyValidation,
applyValidationTs,
controlledComponent,
controlledComponentTs,
} from "./codeExamples/getStarted"
import LearnMore from "../components/learnMore"
import Footer from "../components/Footer"
Expand Down Expand Up @@ -269,6 +270,8 @@ const Faq = ({ location, defaultLang, getStarted }: Props) => {
<CodeArea
rawData={controlledComponent}
url="https://codesandbox.io/s/react-hook-form-controller-079xx"
tsRawData={controlledComponentTs}
tsUrl="https://codesandbox.io/s/react-hook-form-controller-ts-wr80q"
/>

<h2
Expand Down
3 changes: 3 additions & 0 deletions src/components/V5/ApiPageV5.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import setMultipleErrors from "../V5/codeExamples/setMultipleErrors"
import setAllErrors from "../V5/codeExamples/setAllErrors"
import resetCodeControlled from "../V5/codeExamples/resetCodeControlled"
import resetController from "../V5/codeExamples/resetController"
import resetControllerTs from "../V5/codeExamples/resetControllerTs"
import control from "../V5/codeExamples/control"
import nativeValidation from "../V5/codeExamples/nativeValidation"
import UseFieldArray from "../UseFieldArray"
Expand Down Expand Up @@ -775,6 +776,8 @@ function ApiPage({ formData, defaultLang, api }: Props) {
<CodeArea
rawData={resetController}
url="https://codesandbox.io/s/react-hook-form-controller-079xx"
tsRawData={resetControllerTs}
tsUrl="https://codesandbox.io/s/react-hook-form-controller-ts-v5-g2gmc"
/>
<CodeArea
rawData={resetCodeControlled}
Expand Down
44 changes: 44 additions & 0 deletions src/components/V5/codeExamples/resetControllerTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export default `import React from "react";
import { useForm, Controller } from "react-hook-form";
import { TextField } from "@material-ui/core";

interface IFormInputs {
firstName: string
lastName: string
}

export default function App() {
const { register, handleSubmit, reset, setValue, control } = useForm<IFormInputs>();
const onSubmit = (data: IFormInputs) => console.log(data);

return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
as={TextField}
name="firstName"
control={control}
rules={ required: true }
defaultValue=""
/>
<Controller
as={TextField}
name="lastName"
control={control}
defaultValue=""
/>

<input type="submit" />
<input type="button" onClick={reset} />
<input
type="button"
onClick={() => {
reset({
firstName: "bill",
lastName: "luo"
});
}}
/>
</form>
);
}
`
33 changes: 33 additions & 0 deletions src/components/codeExamples/getStarted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,39 @@ function App() {
}
`

export const controlledComponentTs = `import React from "react";
import { useForm, Controller } from "react-hook-form";
import ReactSelect from "react-select";
import { TextField, Checkbox } from "@material-ui/core";

interface IFormInputs {
TextField: string
MyCheckbox: boolean
}

function App() {
const methods = useForm<IFormInputs>();
const { handleSubmit, control, reset } = methods;
const onSubmit = (data: IFormInputs) => console.log(data);

return (
<form onSubmit={handleSubmit(onSubmit)}>
{/* Option 1: pass a component to the Controller. */}
<Controller as={TextField} name="TextField" control={control} defaultValue="" />

{/* Option 2: use render props to assign events and value */}
<Controller
name="MyCheckbox"
control={control}
defaultValue={false}
rules={{ required: true }}
render=(props => <Checkbox {...props} />) // props contains: onChange, onBlur and value
/>
</form>
);
}
`

export const globalState = `import React from "react";
import { useForm } from "react-hook-form";
import { connect } from "react-redux";
Expand Down
44 changes: 44 additions & 0 deletions src/components/codeExamples/resetControllerTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export default `import React from "react";
import { useForm, Controller } from "react-hook-form";
import { TextField } from "@material-ui/core";

interface IFormInputs {
firstName: string
lastName: string
}

export default function App() {
const { register, handleSubmit, reset, setValue, control } = useForm<IFormInputs>();
const onSubmit = (data: IFormInputs) => console.log(data);

return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
as={TextField}
name="firstName"
control={control}
rules={ required: true }
defaultValue=""
/>
<Controller
as={TextField}
name="lastName"
control={control}
defaultValue=""
/>

<input type="submit" />
<input type="button" onClick={reset} />
<input
type="button"
onClick={() => {
reset({
firstName: "bill",
lastName: "luo"
});
}}
/>
</form>
);
}
`