-
-
Notifications
You must be signed in to change notification settings - Fork 357
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
[WIP] Shared Component Library Docs #825
Draft
crutchcorn
wants to merge
8
commits into
main
Choose a base branch
from
demo-internal-components
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
56438b5
chore: WIP
crutchcorn bd31494
chore: getting closer...
crutchcorn 673d1a1
chore: move working demo to custom-component-wrapper
crutchcorn 3f80000
chore: move exported types to right location
crutchcorn 0bd9576
chore: make usage look nicer
crutchcorn 29fa99b
Merge branch 'main' into demo-internal-components
lachlancollins bb26093
Fix pnpm-lock
lachlancollins 2adadbf
Merge branch 'main' into demo-internal-components
lachlancollins 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 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 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 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 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 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,34 @@ | ||
{ | ||
"name": "@tanstack/form-example-react-custom-component-wrapper", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite --port=3001", | ||
"build": "vite build", | ||
"preview": "vite preview", | ||
"test:types": "tsc" | ||
}, | ||
"dependencies": { | ||
"@tanstack/react-form": "^0.25.3", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^18.3.3", | ||
"@types/react-dom": "^18.3.0", | ||
"@vitejs/plugin-react": "^4.3.0", | ||
"vite": "^5.1.4" | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"development": [ | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/react/custom-component-wrapper/public/emblem-light.svg
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 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,132 @@ | ||
import * as React from 'react' | ||
import { createRoot } from 'react-dom/client' | ||
import { useForm } from '@tanstack/react-form' | ||
import type { | ||
DeepKeyValueName, | ||
FieldOptions, | ||
ReactFormApi, | ||
} from '@tanstack/react-form' | ||
|
||
/** | ||
* Export these components to your design system or a dedicated component location | ||
*/ | ||
interface TextInputFieldProps< | ||
TFormData extends unknown, | ||
TName extends DeepKeyValueName<TFormData, string>, | ||
> extends FieldOptions<TFormData, TName> { | ||
form: ReactFormApi<TFormData, any> | ||
// Your custom props | ||
label: string | ||
} | ||
|
||
function TextInputField< | ||
TFormData extends unknown, | ||
TName extends DeepKeyValueName<TFormData, string>, | ||
>({ form, name, label, ...fieldProps }: TextInputFieldProps<TFormData, TName>) { | ||
return ( | ||
// Manually type-cast form.Field to work around this issue: | ||
// https://twitter.com/crutchcorn/status/1809827621485900049 | ||
<form.Field<TName, any, any> | ||
{...fieldProps} | ||
name={name} | ||
children={(field) => { | ||
return ( | ||
<div style={{ marginBottom: '1rem' }}> | ||
<div> | ||
<label htmlFor={field.name}>{label}</label> | ||
</div> | ||
<input | ||
id={field.name} | ||
name={field.name} | ||
value={field.state.value} | ||
onBlur={field.handleBlur} | ||
onChange={(e) => field.handleChange(e.target.value)} | ||
/> | ||
{field.state.meta.isValidating ? ( | ||
<div style={{ color: 'gray' }}>Validating...</div> | ||
) : null} | ||
{field.state.meta.isTouched && field.state.meta.errors.length ? ( | ||
<div style={{ color: 'red' }}> | ||
{field.state.meta.errors.join(', ')} | ||
</div> | ||
) : null} | ||
</div> | ||
) | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
function SubmitButton({ form }: { form: ReactFormApi<any, any> }) { | ||
return ( | ||
<form.Subscribe | ||
selector={(state) => [state.canSubmit, state.isSubmitting]} | ||
children={([canSubmit, isSubmitting]) => ( | ||
<> | ||
<button type="submit" disabled={!canSubmit}> | ||
{isSubmitting ? '...' : 'Submit'} | ||
</button> | ||
</> | ||
)} | ||
/> | ||
) | ||
} | ||
|
||
/** | ||
* Then use it in your application | ||
*/ | ||
export default function App() { | ||
const form = useForm({ | ||
defaultValues: { | ||
firstName: '', | ||
age: 0, | ||
}, | ||
onSubmit: async ({ value }) => { | ||
// Do something with form data | ||
console.log(value) | ||
}, | ||
}) | ||
|
||
return ( | ||
<div> | ||
<h1>Wrapped Fields Form Example</h1> | ||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
form.handleSubmit() | ||
}} | ||
> | ||
{/* A type-safe, wrapped field component*/} | ||
<TextInputField | ||
label={'First name:'} | ||
form={form} | ||
name="firstName" | ||
validators={{ | ||
onChangeAsync: async ({ value }) => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
if (value.length < 3) { | ||
return 'Name must be at least 3 characters long' | ||
} | ||
return undefined | ||
}, | ||
}} | ||
/> | ||
{/* Correctly throws a warning when the wrong data type is passed */} | ||
<TextInputField label={'Age:'} form={form} name="age" /> | ||
<SubmitButton form={form} /> | ||
<button type="reset" onClick={() => form.reset()}> | ||
Reset | ||
</button> | ||
</form> | ||
</div> | ||
) | ||
} | ||
|
||
const rootElement = document.getElementById('root')! | ||
|
||
createRoot(rootElement).render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
) |
This file contains 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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"jsx": "react", | ||
"noEmit": true, | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"lib": ["DOM", "DOM.Iterable", "ES2020"] | ||
} | ||
} |
This file contains 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 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does anyone know why
extends unknown
constrainsfield.name
tostring
instead ofstring | number
?@typescript-eslint/no-unnecessary-type-constraint
and other sources I've found claim that an unconstrained generic type parameter already defaults tounknown
, but this must not be true. Maybe some TS wizard will say "oh that's the extends unknown trick it fixes ..." 😅Before I found this issue I had some of the implementation done myself but couldn't work out why
DeepKeys
doesn't excludenumber
, among other TS issues.It's also a shame that
form.Field<TName, any, string>
doesn't work, because the types offield.state.value
andfield.handleChange
are exactly where it would be useful to have TypeScript help.