-
-
Notifications
You must be signed in to change notification settings - Fork 610
feat(react-from): extend appform #2106
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7519997
feat(react-from): extend appform
harry-whorlow e79b4f2
chore: update to api
harry-whorlow 4a49d2e
docs: form composition update
harry-whorlow 94d3397
chore: test
harry-whorlow 6323166
fix: lock file
harry-whorlow 02b63cc
chore: pr comments
harry-whorlow 11f51c2
chore: changeset
harry-whorlow f6733e4
ci: apply automated fixes and generate docs
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/react-form': minor | ||
| --- | ||
|
|
||
| Adds extension method to AppForm allowing for teams to extend upstream AppForms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // @ts-check | ||
|
|
||
| /** @type {import('eslint').Linter.Config} */ | ||
| const config = { | ||
| extends: ['plugin:react/recommended', 'plugin:react-hooks/recommended'], | ||
| rules: { | ||
| 'react/no-children-prop': 'off', | ||
| }, | ||
| } | ||
|
|
||
| module.exports = config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| pnpm-lock.yaml | ||
| yarn.lock | ||
| package-lock.json | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Example | ||
|
|
||
| To run this example: | ||
|
|
||
| - `npm install` | ||
| - `npm run dev` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <link rel="icon" type="image/svg+xml" href="/emblem-light.svg" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <meta name="theme-color" content="#000000" /> | ||
|
|
||
| <title>TanStack Form React Simple Example App</title> | ||
| </head> | ||
| <body> | ||
| <noscript>You need to enable JavaScript to run this app.</noscript> | ||
| <div id="root"></div> | ||
| <script type="module" src="/src/index.tsx"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "name": "@tanstack/form-example-react-composition", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite --port=3001", | ||
| "build": "vite build", | ||
| "preview": "vite preview", | ||
| "test:types": "tsc" | ||
| }, | ||
| "dependencies": { | ||
| "@tanstack/react-form": "^1.28.6", | ||
| "react": "^19.0.0", | ||
| "react-dom": "^19.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@tanstack/react-devtools": "^0.9.7", | ||
| "@tanstack/react-form-devtools": "^0.2.20", | ||
| "@types/react": "^19.0.7", | ||
| "@types/react-dom": "^19.0.3", | ||
| "@vitejs/plugin-react": "^5.1.1", | ||
| "vite": "^7.2.2" | ||
| }, | ||
| "browserslist": { | ||
| "production": [ | ||
| ">0.2%", | ||
| "not dead", | ||
| "not op_mini all" | ||
| ], | ||
| "development": [ | ||
| "last 1 chrome version", | ||
| "last 1 firefox version", | ||
| "last 1 safari version" | ||
| ] | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { createFormHook, createFormHookContexts } from '@tanstack/react-form' | ||
|
|
||
| // | ||
| // fields | ||
| // | ||
| import { TextField } from './FieldComponents/TextField' | ||
|
|
||
| // | ||
| // components | ||
| // | ||
| import { SubmitButton } from './FormComponents/SubmitButton' | ||
|
|
||
| export const { fieldContext, formContext, useFieldContext, useFormContext } = | ||
| createFormHookContexts() | ||
harry-whorlow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const { useAppForm } = createFormHook({ | ||
| fieldContext, | ||
| formContext, | ||
| fieldComponents: { TextField: TextField }, | ||
| formComponents: { SubmitButton }, | ||
| }) | ||
|
|
||
| export default useAppForm | ||
23 changes: 23 additions & 0 deletions
23
examples/react/composition/src/AppForm/FieldComponents/TextField.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { useFieldContext } from '../AppForm' | ||
|
|
||
| export function TextField({ label }: { label: string }) { | ||
| // The `Field` infers that it should have a `value` type of `string` | ||
| const field = useFieldContext<string>() | ||
| return ( | ||
| <label> | ||
| <span>{label}</span> | ||
| <input | ||
| value={field.state.value} | ||
| onChange={(e) => field.handleChange(e.target.value)} | ||
| onBlur={() => field.handleBlur()} | ||
| /> | ||
harry-whorlow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| <> | ||
| {field.state.meta.isTouched && !field.state.meta.isValid ? ( | ||
| <em>{field.state.meta.errors.join(',')}</em> | ||
| ) : null} | ||
| {field.state.meta.isValidating ? 'Validating...' : null} | ||
| </> | ||
| </label> | ||
| ) | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
examples/react/composition/src/AppForm/FormComponents/SubmitButton.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { useFormContext } from '../AppForm' | ||
|
|
||
| export function SubmitButton({ label }: { label: string }) { | ||
| const form = useFormContext() | ||
| return ( | ||
| <form.Subscribe selector={(state) => state.isSubmitting}> | ||
| {(isSubmitting) => ( | ||
| <button type="submit" disabled={isSubmitting}> | ||
| {label} | ||
| </button> | ||
| )} | ||
| </form.Subscribe> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import * as React from 'react' | ||
| import { createRoot } from 'react-dom/client' | ||
|
|
||
| import { TanStackDevtools } from '@tanstack/react-devtools' | ||
| import { formDevtoolsPlugin } from '@tanstack/react-form-devtools' | ||
|
|
||
| import useAppForm from './AppForm/AppForm' | ||
|
|
||
| export default function App() { | ||
| const form = useAppForm({ | ||
| defaultValues: { | ||
| firstName: '', | ||
| lastName: '', | ||
| }, | ||
| onSubmit: async ({ value }) => { | ||
| // Do something with form data | ||
| console.log(value) | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <h1>Simple Form Example</h1> | ||
|
|
||
| <form | ||
| onSubmit={(e) => { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
| form.handleSubmit() | ||
| }} | ||
| > | ||
| {/* A type-safe field component*/} | ||
| <form.AppField | ||
| name="firstName" | ||
| validators={{ | ||
| onChange: ({ value }) => | ||
| !value | ||
| ? 'A first name is required' | ||
| : value.length < 3 | ||
| ? 'First name must be at least 3 characters' | ||
| : undefined, | ||
| onChangeAsyncDebounceMs: 500, | ||
| onChangeAsync: async ({ value }) => { | ||
| await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
| return ( | ||
| value.includes('error') && 'No "error" allowed in first name' | ||
| ) | ||
| }, | ||
| }} | ||
| > | ||
| {(f) => <f.TextField label="first name" />} | ||
| </form.AppField> | ||
harry-whorlow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| <form.AppField name="lastName"> | ||
| {(f) => <f.TextField label="last name" />} | ||
| </form.AppField> | ||
|
|
||
| <form.AppForm> | ||
| <form.SubmitButton label="save" /> | ||
| </form.AppForm> | ||
| </form> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const rootElement = document.getElementById('root')! | ||
|
|
||
| createRoot(rootElement).render( | ||
| <React.StrictMode> | ||
| <App /> | ||
|
|
||
| <TanStackDevtools | ||
| config={{ hideUntilHover: true }} | ||
| plugins={[formDevtoolsPlugin()]} | ||
| /> | ||
| </React.StrictMode>, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ESNext", | ||
| "lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
| "module": "ESNext", | ||
| "skipLibCheck": true, | ||
|
|
||
| /* Bundler mode */ | ||
| "moduleResolution": "Bundler", | ||
| "allowImportingTsExtensions": true, | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "noEmit": true, | ||
| "jsx": "react-jsx", | ||
|
|
||
| /* Linting */ | ||
| "strict": true, | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noFallthroughCasesInSwitch": true | ||
| }, | ||
| "include": ["src"] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.