Form.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. const schema = formSchemas.find(it => it.variable === clearVariable)
  62. shouldClearVariable[clearVariable] = schema ? schema.default : undefined
  63. })
  64. }
  65. onChange({ ...value, [key]: val, ...shouldClearVariable })
  66. }
  67. const renderField = (formSchema: CredentialFormSchema) => {
  68. const tooltip = formSchema.tooltip
  69. const tooltipContent = (tooltip && (
  70. <Tooltip
  71. popupContent={
  72. <div className='w-[200px]'>
  73. {tooltip[language] || tooltip.en_US}
  74. </div>}
  75. triggerClassName='ml-1 w-4 h-4'
  76. asChild={false}
  77. />
  78. ))
  79. if (formSchema.type === FormTypeEnum.textInput || formSchema.type === FormTypeEnum.secretInput || formSchema.type === FormTypeEnum.textNumber) {
  80. const {
  81. variable,
  82. label,
  83. placeholder,
  84. required,
  85. show_on,
  86. } = formSchema as (CredentialFormSchemaTextInput | CredentialFormSchemaSecretInput)
  87. if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value))
  88. return null
  89. const disabled = readonly || (isEditMode && (variable === '__model_type' || variable === '__model_name'))
  90. return (
  91. <div key={variable} className={cn(itemClassName, 'py-3')}>
  92. <div className={cn(fieldLabelClassName, 'flex items-center py-2 text-sm text-gray-900')}>
  93. {label[language] || label.en_US}
  94. {
  95. required && (
  96. <span className='ml-1 text-red-500'>*</span>
  97. )
  98. }
  99. {tooltipContent}
  100. </div>
  101. <Input
  102. className={cn(inputClassName, `${disabled && 'cursor-not-allowed opacity-60'}`)}
  103. value={(isShowDefaultValue && ((value[variable] as string) === '' || value[variable] === undefined || value[variable] === null)) ? formSchema.default : value[variable]}
  104. onChange={val => handleFormChange(variable, val)}
  105. validated={validatedSuccess}
  106. placeholder={placeholder?.[language] || placeholder?.en_US}
  107. disabled={disabled}
  108. type={formSchema.type === FormTypeEnum.textNumber ? 'number' : 'text'}
  109. {...(formSchema.type === FormTypeEnum.textNumber ? { min: (formSchema as CredentialFormSchemaNumberInput).min, max: (formSchema as CredentialFormSchemaNumberInput).max } : {})}
  110. />
  111. {fieldMoreInfo?.(formSchema)}
  112. {validating && changeKey === variable && <ValidatingTip />}
  113. </div>
  114. )
  115. }
  116. if (formSchema.type === FormTypeEnum.radio) {
  117. const {
  118. options,
  119. variable,
  120. label,
  121. show_on,
  122. required,
  123. } = formSchema as CredentialFormSchemaRadio
  124. if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value))
  125. return null
  126. const disabled = isEditMode && (variable === '__model_type' || variable === '__model_name')
  127. return (
  128. <div key={variable} className={cn(itemClassName, 'py-3')}>
  129. <div className={cn(fieldLabelClassName, 'flex items-center py-2 text-sm text-gray-900')}>
  130. {label[language] || label.en_US}
  131. {
  132. required && (
  133. <span className='ml-1 text-red-500'>*</span>
  134. )
  135. }
  136. {tooltipContent}
  137. </div>
  138. <div className={`grid grid-cols-${options?.length} gap-3`}>
  139. {
  140. options.filter((option) => {
  141. if (option.show_on.length)
  142. return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)
  143. return true
  144. }).map(option => (
  145. <div
  146. className={`
  147. flex items-center px-3 py-2 rounded-lg border border-gray-100 bg-gray-25 cursor-pointer
  148. ${value[variable] === option.value && 'bg-white border-[1.5px] border-primary-400 shadow-sm'}
  149. ${disabled && '!cursor-not-allowed opacity-60'}
  150. `}
  151. onClick={() => handleFormChange(variable, option.value)}
  152. key={`${variable}-${option.value}`}
  153. >
  154. <div className={`
  155. flex justify-center items-center mr-2 w-4 h-4 border border-gray-300 rounded-full
  156. ${value[variable] === option.value && 'border-[5px] border-primary-600'}
  157. `} />
  158. <div className='text-sm text-gray-900'>{option.label[language] || option.label.en_US}</div>
  159. </div>
  160. ))
  161. }
  162. </div>
  163. {fieldMoreInfo?.(formSchema)}
  164. {validating && changeKey === variable && <ValidatingTip />}
  165. </div>
  166. )
  167. }
  168. if (formSchema.type === 'select') {
  169. const {
  170. options,
  171. variable,
  172. label,
  173. show_on,
  174. required,
  175. placeholder,
  176. } = formSchema as CredentialFormSchemaSelect
  177. if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value))
  178. return null
  179. return (
  180. <div key={variable} className={cn(itemClassName, 'py-3')}>
  181. <div className={cn(fieldLabelClassName, 'flex items-center py-2 text-sm text-gray-900')}>
  182. {label[language] || label.en_US}
  183. {
  184. required && (
  185. <span className='ml-1 text-red-500'>*</span>
  186. )
  187. }
  188. {tooltipContent}
  189. </div>
  190. <SimpleSelect
  191. className={cn(inputClassName)}
  192. disabled={readonly}
  193. defaultValue={(isShowDefaultValue && ((value[variable] as string) === '' || value[variable] === undefined || value[variable] === null)) ? formSchema.default : value[variable]}
  194. items={options.filter((option) => {
  195. if (option.show_on.length)
  196. return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)
  197. return true
  198. }).map(option => ({ value: option.value, name: option.label[language] || option.label.en_US }))}
  199. onSelect={item => handleFormChange(variable, item.value as string)}
  200. placeholder={placeholder?.[language] || placeholder?.en_US}
  201. />
  202. {fieldMoreInfo?.(formSchema)}
  203. {validating && changeKey === variable && <ValidatingTip />}
  204. </div>
  205. )
  206. }
  207. if (formSchema.type === 'boolean') {
  208. const {
  209. variable,
  210. label,
  211. show_on,
  212. required,
  213. } = formSchema as CredentialFormSchemaRadio
  214. if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value))
  215. return null
  216. return (
  217. <div key={variable} className={cn(itemClassName, 'py-3')}>
  218. <div className='flex items-center justify-between py-2 text-sm text-gray-900'>
  219. <div className='flex items-center space-x-2'>
  220. <span className={cn(fieldLabelClassName, 'flex items-center py-2 text-sm text-gray-900')}>{label[language] || label.en_US}</span>
  221. {
  222. required && (
  223. <span className='ml-1 text-red-500'>*</span>
  224. )
  225. }
  226. {tooltipContent}
  227. </div>
  228. <Radio.Group
  229. className='flex items-center'
  230. value={value[variable] === null ? undefined : (value[variable] ? 1 : 0)}
  231. onChange={val => handleFormChange(variable, val === 1)}
  232. >
  233. <Radio value={1} className='!mr-1'>True</Radio>
  234. <Radio value={0}>False</Radio>
  235. </Radio.Group>
  236. </div>
  237. {fieldMoreInfo?.(formSchema)}
  238. </div>
  239. )
  240. }
  241. }
  242. return (
  243. <div className={className}>
  244. {
  245. formSchemas.map(formSchema => renderField(formSchema))
  246. }
  247. </div>
  248. )
  249. }
  250. export default Form