Skip to content

Commit

Permalink
chore(examples): Convert with-react-hook-form example to TypeScript (
Browse files Browse the repository at this point in the history
…#38796)

Converted `with-react-hook-form` example to TypeScript as it is pointed out in the contributing docs that examples
should be converted to TS.

- updated dependencies
- fixed/added types where necessary

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
  • Loading branch information
nramkissoon and balazsorban44 committed Sep 29, 2022
1 parent 4cb96fb commit 2af441c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
12 changes: 9 additions & 3 deletions examples/with-react-hook-form/package.json
Expand Up @@ -7,8 +7,14 @@
},
"dependencies": {
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-hook-form": "7.4.0"
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.33.1"
},
"devDependencies": {
"@types/node": "^18.0.6",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"typescript": "^4.7.4"
}
}
5 changes: 0 additions & 5 deletions examples/with-react-hook-form/pages/_app.js

This file was deleted.

6 changes: 6 additions & 0 deletions examples/with-react-hook-form/pages/_app.tsx
@@ -0,0 +1,6 @@
import { AppProps } from 'next/app'
import '../styles/global.css'

export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
@@ -1,24 +1,34 @@
import { useState } from 'react'
import { useForm } from 'react-hook-form'

interface User {
name: string
}

interface LoginFormValues {
username: string
password: string
remember: boolean
}

const IndexPage = () => {
const [user, setUser] = useState()
const [user, setUser] = useState<User>()
const {
register,
formState: { errors },
handleSubmit,
} = useForm()
const onSubmit = ({ username, password, remember }) => {
} = useForm<LoginFormValues>()
const onSubmit = handleSubmit(({ username, password, remember }) => {
// You should handle login logic with username, password and remember form data
setUser({ name: username })
}
})

return (
<div className="container">
{user ? (
<span className="hello-user">Hello, {user.name}!</span>
) : (
<form onSubmit={handleSubmit(onSubmit)}>
<form onSubmit={onSubmit}>
<div className="row">
<h3 className="form-header">LOGIN</h3>
</div>
Expand Down
20 changes: 20 additions & 0 deletions examples/with-react-hook-form/tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}

0 comments on commit 2af441c

Please sign in to comment.