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 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { FormContainer } from '@/components/form-container';
  2. import { SelectWithSearch } from '@/components/originui/select-with-search';
  3. import { TopNFormField } from '@/components/top-n-item';
  4. import {
  5. Form,
  6. FormControl,
  7. FormField,
  8. FormItem,
  9. FormLabel,
  10. FormMessage,
  11. } from '@/components/ui/form';
  12. import { useTranslate } from '@/hooks/common-hooks';
  13. import { zodResolver } from '@hookform/resolvers/zod';
  14. import { memo } from 'react';
  15. import { useForm, useFormContext } from 'react-hook-form';
  16. import { z } from 'zod';
  17. import { initialWikipediaValues } from '../../constant';
  18. import { useFormValues } from '../../hooks/use-form-values';
  19. import { useWatchFormChange } from '../../hooks/use-watch-form-change';
  20. import { INextOperatorForm } from '../../interface';
  21. import { LanguageOptions } from '../../options';
  22. import { buildOutputList } from '../../utils/build-output-list';
  23. import { FormWrapper } from '../components/form-wrapper';
  24. import { Output } from '../components/output';
  25. import { QueryVariable } from '../components/query-variable';
  26. export const WikipediaFormPartialSchema = {
  27. top_n: z.string(),
  28. language: z.string(),
  29. };
  30. const FormSchema = z.object({
  31. query: z.string(),
  32. ...WikipediaFormPartialSchema,
  33. });
  34. export function WikipediaFormWidgets() {
  35. const { t } = useTranslate('common');
  36. const form = useFormContext();
  37. return (
  38. <>
  39. <TopNFormField></TopNFormField>
  40. <FormField
  41. control={form.control}
  42. name="language"
  43. render={({ field }) => (
  44. <FormItem>
  45. <FormLabel>{t('language')}</FormLabel>
  46. <FormControl>
  47. <SelectWithSearch {...field} options={LanguageOptions} />
  48. </FormControl>
  49. <FormMessage />
  50. </FormItem>
  51. )}
  52. />
  53. </>
  54. );
  55. }
  56. const outputList = buildOutputList(initialWikipediaValues.outputs);
  57. function WikipediaForm({ node }: INextOperatorForm) {
  58. const defaultValues = useFormValues(initialWikipediaValues, node);
  59. const form = useForm<z.infer<typeof FormSchema>>({
  60. defaultValues,
  61. resolver: zodResolver(FormSchema),
  62. });
  63. useWatchFormChange(node?.id, form);
  64. return (
  65. <Form {...form}>
  66. <FormWrapper>
  67. <FormContainer>
  68. <QueryVariable></QueryVariable>
  69. <WikipediaFormWidgets></WikipediaFormWidgets>
  70. </FormContainer>
  71. </FormWrapper>
  72. <div className="p-5">
  73. <Output list={outputList}></Output>
  74. </div>
  75. </Form>
  76. );
  77. }
  78. export default memo(WikipediaForm);