mirror of
https://github.com/LukeHagar/form.git
synced 2025-12-09 12:27:44 +00:00
65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
import React from "react";
|
|
import ReactDOM from "react-dom/client";
|
|
import { useForm } from "@tanstack/react-form";
|
|
|
|
export default function App() {
|
|
const form = useForm({
|
|
// Memoize your default values to prevent re-renders
|
|
defaultValues: React.useMemo(
|
|
() => ({
|
|
firstName: "",
|
|
lastName: "",
|
|
}),
|
|
[]
|
|
),
|
|
onSubmit: (values) => {
|
|
// Do something with form data
|
|
},
|
|
});
|
|
|
|
return (
|
|
<div>
|
|
<h1>Simple Form Example</h1>
|
|
{/* A pre-bound form component */}
|
|
<form.Form>
|
|
<div>
|
|
{/* A type-safe and pre-bound field component*/}
|
|
<form.Field
|
|
name="firstName"
|
|
validate={(value) => !value && "A first name is required"}
|
|
children={(field) => (
|
|
// Avoid hasty abstractions. Render props are great!
|
|
<>
|
|
<label htmlFor={field.name}>First Name:</label>
|
|
<input name={field.name} {...field.getInputProps()} />
|
|
</>
|
|
)}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<form.Field
|
|
name="lastName"
|
|
children={(field) => (
|
|
<>
|
|
<label htmlFor={field.name}>Last Name:</label>
|
|
<input name={field.name} {...field.getInputProps()} />
|
|
</>
|
|
)}
|
|
/>
|
|
</div>
|
|
<form.Subscribe
|
|
selector={(state) => [state.canSubmit, state.isSubmitting]}
|
|
children={([canSubmit, isSubmitting]) => (
|
|
<button type="submit" disabled={!canSubmit}>
|
|
{isSubmitting ? "..." : "Submit"}
|
|
</button>
|
|
)}
|
|
/>
|
|
</form.Form>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const rootElement = document.getElementById("root");
|
|
ReactDOM.createRoot(rootElement).render(<App />);
|