You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { NodeProps, NodeResizeControl } from '@xyflow/react';
  2. import {
  3. Form,
  4. FormControl,
  5. FormField,
  6. FormItem,
  7. FormMessage,
  8. } from '@/components/ui/form';
  9. import { Input } from '@/components/ui/input';
  10. import { Textarea } from '@/components/ui/textarea';
  11. import { INoteNode } from '@/interfaces/database/flow';
  12. import { zodResolver } from '@hookform/resolvers/zod';
  13. import { NotebookPen } from 'lucide-react';
  14. import { memo } from 'react';
  15. import { useForm } from 'react-hook-form';
  16. import { useTranslation } from 'react-i18next';
  17. import { z } from 'zod';
  18. import { NodeWrapper } from '../node-wrapper';
  19. import { ResizeIcon, controlStyle } from '../resize-icon';
  20. import { useWatchFormChange, useWatchNameFormChange } from './use-watch-change';
  21. const FormSchema = z.object({
  22. text: z.string(),
  23. });
  24. const NameFormSchema = z.object({
  25. name: z.string(),
  26. });
  27. function NoteNode({ data, id, selected }: NodeProps<INoteNode>) {
  28. const { t } = useTranslation();
  29. const form = useForm<z.infer<typeof FormSchema>>({
  30. resolver: zodResolver(FormSchema),
  31. defaultValues: data.form,
  32. });
  33. const nameForm = useForm<z.infer<typeof NameFormSchema>>({
  34. resolver: zodResolver(NameFormSchema),
  35. defaultValues: { name: data.name },
  36. });
  37. useWatchFormChange(id, form);
  38. useWatchNameFormChange(id, nameForm);
  39. return (
  40. <NodeWrapper
  41. className="p-0 w-full h-full flex flex-col"
  42. selected={selected}
  43. >
  44. <NodeResizeControl minWidth={190} minHeight={128} style={controlStyle}>
  45. <ResizeIcon />
  46. </NodeResizeControl>
  47. <section className="p-2 flex gap-2 bg-background-note items-center note-drag-handle rounded-t">
  48. <NotebookPen className="size-4" />
  49. <Form {...nameForm}>
  50. <form className="flex-1">
  51. <FormField
  52. control={nameForm.control}
  53. name="name"
  54. render={({ field }) => (
  55. <FormItem className="h-full">
  56. <FormControl>
  57. <Input
  58. placeholder={t('flow.notePlaceholder')}
  59. {...field}
  60. type="text"
  61. className="bg-transparent border-none focus-visible:outline focus-visible:outline-text-sub-title"
  62. />
  63. </FormControl>
  64. <FormMessage />
  65. </FormItem>
  66. )}
  67. />
  68. </form>
  69. </Form>
  70. </section>
  71. <Form {...form}>
  72. <form className="flex-1 p-1">
  73. <FormField
  74. control={form.control}
  75. name="text"
  76. render={({ field }) => (
  77. <FormItem className="h-full">
  78. <FormControl>
  79. <Textarea
  80. placeholder={t('flow.notePlaceholder')}
  81. className="resize-none rounded-none p-1 h-full overflow-auto bg-transparent focus-visible:ring-0 border-none"
  82. {...field}
  83. />
  84. </FormControl>
  85. <FormMessage />
  86. </FormItem>
  87. )}
  88. />
  89. </form>
  90. </Form>
  91. </NodeWrapper>
  92. );
  93. }
  94. export default memo(NoteNode);