|
| 1 | +import { useErrorsLngChange } from '@/hooks/use-errors-lng-change' |
| 2 | +import { TFlowInput, useFlowInputSchema } from '@/lib/schema/flow-input' |
| 3 | +import { isStringBoolean, isStringNumber, toBoolean } from '@/utils' |
| 4 | +import { zodResolver } from '@hookform/resolvers/zod' |
| 5 | +import { toNumber } from 'lodash' |
| 6 | +import { X } from 'lucide-react' |
| 7 | +import { useCallback } from 'react' |
| 8 | +import { useFieldArray, useForm } from 'react-hook-form' |
| 9 | +import { useTranslation } from 'react-i18next' |
| 10 | +import { |
| 11 | + Button, |
| 12 | + Form, |
| 13 | + FormControl, |
| 14 | + FormField, |
| 15 | + FormItem, |
| 16 | + FormMessage, |
| 17 | + Input, |
| 18 | + Label, |
| 19 | + Select, |
| 20 | + SelectContent, |
| 21 | + SelectItem, |
| 22 | + SelectTrigger, |
| 23 | + SelectValue, |
| 24 | +} from '../ui' |
| 25 | + |
| 26 | +type Props = { |
| 27 | + id?: string |
| 28 | + onSubmit?: (data: TFlowInput) => void |
| 29 | + defaultValues?: TFlowInput |
| 30 | +} |
| 31 | + |
| 32 | +const parseVariableValue = (value: string) => { |
| 33 | + if (isStringNumber(value)) return toNumber(value) |
| 34 | + if (isStringBoolean(value)) return toBoolean(value) |
| 35 | + return value |
| 36 | +} |
| 37 | + |
| 38 | +export const VariablesSettingForm = ({ |
| 39 | + id = 'variables-setting-form', |
| 40 | + onSubmit, |
| 41 | + defaultValues, |
| 42 | +}: Props) => { |
| 43 | + const { t } = useTranslation(['forms', 'common']) |
| 44 | + const schema = useFlowInputSchema() |
| 45 | + const form = useForm<TFlowInput>({ |
| 46 | + resolver: zodResolver(schema), |
| 47 | + mode: 'onChange', |
| 48 | + defaultValues: { |
| 49 | + ...defaultValues, |
| 50 | + }, |
| 51 | + }) |
| 52 | + const { |
| 53 | + fields: variables, |
| 54 | + append, |
| 55 | + prepend, |
| 56 | + remove, |
| 57 | + swap, |
| 58 | + move, |
| 59 | + insert, |
| 60 | + } = useFieldArray({ |
| 61 | + control: form.control, |
| 62 | + name: 'variables', // unique name for your Field Array |
| 63 | + }) |
| 64 | + |
| 65 | + useErrorsLngChange(form) |
| 66 | + |
| 67 | + const handleSubmit = (data: TFlowInput) => { |
| 68 | + if (onSubmit) { |
| 69 | + onSubmit({ |
| 70 | + ...data, |
| 71 | + variables: data.variables?.map((variable) => ({ |
| 72 | + ...variable, |
| 73 | + value: parseVariableValue(variable.value), |
| 74 | + })), |
| 75 | + }) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const handleAddVariable = useCallback(() => { |
| 80 | + form.trigger() |
| 81 | + |
| 82 | + append({ name: '', value: '', type: 'string' }) |
| 83 | + }, [append, form]) |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className='space-y-3'> |
| 87 | + <div className='space-y-2 max-h-[25rem] overflow-y-auto pb-1 overflow-x-auto hidden-scroll pl-[2px]'> |
| 88 | + <Label> |
| 89 | + <span>{t('common:variables')}</span> |
| 90 | + </Label> |
| 91 | + <Form {...form}> |
| 92 | + <form |
| 93 | + className='space-y-3 ' |
| 94 | + id={id} |
| 95 | + onSubmit={form.handleSubmit(handleSubmit)} |
| 96 | + > |
| 97 | + {variables.length > 0 ? ( |
| 98 | + variables.map((variable, index) => { |
| 99 | + return ( |
| 100 | + <div key={variable.id} className='flex gap-3 w-full'> |
| 101 | + <FormField |
| 102 | + name={`variables.${index}.name`} |
| 103 | + control={form.control} |
| 104 | + render={({ field }) => { |
| 105 | + return ( |
| 106 | + <FormItem className='w-full'> |
| 107 | + <FormControl> |
| 108 | + <Input |
| 109 | + {...field} |
| 110 | + autoComplete='off' |
| 111 | + placeholder={t('variable_name.placeholder')} |
| 112 | + /> |
| 113 | + </FormControl> |
| 114 | + <FormMessage /> |
| 115 | + </FormItem> |
| 116 | + ) |
| 117 | + }} |
| 118 | + /> |
| 119 | + <FormField |
| 120 | + name={`variables.${index}.value`} |
| 121 | + control={form.control} |
| 122 | + render={({ field }) => { |
| 123 | + return ( |
| 124 | + <FormItem className='w-full'> |
| 125 | + <FormControl> |
| 126 | + <Input |
| 127 | + {...field} |
| 128 | + autoComplete='off' |
| 129 | + placeholder={t('variable_value.placeholder')} |
| 130 | + /> |
| 131 | + </FormControl> |
| 132 | + <FormMessage /> |
| 133 | + </FormItem> |
| 134 | + ) |
| 135 | + }} |
| 136 | + /> |
| 137 | + <FormField |
| 138 | + name={`variables.${index}.type`} |
| 139 | + control={form.control} |
| 140 | + render={({ field }) => { |
| 141 | + return ( |
| 142 | + <FormItem className='w-24 flex-shrink-0'> |
| 143 | + <Select |
| 144 | + onValueChange={field.onChange} |
| 145 | + defaultValue={field.value} |
| 146 | + > |
| 147 | + <FormControl> |
| 148 | + <SelectTrigger> |
| 149 | + <SelectValue |
| 150 | + placeholder={t('variable_type.placeholder')} |
| 151 | + /> |
| 152 | + </SelectTrigger> |
| 153 | + </FormControl> |
| 154 | + <SelectContent> |
| 155 | + {['string', 'number', 'boolean'].map((type) => ( |
| 156 | + <SelectItem key={type} value={type}> |
| 157 | + {type} |
| 158 | + </SelectItem> |
| 159 | + ))} |
| 160 | + </SelectContent> |
| 161 | + </Select> |
| 162 | + <FormMessage /> |
| 163 | + </FormItem> |
| 164 | + ) |
| 165 | + }} |
| 166 | + /> |
| 167 | + <Button |
| 168 | + className='p-0 w-9 h-9 flex-shrink-0' |
| 169 | + variant='destructive' |
| 170 | + type='button' |
| 171 | + onClick={() => remove(index)} |
| 172 | + > |
| 173 | + <X /> |
| 174 | + </Button> |
| 175 | + </div> |
| 176 | + ) |
| 177 | + }) |
| 178 | + ) : ( |
| 179 | + <span className='text-sm text-muted-foreground block text-center py-2'> |
| 180 | + Not have any variable, click button below to add new variable. |
| 181 | + </span> |
| 182 | + )} |
| 183 | + </form> |
| 184 | + </Form> |
| 185 | + </div> |
| 186 | + <Button onClick={handleAddVariable} className='w-full'> |
| 187 | + Add new variable |
| 188 | + </Button> |
| 189 | + </div> |
| 190 | + ) |
| 191 | +} |
| 192 | + |
| 193 | +export default VariablesSettingForm |
0 commit comments