Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

index.tsx

66 lines1.7 KB

1import * as React from 'react'
2import { TanStackDevtools } from '@tanstack/react-devtools'
3import { useForm } from '@tanstack/react-form'
4import { formDevtoolsPlugin } from '@tanstack/react-form-devtools'
5import ReactDOM from 'react-dom/client'
6import './index.css'
7import { StringField } from './StringField'
8import { sharedFormOptions } from './sharedForm'
9import { FormSection } from './FormSection'
10import { FormSubmitButton } from './FormSubmitButton'
11​
12function App() {
13 const form = useForm({
14 ...sharedFormOptions,
15 onSubmit: ({ value }) => {
16 // Do something with form data
17 console.log(value)
18 },
19 })
20​
21 return (
22 <div>
23 <h1>Simple Form Example</h1>
24 <form
25 onSubmit={(e) => {
26 e.preventDefault()
27 e.stopPropagation()
28 form.handleSubmit()
29 }}
30 >
31 <form.Field name="firstName">
32 {(field) => <StringField field={field} />}
33 </form.Field>
34 <form.Field name="lastName">
35 {(field) => <StringField field={field} />}
36 </form.Field>
37​
38 <FormSection form={form} />
39​
40 <FormSubmitButton form={form} />
41 <button
42 type="reset"
43 onClick={(e) => {
44 // Avoid unexpected resets of form elements (especially <select> elements)
45 e.preventDefault()
46 form.reset()
47 }}
48 >
49 Reset
50 </button>
51 </form>
52 <TanStackDevtools
53 plugins={[formDevtoolsPlugin()]}
54 config={{
55 hideUntilHover: false,
56 }}
57 />
58 </div>
59 )
61​
62ReactDOM.createRoot(document.getElementById('root')!).render(
63 <React.StrictMode>
64 <App />
65 </React.StrictMode>,