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.

begin-dynamic-options.tsx 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use client';
  2. import { BlockButton, Button } from '@/components/ui/button';
  3. import {
  4. FormControl,
  5. FormField,
  6. FormItem,
  7. FormMessage,
  8. } from '@/components/ui/form';
  9. import { Input } from '@/components/ui/input';
  10. import { X } from 'lucide-react';
  11. import { useFieldArray, useFormContext } from 'react-hook-form';
  12. import { useTranslation } from 'react-i18next';
  13. export function BeginDynamicOptions() {
  14. const { t } = useTranslation();
  15. const form = useFormContext();
  16. const name = 'options';
  17. const { fields, remove, append } = useFieldArray({
  18. name: name,
  19. control: form.control,
  20. });
  21. return (
  22. <div className="space-y-5">
  23. {fields.map((field, index) => {
  24. const typeField = `${name}.${index}.value`;
  25. return (
  26. <div key={field.id} className="flex items-center gap-2">
  27. <FormField
  28. control={form.control}
  29. name={typeField}
  30. render={({ field }) => (
  31. <FormItem className="flex-1">
  32. <FormControl>
  33. <Input
  34. {...field}
  35. placeholder={t('common.pleaseInput')}
  36. ></Input>
  37. </FormControl>
  38. <FormMessage />
  39. </FormItem>
  40. )}
  41. />
  42. <Button variant={'ghost'} onClick={() => remove(index)}>
  43. <X className="text-text-sub-title-invert " />
  44. </Button>
  45. </div>
  46. );
  47. })}
  48. <BlockButton onClick={() => append({ value: '' })} type="button">
  49. {t('flow.addVariable')}
  50. </BlockButton>
  51. </div>
  52. );
  53. }