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

[Docs] Update next-forms example #40284

Merged
merged 5 commits into from Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions examples/next-forms/.eslintrc.json
@@ -1,3 +1,4 @@
{
"root": true,
"extends": "next/core-web-vitals"
}
1 change: 1 addition & 0 deletions examples/next-forms/next.config.js
@@ -1,3 +1,4 @@
/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
}
11 changes: 7 additions & 4 deletions examples/next-forms/package.json
Expand Up @@ -8,11 +8,14 @@
},
"dependencies": {
"next": "latest",
"react": "17.0.2",
"react-dom": "17.0.2"
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"eslint": "8.4.1",
"eslint-config-next": "latest"
"@types/node": "18.7.15",
"@types/react": "18.0.18",
"eslint": "8.23.0",
"eslint-config-next": "latest",
"typescript": "4.8.2"
}
}
7 changes: 0 additions & 7 deletions examples/next-forms/pages/_app.js

This file was deleted.

6 changes: 6 additions & 0 deletions examples/next-forms/pages/_app.tsx
@@ -0,0 +1,6 @@
import type { AppProps } from 'next/app'
import '../styles/globals.css'

export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
@@ -1,4 +1,13 @@
export default function handler(req, res) {
import type { NextApiRequest, NextApiResponse } from 'next'

type ResponseData = {
data: string
}

export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
const body = req.body
console.log('body: ', body)

Expand Down
Expand Up @@ -3,7 +3,7 @@ import Image from 'next/image'
import Link from 'next/link'
import styles from '../styles/Home.module.css'

export default function Home() {
export default function IndexPage() {
return (
<div className={styles.container}>
<Head>
Expand Down
@@ -1,25 +1,26 @@
import Link from 'next/link'
import { FormEvent } from 'react'
import styles from '../styles/Home.module.css'

export default function PageWithJSbasedForm() {
// Handle the submit event on form submit.
const handleSubmit = async (event) => {
const handleSubmit = async (event: FormEvent) => {
// Stop the form from submitting and refreshing the page.
event.preventDefault()

// Cast the event target to an html form
const form = event.target as HTMLFormElement

// Get data from the form.
const data = {
first: event.target.first.value,
last: event.target.last.value,
first: form.first.value as string,
last: form.last.value as string,
}

const JSONdata = JSON.stringify(data)

// Send the form data to our API and get a response.
const response = await fetch('/api/form', {
// Body of the request is the JSON data we created above.
body: JSONdata,

body: JSON.stringify(data),
// Tell the server we're sending JSON.
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -53,7 +54,6 @@ export default function PageWithJSbasedForm() {
<input type="text" id="first" name="first" required />
<label htmlFor="last">Last Name</label>
<input type="text" id="last" name="last" required />

<button type="submit">Submit</button>
</form>
</div>
Expand Down
Expand Up @@ -16,16 +16,18 @@ export default function Form() {
<code className={styles.code}>pages/no-js-form.js</code>
</p>

{/*action: The action attribute defines where the data gets sent. Its value must be a valid relative or absolute URL. If this attribute isn't provided, the data will be sent to the URL of the page containing the form — the current page.
method: The HTTP method to submit the form with. (case insensitive) s*/}

{/*
* action: The action attribute defines where the data gets sent.
* Its value must be a valid relative or absolute URL.
* If this attribute isn't provided, the data will be sent to the URL
* of the page containing the form — the current page.
* method: The HTTP method to submit the form with. (case insensitive)
*/}
<form action="/api/form" method="post">
<label htmlFor="first">First Name</label>
<input type="text" id="first" name="first" required />

<label htmlFor="last">Last Name</label>
<input type="text" id="last" name="last" required />

<button type="submit">Submit</button>
</form>
</div>
Expand Down
20 changes: 20 additions & 0 deletions examples/next-forms/tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"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"]
}