Replies: 2 comments
-
You can pass the form metadata down and retrieves the fields using export default function LoginForm() {
const lastResult = useActionResult();
const [form, fields] = useForm({
// ...
});
return (
<form method="post" id={form.id} onSubmit={form.onSubmit}>
<FormInternals form={form} />
</form>
);
}
function FormInternals({ form }: { form: FormMetadata<z.input<typeof schema>> }) {
const { pending } = useFormStatus();
const fields = form.getFieldset();
return (
<div>
<label>Username</label>
<input type="text" name={fields.username.name} disabled={pending} />
<div>{fields.username.errors}</div>
</div>
<div>
<label>Password</label>
<input type="password" name={fields.password.name} disabled={pending} />
<div>{fields.password.errors}</div>
</div>
<button disabled={pending}>Login</button>
)
} or, if you are using form context: export default function LoginForm() {
const lastResult = useActionResult();
const [form, fields] = useForm({
// ...
});
return (
<FormProvider context={form.context}>
<form method="post" id={form.id} onSubmit={form.onSubmit}>
<FormInternals name={form.name} />
</form>
</FormProvider>
);
}
function FormInternals({ name }: { name: FormName<z.input<typeof schema>> }) {
const { pending } = useFormStatus();
const [field] = useField(name);
const fields = field.getFieldset();
return (
<div>
<label>Username</label>
<input type="text" name={fields.username.name} disabled={pending} />
<div>{fields.username.errors}</div>
</div>
<div>
<label>Password</label>
<input type="password" name={fields.password.name} disabled={pending} />
<div>{fields.password.errors}</div>
</div>
<button disabled={pending}>Login</button>
)
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
If you are just trying to disable inputs/buttons, this can be made very simple with the convenience of html Any descendant form controls of the const Fieldset = ({ children }) => {
const { pending } = useFormStatus();
return <fieldset disabled={pending}>{children}</fieldset>;
};
export default function LoginForm() {
const [form, { username, password }] = useForm({});
return (
<form method="post" id={form.id} onSubmit={form.onSubmit}>
<Fieldset>
<div>
<label htmlFor={username.id}>Username</label>
{/* No need to set 'disabled' here */}
<input type="text" id={username.id} name={username.name} />
<div>{username.errors}</div>
</div>
<div>
<label htmlFor={password.id}>Password</label>
{/* No need to set 'disabled' here */}
<input id={password.id} type="password" name={password.name} />
<div>{password.errors}</div>
</div>
{/* No need to set 'disabled' here */}
<button type="submit">Login</button>
</Fieldset>
</form>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Describe the bug and the expected behavior
With the new
useFormStatus
hook it requires that the hook not be called within the same component that<form>
lives in due to a pitfallSince
fields
is dynamically generated given the schema, trying to typefields
when it's passed to the new component becomes a bit of a headacheConform version
v1.0.2
Steps to Reproduce the Bug or Issue
What browsers are you seeing the problem on?
No response
Screenshots or Videos
No response
Additional context
No response
Beta Was this translation helpful? Give feedback.
All reactions