-
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.
- Loading branch information
1 parent
7277b3e
commit 1c7e0aa
Showing
3 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
apps/web/app/routes/test-examples/field-with-autocomplete.tsx
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,117 @@ | ||
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 = 'Field with autocomplete' | ||
const description = | ||
'In this example, we pass a autocomplete prop to a field without children.' | ||
|
||
export const meta: MetaFunction = () => metaTags({ title, description }) | ||
|
||
const schema = z.object({ | ||
name: z.string().min(1), | ||
code: z.number().int().optional(), | ||
organization: z.string().optional(), | ||
organizationTitle: z.enum(['director', 'manager', 'employee']), | ||
streetAddress: z.string().optional(), | ||
country: z.string().optional(), | ||
postalCode: z.string().optional(), | ||
addressLine1: z.string().optional(), | ||
}) | ||
|
||
export const loader: LoaderFunction = () => ({ | ||
code: hljs.highlight('', { 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, Button }) => ( | ||
<> | ||
{/* Text field */} | ||
<Field name="name" autoComplete="name" /> | ||
{/* Numeric field */} | ||
<Field name="code"> | ||
{({ Label, Input, Errors }) => ( | ||
<> | ||
<Label>Code</Label> | ||
<Input type="number" autoComplete="one-time-code" /> | ||
<Errors /> | ||
</> | ||
)} | ||
</Field> | ||
{/* Select field */} | ||
<Field name="organizationTitle" autoComplete="organization-title" /> | ||
{/* Textarea field */} | ||
<Field | ||
name="streetAddress" | ||
multiline | ||
autoComplete="street-address" | ||
/> | ||
{/* Text field child */} | ||
<Field name="organization" autoComplete="organization"> | ||
{({ Label, Input, Errors }) => ( | ||
<> | ||
<Label>Organization</Label> | ||
<Input autoComplete="organization" /> | ||
<Errors /> | ||
</> | ||
)} | ||
</Field> | ||
{/* Smart input child*/} | ||
<Field name="country"> | ||
{({ Label, SmartInput, Errors }) => ( | ||
<> | ||
<Label>Country</Label> | ||
<SmartInput autoComplete="country" /> | ||
<Errors /> | ||
</> | ||
)} | ||
</Field> | ||
{/* Select child */} | ||
<Field name="postalCode"> | ||
{({ Label, Select, Errors }) => ( | ||
<> | ||
<Label>Postal code</Label> | ||
<Select autoComplete="postal-code"> | ||
<option value="12345">12345</option> | ||
<option value="67890">67890</option> | ||
</Select> | ||
<Errors /> | ||
</> | ||
)} | ||
</Field> | ||
{/* Textarea child */} | ||
<Field name="addressLine1"> | ||
{({ Label, Multiline, Errors }) => ( | ||
<> | ||
<Label>Postal code</Label> | ||
<Multiline autoComplete="address-line1" /> | ||
|
||
<Errors /> | ||
</> | ||
)} | ||
</Field> | ||
<Errors /> | ||
<Button /> | ||
</> | ||
)} | ||
</Form> | ||
</Example> | ||
) | ||
} |
39 changes: 39 additions & 0 deletions
39
apps/web/tests/examples/forms/field-with-autocomplete.spec.ts
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,39 @@ | ||
import { test, expect } from 'tests/setup/tests' | ||
|
||
const route = '/test-examples/field-with-autocomplete' | ||
|
||
test('With JS enabled', async ({ example }) => { | ||
const { page } = example | ||
|
||
const name = example.field('name') | ||
const code = example.field('code') | ||
const organizationTitle = example.field('organizationTitle') | ||
const organization = example.field('organization') | ||
const streetAddress = example.field('streetAddress') | ||
const country = example.field('country') | ||
const postalCode = example.field('postalCode') | ||
const addressLine1 = example.field('addressLine1') | ||
|
||
await page.goto(route) | ||
|
||
await expect(name.input).toHaveAttribute('autocomplete', 'name') | ||
await expect(code.input).toHaveAttribute('autocomplete', 'one-time-code') | ||
await expect(organizationTitle.input).toHaveAttribute( | ||
'autocomplete', | ||
'organization-title', | ||
) | ||
await expect(streetAddress.input).toHaveAttribute( | ||
'autocomplete', | ||
'street-address', | ||
) | ||
await expect(organization.input).toHaveAttribute( | ||
'autocomplete', | ||
'organization', | ||
) | ||
await expect(country.input).toHaveAttribute('autocomplete', 'country') | ||
await expect(postalCode.input).toHaveAttribute('autocomplete', 'postal-code') | ||
await expect(addressLine1.input).toHaveAttribute( | ||
'autocomplete', | ||
'address-line1', | ||
) | ||
}) |
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