Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(examples): Convert with-react-hook-form example to TypeScript #38796

Merged
merged 4 commits into from Sep 29, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"]
}