選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

popover-form.tsx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {
  2. Form,
  3. FormControl,
  4. FormField,
  5. FormItem,
  6. FormMessage,
  7. } from '@/components/ui/form';
  8. import { Input } from '@/components/ui/input';
  9. import { Popover, PopoverContent } from '@/components/ui/popover';
  10. import { useParseDocument } from '@/hooks/document-hooks';
  11. import { IModalProps } from '@/interfaces/common';
  12. import { zodResolver } from '@hookform/resolvers/zod';
  13. import { PropsWithChildren } from 'react';
  14. import { useForm } from 'react-hook-form';
  15. import { useTranslation } from 'react-i18next';
  16. import { z } from 'zod';
  17. const reg =
  18. /^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/;
  19. const FormSchema = z.object({
  20. url: z.string(),
  21. result: z.any(),
  22. });
  23. const values = {
  24. url: '',
  25. result: null,
  26. };
  27. export const PopoverForm = ({
  28. children,
  29. visible,
  30. switchVisible,
  31. }: PropsWithChildren<IModalProps<any>>) => {
  32. const form = useForm({
  33. defaultValues: values,
  34. resolver: zodResolver(FormSchema),
  35. });
  36. const { parseDocument, loading } = useParseDocument();
  37. const { t } = useTranslation();
  38. // useResetFormOnCloseModal({
  39. // form,
  40. // visible,
  41. // });
  42. async function onSubmit(values: z.infer<typeof FormSchema>) {
  43. const val = values.url;
  44. if (reg.test(val)) {
  45. const ret = await parseDocument(val);
  46. if (ret?.data?.code === 0) {
  47. form.setValue('result', ret?.data?.data);
  48. }
  49. }
  50. }
  51. const content = (
  52. <Form {...form}>
  53. <form onSubmit={form.handleSubmit(onSubmit)}>
  54. <FormField
  55. control={form.control}
  56. name={`url`}
  57. render={({ field }) => (
  58. <FormItem className="flex-1">
  59. <FormControl>
  60. <Input
  61. {...field}
  62. // onPressEnter={(e) => e.preventDefault()}
  63. placeholder={t('flow.pasteFileLink')}
  64. // suffix={
  65. // <Button
  66. // type="primary"
  67. // onClick={onOk}
  68. // size={'small'}
  69. // loading={loading}
  70. // >
  71. // {t('common.submit')}
  72. // </Button>
  73. // }
  74. />
  75. </FormControl>
  76. <FormMessage />
  77. </FormItem>
  78. )}
  79. />
  80. <FormField
  81. control={form.control}
  82. name={`result`}
  83. render={() => <></>}
  84. />
  85. </form>
  86. </Form>
  87. );
  88. return (
  89. <Popover open={visible} onOpenChange={switchVisible}>
  90. {children}
  91. <PopoverContent>{content}</PopoverContent>
  92. </Popover>
  93. );
  94. };