Skip to content

Commit

Permalink
chore: fix remix example
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Nov 12, 2023
1 parent 78c9ce5 commit ee779f0
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 121 deletions.
54 changes: 26 additions & 28 deletions examples/remix/app/routes/login-fetcher.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConformBoundary, conform, useForm } from '@conform-to/react';
import { conform, useForm } from '@conform-to/react';
import { parse } from '@conform-to/zod';
import type { ActionArgs } from '@remix-run/node';
import { json, redirect } from '@remix-run/node';
Expand All @@ -24,7 +24,7 @@ export async function action({ request }: ActionArgs) {

export default function Login() {
const fetcher = useFetcher<typeof action>();
const { form, fields, context } = useForm({
const { form, fields } = useForm({
// Sync the result of last submission
lastResult: fetcher.data,

Expand All @@ -37,33 +37,31 @@ export default function Login() {
});

return (
<ConformBoundary context={context}>
<fetcher.Form method="post" {...conform.form(form)}>
<fetcher.Form method="post" {...conform.form(form)}>
<div>
<label>Email</label>
<input
className={!fields.email.valid ? 'error' : ''}
{...conform.input(fields.email)}
/>
<div>{fields.email.errors}</div>
</div>
<div>
<label>Password</label>
<input
className={!fields.password.valid ? 'error' : ''}
{...conform.input(fields.password, { type: 'password' })}
/>
<div>{fields.password.errors}</div>
</div>
<label>
<div>
<label>Email</label>
<input
className={fields.email.error ? 'error' : ''}
{...conform.input(fields.email)}
/>
<div>{fields.email.error}</div>
<span>Remember me</span>
<input {...conform.input(fields.remember, { type: 'checkbox' })} />
</div>
<div>
<label>Password</label>
<input
className={fields.password.error ? 'error' : ''}
{...conform.input(fields.password, { type: 'password' })}
/>
<div>{fields.password.error}</div>
</div>
<label>
<div>
<span>Remember me</span>
<input {...conform.input(fields.remember, { type: 'checkbox' })} />
</div>
</label>
<hr />
<button>Login</button>
</fetcher.Form>
</ConformBoundary>
</label>
<hr />
<button>Login</button>
</fetcher.Form>
);
}
54 changes: 26 additions & 28 deletions examples/remix/app/routes/login.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConformBoundary, conform, useForm } from '@conform-to/react';
import { conform, useForm } from '@conform-to/react';
import { parse } from '@conform-to/zod';
import type { ActionArgs } from '@remix-run/node';
import { json, redirect } from '@remix-run/node';
Expand All @@ -25,7 +25,7 @@ export async function action({ request }: ActionArgs) {
export default function Login() {
// Last submission returned by the server
const lastResult = useActionData<typeof action>();
const { form, context, fields } = useForm({
const { form, fields } = useForm({
// Sync the result of last submission
lastResult,

Expand All @@ -39,33 +39,31 @@ export default function Login() {
});

return (
<ConformBoundary context={context}>
<Form method="post" {...conform.form(form)}>
<Form method="post" {...conform.form(form)}>
<div>
<label>Email</label>
<input
className={!fields.email.valid ? 'error' : ''}
{...conform.input(fields.email)}
/>
<div>{fields.email.errors}</div>
</div>
<div>
<label>Password</label>
<input
className={!fields.password.valid ? 'error' : ''}
{...conform.input(fields.password, { type: 'password' })}
/>
<div>{fields.password.errors}</div>
</div>
<label>
<div>
<label>Email</label>
<input
className={fields.email.error ? 'error' : ''}
{...conform.input(fields.email)}
/>
<div>{fields.email.error}</div>
<span>Remember me</span>
<input {...conform.input(fields.remember, { type: 'checkbox' })} />
</div>
<div>
<label>Password</label>
<input
className={fields.password.error ? 'error' : ''}
{...conform.input(fields.password, { type: 'password' })}
/>
<div>{fields.password.error}</div>
</div>
<label>
<div>
<span>Remember me</span>
<input {...conform.input(fields.remember, { type: 'checkbox' })} />
</div>
</label>
<hr />
<button>Login</button>
</Form>
</ConformBoundary>
</label>
<hr />
<button>Login</button>
</Form>
);
}
62 changes: 30 additions & 32 deletions examples/remix/app/routes/signup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConformBoundary, conform, useForm } from '@conform-to/react';
import { conform, useForm } from '@conform-to/react';
import { parse, refine } from '@conform-to/zod';
import type { ActionArgs } from '@remix-run/node';
import { json, redirect } from '@remix-run/node';
Expand Down Expand Up @@ -73,7 +73,7 @@ export async function action({ request }: ActionArgs) {

export default function Signup() {
const lastResult = useActionData<typeof action>();
const { form, fields, context } = useForm({
const { form, fields } = useForm({
lastResult,
onValidate({ formData }) {
return parse(formData, {
Expand All @@ -85,35 +85,33 @@ export default function Signup() {
});

return (
<ConformBoundary context={context}>
<Form method="post" {...conform.form(form)}>
<label>
<div>Username</div>
<input
className={fields.username.error ? 'error' : ''}
{...conform.input(fields.username)}
/>
<div>{fields.username.error}</div>
</label>
<label>
<div>Password</div>
<input
className={fields.password.error ? 'error' : ''}
{...conform.input(fields.password, { type: 'password' })}
/>
<div>{fields.password.error}</div>
</label>
<label>
<div>Confirm Password</div>
<input
className={fields.confirmPassword.error ? 'error' : ''}
{...conform.input(fields.confirmPassword, { type: 'password' })}
/>
<div>{fields.confirmPassword.error}</div>
</label>
<hr />
<button>Signup</button>
</Form>
</ConformBoundary>
<Form method="post" {...conform.form(form)}>
<label>
<div>Username</div>
<input
className={!fields.username.valid ? 'error' : ''}
{...conform.input(fields.username)}
/>
<div>{fields.username.errors}</div>
</label>
<label>
<div>Password</div>
<input
className={!fields.password.valid ? 'error' : ''}
{...conform.input(fields.password, { type: 'password' })}
/>
<div>{fields.password.errors}</div>
</label>
<label>
<div>Confirm Password</div>
<input
className={!fields.confirmPassword.valid ? 'error' : ''}
{...conform.input(fields.confirmPassword, { type: 'password' })}
/>
<div>{fields.confirmPassword.errors}</div>
</label>
<hr />
<button>Signup</button>
</Form>
);
}
28 changes: 14 additions & 14 deletions examples/remix/app/routes/todos.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Field } from '@conform-to/react';
import {
FormProvider,
useForm,
useFieldset,
useFieldList,
conform,
intent,
ConformBoundary,
} from '@conform-to/react';
import { parse } from '@conform-to/zod';
import type { ActionArgs } from '@remix-run/node';
Expand Down Expand Up @@ -40,9 +40,9 @@ export default function TodoForm() {
const lastResult = useActionData<typeof action>();
const { form, fields, context } = useForm({
lastResult,
onValidate({ formData }) {
return parse(formData, { schema: todosSchema });
},
// onValidate({ formData }) {
// return parse(formData, { schema: todosSchema });
// },
shouldValidate: 'onBlur',
});
const taskList = useFieldList({
Expand All @@ -52,20 +52,20 @@ export default function TodoForm() {
});

return (
<ConformBoundary context={context}>
<FormProvider context={context}>
<Form method="post" {...conform.form(form)}>
<div>
<label>Title</label>
<input
className={fields.title.error ? 'error' : ''}
className={!fields.title.valid ? 'error' : ''}
{...conform.input(fields.title)}
/>
<div>{fields.title.error}</div>
<div>{fields.title.errors}</div>
</div>
<hr />
<div className="form-error">{fields.tasks.error}</div>
<div className="form-error">{fields.tasks.errors}</div>
{taskList.map((task, index) => (
<p key={task.key}>
<div key={task.key}>
<TaskFieldset
title={`Task #${index + 1}`}
name={task.name}
Expand All @@ -87,7 +87,7 @@ export default function TodoForm() {
>
Clear
</button>
</p>
</div>
))}
<button
{...intent.list.insert(fields.tasks, {
Expand All @@ -99,7 +99,7 @@ export default function TodoForm() {
<hr />
<button>Save</button>
</Form>
</ConformBoundary>
</FormProvider>
);
}

Expand All @@ -118,16 +118,16 @@ function TaskFieldset({ title, name, formId }: TaskFieldsetProps) {
<div>
<label>{title}</label>
<input
className={fields.content.error ? 'error' : ''}
className={!fields.content.valid ? 'error' : ''}
{...conform.input(fields.content)}
/>
<div>{fields.content.error}</div>
<div>{fields.content.errors}</div>
</div>
<div>
<label>
<span>Completed</span>
<input
className={fields.completed.error ? 'error' : ''}
className={!fields.completed.valid ? 'error' : ''}
{...conform.input(fields.completed, {
type: 'checkbox',
})}
Expand Down
7 changes: 6 additions & 1 deletion packages/conform-dom/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,19 @@ export function createForm<Type extends Record<string, unknown> = any>(
const cache: Record<string, string | undefined> = {};
const keyProxy = new Proxy(key, {
get(_, name: string) {
// This makes getSerializedState() returning the raw key instead
if (name === 'toJSON') {
return () => key;
}

if (typeof cache[name] === 'undefined') {
const currentKey = key[name] ?? '';
const resultKey =
name === ''
? currentKey
: `${
keyProxy[formatPaths(getPaths(name).slice(0, -1))] ?? ''
}${currentKey}`;
}/${currentKey}`;

if (resultKey) {
cache[name] = resultKey;
Expand Down
5 changes: 3 additions & 2 deletions packages/conform-react/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
STATE,
} from '@conform-to/dom';
import {
type ReactElement,
type ReactNode,
type MutableRefObject,
createContext,
Expand Down Expand Up @@ -84,10 +85,10 @@ export function useFormContext(
return result;
}

export function FormContextProvider(props: {
export function FormProvider(props: {
context: Form;
children: ReactNode;
}): ReactNode {
}): ReactElement {
const context = useContext(Context);
const value = useMemo(
() => ({ ...context, [props.context.id]: props.context }),
Expand Down
2 changes: 1 addition & 1 deletion packages/conform-react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
export {
type Field,
type FieldMetadata as FieldConfig,
FormContextProvider,
FormProvider,
FormStateInput,
} from './context';
export {
Expand Down
Loading

0 comments on commit ee779f0

Please sign in to comment.