How to clear inputs on successful submission #169
-
I want to clear my input fields on successful submission, as described here. For that, I need access to the form ref, which I don't have, as I am using What would be the proper way to go about this? |
Beta Was this translation helpful? Give feedback.
Answered by
edmundhung
Jun 6, 2023
Replies: 2 comments 5 replies
-
Hi @MoSattler, you can pass your own ref to function Example() {
const ref = useRef<HTMLFormElement>(null);
const [form, { email, password }] = useForm({
ref,
});
// `form.ref / form.props.ref` will now be the same as `ref`
return (
<form {...form.props}>
{/* ... */}
</form>
);
} I should add this to the docs. Thanks for the question! |
Beta Was this translation helpful? Give feedback.
4 replies
-
Any idea why this isn't working with useFetcher? export function ChatEditor({ chat }: { chat?: ChatWithMessages | null }) {
const chatFetcher = useFetcher<typeof action>()
const isPending = chatFetcher.state !== 'idle'
const [form, fields] = useForm({
id: 'chat-editor',
constraint: getFieldsetConstraint(ChatEditorSchema),
lastSubmission:
chatFetcher?.data?.status !== 'success'
? chatFetcher.data?.submission
: undefined,
onValidate({ formData }) {
return parse(formData, { schema: ChatEditorSchema })
},
defaultValue: {
title: chat?.title ?? '',
content: '',
},
})
return (
<div>
<Form
method="post"
className="flex h-full flex-col gap-y-4 overflow-y-auto overflow-x-hidden px-10 pt-12"
{...form.props}
encType="multipart/form-data"
>
<AuthenticityTokenInput />
<button type="submit" className="hidden" />
{chat ? <input type="hidden" name="id" value={chat.id} /> : null}
<div className="flex w-full items-end gap-1">
<TextareaFieldWithSubmit
className="w-full"
labelProps={{ children: 'Content' }}
textareaProps={{
...conform.textarea(fields.content, { ariaAttributes: true }),
}}
errors={fields.content.errors}
isPending={isPending}
/>
</div>
<ErrorList id={form.errorId} errors={form.errors} />
</Form>
</div>
)
}``` |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like there is a regression from 0.6.2 on how defaultValue is handled 🤦🏼 .
For now, please do this:
This is just a temporary fix. I will make sure this is fixed on the next version.