Form.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { useState } from 'react'
  2. import type { FC } from 'react'
  3. import { ValidatingTip } from '../../key-validator/ValidateStatus'
  4. import type {
  5. CredentialFormSchema,
  6. CredentialFormSchemaNumberInput,
  7. CredentialFormSchemaRadio,
  8. CredentialFormSchemaSecretInput,
  9. CredentialFormSchemaSelect,
  10. CredentialFormSchemaTextInput,
  11. FormValue,
  12. } from '../declarations'
  13. import { FormTypeEnum } from '../declarations'
  14. import { useLanguage } from '../hooks'
  15. import Input from './Input'
  16. import cn from '@/utils/classnames'
  17. import { SimpleSelect } from '@/app/components/base/select'
  18. import Tooltip from '@/app/components/base/tooltip'
  19. import Radio from '@/app/components/base/radio'
  20. type FormProps = {
  21. className?: string
  22. itemClassName?: string
  23. fieldLabelClassName?: string
  24. value: FormValue
  25. onChange: (val: FormValue) => void
  26. formSchemas: CredentialFormSchema[]
  27. validating: boolean
  28. validatedSuccess?: boolean
  29. showOnVariableMap: Record<string, string[]>
  30. isEditMode: boolean
  31. readonly?: boolean
  32. inputClassName?: string
  33. isShowDefaultValue?: boolean
  34. fieldMoreInfo?: (payload: CredentialFormSchema) => JSX.Element | null
  35. }
  36. const Form: FC<FormProps> = ({
  37. className,
  38. itemClassName,
  39. fieldLabelClassName,
  40. value,
  41. onChange,
  42. formSchemas,
  43. validating,
  44. validatedSuccess,
  45. showOnVariableMap,
  46. isEditMode,
  47. readonly,
  48. inputClassName,
  49. isShowDefaultValue = false,
  50. fieldMoreInfo,
  51. }) => {
  52. const language = useLanguage()
  53. const [changeKey, setChangeKey] = useState('')
  54. const handleFormChange = (key: string, val: string | boolean) => {
  55. if (isEditMode && (key === '__model_type' || key === '__model_name'))
  56. return
  57. setChangeKey(key)
  58. const shouldClearVariable: Record<string, string | undefined> = {}
  59. if (showOnVariableMap[key]?.length) {
  60. showOnVariableMap[key].forEach((clearVariable) => {
  61. shouldClearVariable[clearVariable] = undefined
  62. })
  63. }
  64. onChange({ ...value, [key]: val, ...shouldClearVariable })
  65. }
  66. const renderField = (formSchema: CredentialFormSchema) => {
  67. const tooltip = formSchema.tooltip
  68. const tooltipContent = (tooltip && (
  69. <Tooltip
  70. popupContent={
  71. <div className='w-[200px]'>
  72. {tooltip[language] || tooltip.en_US}
  73. </div>}
  74. triggerClassName='ml-1 w-4 h-4'
  75. asChild={false}
  76. />
  77. ))
  78. if (formSchema.type === FormTypeEnum.textInput || formSchema.type === FormTypeEnum.secretInput || formSchema.type === FormTypeEnum.textNumber) {
  79. const {
  80. variable,
  81. label,
  82. placeholder,
  83. required,
  84. show_on,
  85. } = formSchema as (CredentialFormSchemaTextInput | CredentialFormSchemaSecretInput)
  86. if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value))
  87. return null
  88. const disabled = readonly || (isEditMode && (variable === '__model_type' || variable === '__model_name'))
  89. return (
  90. <div key={variable} className={cn(itemClassName, 'py-3')}>
  91. <div className={cn(fieldLabelClassName, 'flex items-center py-2 text-sm text-gray-900')}>
  92. {label[language] || label.en_US}
  93. {
  94. required && (
  95. <span className='ml-1 text-red-500'>*</span>
  96. )
  97. }
  98. {tooltipContent}
  99. </div>
  100. <Input
  101. className={cn(inputClassName, `${disabled && 'cursor-not-allowed opacity-60'}`)}
  102. value={(isShowDefaultValue && ((value[variable] as string) === '' || value[variable] === undefined || value[variable] === null)) ? formSchema.default : value[variable]}
  103. onChange={val => handleFormChange(variable, val)}
  104. validated={validatedSuccess}
  105. placeholder={placeholder?.[language] || placeholder?.en_US}
  106. disabled={disabled}
  107. type={formSchema.type === FormTypeEnum.textNumber ? 'number' : 'text'}
  108. {...(formSchema.type === FormTypeEnum.textNumber ? { min: (formSchema as CredentialFormSchemaNumberInput).min, max: (formSchema as CredentialFormSchemaNumberInput).max } : {})}
  109. />
  110. {fieldMoreInfo?.(formSchema)}
  111. {validating && changeKey === variable && <ValidatingTip />}
  112. </div>
  113. )
  114. }
  115. if (formSchema.type === FormTypeEnum.radio) {
  116. const {
  117. options,
  118. variable,
  119. label,
  120. show_on,
  121. required,
  122. } = formSchema as CredentialFormSchemaRadio
  123. if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value))
  124. return null
  125. const disabled = isEditMode && (variable === '__model_type' || variable === '__model_name')
  126. return (
  127. <div key={variable} className={cn(itemClassName, 'py-3')}>
  128. <div className={cn(fieldLabelClassName, 'flex items-center py-2 text-sm text-gray-900')}>
  129. {label[language] || label.en_US}
  130. {
  131. required && (
  132. <span className='ml-1 text-red-500'>*</span>
  133. )
  134. }
  135. {tooltipContent}
  136. </div>
  137. <div className={`grid grid-cols-${options?.length} gap-3`}>
  138. {
  139. options.filter((option) => {
  140. if (option.show_on.length)
  141. return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)
  142. return true
  143. }).map(option => (
  144. <div
  145. className={`
  146. flex items-center px-3 py-2 rounded-lg border border-gray-100 bg-gray-25 cursor-pointer
  147. ${value[variable] === option.value && 'bg-white border-[1.5px] border-primary-400 shadow-sm'}
  148. ${disabled && '!cursor-not-allowed opacity-60'}
  149. `}
  150. onClick={() => handleFormChange(variable, option.value)}
  151. key={`${variable}-${option.value}`}
  152. >
  153. <div className={`
  154. flex justify-center items-center mr-2 w-4 h-4 border border-gray-300 rounded-full
  155. ${value[variable] === option.value && 'border-[5px] border-primary-600'}
  156. `} />
  157. <div className='text-sm text-gray-900'>{option.label[language] || option.label.en_US}</div>
  158. </div>
  159. ))
  160. }
  161. </div>
  162. {fieldMoreInfo?.(formSchema)}
  163. {validating && changeKey === variable && <ValidatingTip />}
  164. </div>
  165. )
  166. }
  167. if (formSchema.type === 'select') {
  168. const {
  169. options,
  170. variable,
  171. label,
  172. show_on,
  173. required,
  174. placeholder,
  175. } = formSchema as CredentialFormSchemaSelect
  176. if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value))
  177. return null
  178. return (
  179. <div key={variable} className={cn(itemClassName, 'py-3')}>
  180. <div className={cn(fieldLabelClassName, 'flex items-center py-2 text-sm text-gray-900')}>
  181. {label[language] || label.en_US}
  182. {
  183. required && (
  184. <span className='ml-1 text-red-500'>*</span>
  185. )
  186. }
  187. {tooltipContent}
  188. </div>
  189. <SimpleSelect
  190. className={cn(inputClassName)}
  191. disabled={readonly}
  192. defaultValue={(isShowDefaultValue && ((value[variable] as string) === '' || value[variable] === undefined || value[variable] === null)) ? formSchema.default : value[variable]}
  193. items={options.filter((option) => {
  194. if (option.show_on.length)
  195. return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)
  196. return true
  197. }).map(option => ({ value: option.value, name: option.label[language] || option.label.en_US }))}
  198. onSelect={item => handleFormChange(variable, item.value as string)}
  199. placeholder={placeholder?.[language] || placeholder?.en_US}
  200. />
  201. {fieldMoreInfo?.(formSchema)}
  202. {validating && changeKey === variable && <ValidatingTip />}
  203. </div>
  204. )
  205. }
  206. if (formSchema.type === 'boolean') {
  207. const {
  208. variable,
  209. label,
  210. show_on,
  211. required,
  212. } = formSchema as CredentialFormSchemaRadio
  213. if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value))
  214. return null
  215. return (
  216. <div key={variable} className={cn(itemClassName, 'py-3')}>
  217. <div className='flex items-center justify-between py-2 text-sm text-gray-900'>
  218. <div className='flex items-center space-x-2'>
  219. <span className={cn(fieldLabelClassName, 'flex items-center py-2 text-sm text-gray-900')}>{label[language] || label.en_US}</span>
  220. {
  221. required && (
  222. <span className='ml-1 text-red-500'>*</span>
  223. )
  224. }
  225. {tooltipContent}
  226. </div>
  227. <Radio.Group
  228. className='flex items-center'
  229. value={value[variable] === null ? undefined : (value[variable] ? 1 : 0)}
  230. onChange={val => handleFormChange(variable, val === 1)}
  231. >
  232. <Radio value={1} className='!mr-1'>True</Radio>
  233. <Radio value={0}>False</Radio>
  234. </Radio.Group>
  235. </div>
  236. {fieldMoreInfo?.(formSchema)}
  237. </div>
  238. )
  239. }
  240. }
  241. return (
  242. <div className={className}>
  243. {
  244. formSchemas.map(formSchema => renderField(formSchema))
  245. }
  246. </div>
  247. )
  248. }
  249. export default Form