generated from sripwoud/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add form and query example pages
- Loading branch information
Showing
17 changed files
with
170 additions
and
79 deletions.
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
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
import type { FieldApi } from '@tanstack/react-form' | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: This is a generic component | ||
export function FieldInfo({ field }: { field: FieldApi<any, any, any, any> }) { | ||
return ( | ||
<> | ||
{field.state.meta.isTouched && field.state.meta.errors.length | ||
? <em style={{ color: 'red' }}>{field.state.meta.errors.join(', ')}</em> | ||
: null} | ||
</> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function NotFound() { | ||
return <h1>404 - Not Found</h1> | ||
} |
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,58 @@ | ||
import { useForm } from '@tanstack/react-form' | ||
import { FieldInfo } from 'c/FieldInfo' | ||
import { capitalize } from 'l/format' | ||
import { type UsernameSchema, usernameSchema } from 'l/schemas' | ||
import type { FormEvent } from 'react' | ||
|
||
export function UsernameForm() { | ||
const form = useForm<UsernameSchema>({ | ||
defaultValues: { | ||
username: '', | ||
} as UsernameSchema, | ||
onSubmit: async ({ value: { username } }) => { | ||
alert(`Submitted - Username: ${username}`) | ||
}, | ||
validators: { onChange: usernameSchema }, | ||
}) | ||
|
||
function handleSubmit(e: FormEvent<HTMLFormElement>) { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
form.handleSubmit() | ||
} | ||
|
||
return ( | ||
<form onSubmit={(e) => handleSubmit(e)}> | ||
<form.Field | ||
name='username' | ||
children={(field) => ( | ||
<> | ||
<label htmlFor={field.name} style={{ marginRight: '4px' }}> | ||
{capitalize(field.name)} | ||
</label> | ||
<input | ||
onChange={(e) => field.handleChange(e.target.value)} | ||
id={field.name} | ||
name={field.name} | ||
type='text' | ||
placeholder={field.name} | ||
value={field.state.value} | ||
style={{ marginRight: '4px' }} | ||
/> | ||
<br /> | ||
<FieldInfo field={field} /> | ||
<br /> | ||
</> | ||
)} | ||
/> | ||
<form.Subscribe | ||
selector={({ canSubmit, isSubmitting }) => [canSubmit, isSubmitting]} | ||
children={([canSubmit, isSubmitting]) => ( | ||
<button type='submit' aria-busy={isSubmitting} disabled={!canSubmit}> | ||
Submit | ||
</button> | ||
)} | ||
/> | ||
</form> | ||
) | ||
} |
Empty file.
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,3 @@ | ||
export function capitalize(str: string) { | ||
return str.charAt(0).toUpperCase() + str.slice(1) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { createRouter } from '@tanstack/react-router' | ||
import { NotFound as defaultNotFoundComponent } from 'components/NotFound' | ||
import { routeTree } from 'routeTree.gen' | ||
|
||
export const router = createRouter({ routeTree }) | ||
export const router = createRouter({ defaultNotFoundComponent, routeTree }) |
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,5 @@ | ||
import { z } from 'zod' | ||
import { usernameSchema as username } from './username' | ||
|
||
export const usernameSchema = z.object({ username }) | ||
export type UsernameSchema = z.infer<typeof usernameSchema> |
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,7 @@ | ||
import { z } from 'zod' | ||
|
||
const USERNAME_REGEX = /^[a-zA-Z0-9_]{1,20}$/ | ||
|
||
export const usernameSchema = z.string().regex(USERNAME_REGEX, { | ||
message: 'Invalid username (should be alphanumeric and between 1 and 20 characters, `_` is allowed)', | ||
}) |
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 was deleted.
Oops, something went wrong.
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,4 @@ | ||
import { createLazyFileRoute } from '@tanstack/react-router' | ||
import { UsernameForm as component } from 'c/UsernameForm' | ||
|
||
export const Route = createLazyFileRoute('/form')({ component }) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { createLazyFileRoute } from '@tanstack/react-router' | ||
import { Latom as component } from 'c/Latom' | ||
|
||
export const Route = createLazyFileRoute('/state')({ | ||
component, | ||
}) |
This file was deleted.
Oops, something went wrong.