您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

query-variable.tsx 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { SelectWithSearch } from '@/components/originui/select-with-search';
  2. import {
  3. FormControl,
  4. FormField,
  5. FormItem,
  6. FormLabel,
  7. FormMessage,
  8. } from '@/components/ui/form';
  9. import { toLower } from 'lodash';
  10. import { ReactNode, useMemo } from 'react';
  11. import { useFormContext } from 'react-hook-form';
  12. import { useTranslation } from 'react-i18next';
  13. import { VariableType } from '../../constant';
  14. import { useBuildQueryVariableOptions } from '../../hooks/use-get-begin-query';
  15. type QueryVariableProps = {
  16. name?: string;
  17. type?: VariableType;
  18. label?: ReactNode;
  19. };
  20. export function QueryVariable({
  21. name = 'query',
  22. type,
  23. label,
  24. }: QueryVariableProps) {
  25. const { t } = useTranslation();
  26. const form = useFormContext();
  27. const nextOptions = useBuildQueryVariableOptions();
  28. const finalOptions = useMemo(() => {
  29. return type
  30. ? nextOptions.map((x) => {
  31. return {
  32. ...x,
  33. options: x.options.filter((y) => toLower(y.type).includes(type)),
  34. };
  35. })
  36. : nextOptions;
  37. }, [nextOptions, type]);
  38. return (
  39. <FormField
  40. control={form.control}
  41. name={name}
  42. render={({ field }) => (
  43. <FormItem>
  44. {label || (
  45. <FormLabel tooltip={t('chat.modelTip')}>
  46. {t('flow.query')}
  47. </FormLabel>
  48. )}
  49. <FormControl>
  50. <SelectWithSearch
  51. options={finalOptions}
  52. {...field}
  53. allowClear
  54. ></SelectWithSearch>
  55. </FormControl>
  56. <FormMessage />
  57. </FormItem>
  58. )}
  59. />
  60. );
  61. }