Aslam97 / shadcn-minimal-tiptap

Minimal Tiptap Editor
https://shadcn-minimal-tiptap.vercel.app
MIT License
837 stars 40 forks source link

Using with react-hook-form doesn't allow for resetting of field #67

Open ZacMelendez opened 21 hours ago

ZacMelendez commented 21 hours ago

First, thank you so much for creating this repo, the RTE has been super easy to set up and use, and I love it's integration with the ShadCN design system.

I am running into an issue where I cannot reset it using the regular ShadCN form components. I have recreated a simple example here on Stackblitz, but this is the behavior I am seeing:

https://github.com/user-attachments/assets/3a3fe088-6ede-499a-89ec-16aa816a6255

The code for the test is as follows:

import './App.css';
import { TooltipProvider } from './components/ui/tooltip';
import { MinimalTiptapEditor } from './components/ui/minimal-tiptap';
import { Button } from '@/components/ui/button';
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from '@/components/ui/form';
import { useForm } from 'react-hook-form';
import { Input } from './components/ui/input';

function App() {
  const form = useForm<{ title: string; description: string }>({
    defaultValues: {
      title: '',
      description: '',
    },
  });
  return (
    <>
      <TooltipProvider>
        <Form {...form}>
          <form
            onSubmit={form.handleSubmit(console.log)}
            className="w-full space-y-6"
          >
            <FormField
              control={form.control}
              name="title"
              render={({ field }) => (
                <FormItem>
                  <FormLabel className="sr-only">Title</FormLabel>
                  <FormControl>
                    <Input
                      {...field}
                      className={'w-full'}
                      placeholder="Type your title here..."
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
            <FormField
              control={form.control}
              name="description"
              render={({ field }) => (
                <FormItem>
                  <FormLabel className="sr-only">Description</FormLabel>
                  <FormControl>
                    <MinimalTiptapEditor
                      {...field}
                      throttleDelay={0}
                      className={'w-full'}
                      editorContentClassName="some-class"
                      output="html"
                      placeholder="Type your description here..."
                      autofocus={true}
                      immediatelyRender={true}
                      editable={true}
                      injectCSS={true}
                      editorClassName="focus:outline-none p-5"
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
            <div className="w-full flex flex-row gap-3">
              <Button
                type="button"
                onClick={() => {
                  form.reset({ title: '', description: '' });
                }}
                size="lg"
                className="w-full"
              >
                Reset
              </Button>
              <Button type="submit" size="lg" className="w-full">
                Submit
              </Button>
            </div>
          </form>
        </Form>
      </TooltipProvider>
    </>
  );
}

export default App;

Is there something else I need to do to ensure it clears out with form.reset(...)?

Aslam97 commented 17 hours ago

@ZacMelendez Glad you find this repo helpful!. There is an example on how to reset here https://github.com/Aslam97/shadcn-minimal-tiptap/blob/main/src/App.tsx