-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from SeasonedSoftware/imperative-submit
Imperative submit
- Loading branch information
Showing
4 changed files
with
106 additions
and
2 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 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,70 @@ | ||
import hljs from 'highlight.js/lib/common' | ||
import type { | ||
ActionFunction, | ||
LoaderFunction, | ||
MetaFunction, | ||
} from '@remix-run/node' | ||
import { formAction } from '~/formAction' | ||
import { z } from 'zod' | ||
import Form from '~/ui/form' | ||
import { metaTags } from '~/helpers' | ||
import { makeDomainFunction } from 'domain-functions' | ||
import Example from '~/ui/example' | ||
|
||
const title = 'Imperative submit' | ||
const description = | ||
'In this example, we trigger a submit as soon as the user enters 4 characters.' | ||
|
||
export const meta: MetaFunction = () => metaTags({ title, description }) | ||
|
||
const code = `const schema = z.object({ token: z.string().length(4) }) | ||
export default () => ( | ||
<Form schema={schema}> | ||
{({ Field, Errors, submit }) => ( | ||
<> | ||
<Field | ||
name="token" | ||
onChange={(ev: React.ChangeEvent<HTMLInputElement>) => { | ||
if (ev.target.value.length === 4) submit() | ||
}} | ||
/> | ||
<Errors /> | ||
</> | ||
)} | ||
</Form> | ||
)` | ||
|
||
const schema = z.object({ | ||
token: z.string().length(4), | ||
}) | ||
|
||
export const loader: LoaderFunction = () => ({ | ||
code: hljs.highlight(code, { language: 'ts' }).value, | ||
}) | ||
|
||
const mutation = makeDomainFunction(schema)(async (values) => values) | ||
|
||
export const action: ActionFunction = async ({ request }) => | ||
formAction({ request, schema, mutation }) | ||
|
||
export default function Component() { | ||
return ( | ||
<Example title={title} description={description}> | ||
<Form schema={schema}> | ||
{({ Field, Errors, submit }) => ( | ||
<> | ||
<Field | ||
name="token" | ||
placeholder="Type 4 digits" | ||
onChange={(ev: React.ChangeEvent<HTMLInputElement>) => { | ||
if (ev.target.value.length === 4) submit() | ||
}} | ||
/> | ||
<Errors /> | ||
</> | ||
)} | ||
</Form> | ||
</Example> | ||
) | ||
} |
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,20 @@ | ||
import { expect } from '@playwright/test' | ||
import { test } from 'tests/setup/tests' | ||
|
||
const route = '/examples/forms/imperative-submit' | ||
|
||
test('With JS enabled', async ({ example }) => { | ||
const { page } = example | ||
const token = example.field('token') | ||
|
||
await page.goto(route) | ||
|
||
await token.input.first().type('123') | ||
expect(page.locator('#action-data > pre')).toBeHidden() | ||
|
||
await token.input.first().type('4') | ||
|
||
await example.expectData({ | ||
token: '1234', | ||
}) | ||
}) |
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