import React, { useState } from 'react' import type { FC } from 'react' import { useTranslation } from 'react-i18next' import { RiBookOpenLine } from '@remixicon/react' import type { CreateExternalAPIReq, FormSchema } from '../declarations' import Input from '@/app/components/base/input' import cn from '@/utils/classnames' type FormProps = { className?: string itemClassName?: string fieldLabelClassName?: string value: CreateExternalAPIReq onChange: (val: CreateExternalAPIReq) => void formSchemas: FormSchema[] inputClassName?: string } const Form: FC = React.memo(({ className, itemClassName, fieldLabelClassName, value, onChange, formSchemas, inputClassName, }) => { const { t, i18n } = useTranslation() const [changeKey, setChangeKey] = useState('') const handleFormChange = (key: string, val: string) => { setChangeKey(key) if (key === 'name') { onChange({ ...value, [key]: val }) } else { onChange({ ...value, settings: { ...value.settings, [key]: val, }, }) } } const renderField = (formSchema: FormSchema) => { const { variable, type, label, required } = formSchema const fieldValue = variable === 'name' ? value[variable] : (value.settings[variable as keyof typeof value.settings] || '') return (
{variable === 'endpoint' && ( {t('dataset.externalAPIPanelDocumentation')} )}
handleFormChange(variable, val.target.value)} required={required} className={cn(inputClassName)} />
) } return (
{formSchemas.map(formSchema => renderField(formSchema))}
) }) export default Form