app-inputs-form.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import Input from '@/app/components/base/input'
  4. import Textarea from '@/app/components/base/textarea'
  5. import { PortalSelect } from '@/app/components/base/select'
  6. import { InputVarType } from '@/app/components/workflow/types'
  7. import { FileUploaderInAttachmentWrapper } from '@/app/components/base/file-uploader'
  8. type Props = {
  9. inputsForms: any[]
  10. inputs: Record<string, any>
  11. inputsRef: any
  12. onFormChange: (value: Record<string, any>) => void
  13. }
  14. const AppInputsForm = ({
  15. inputsForms,
  16. inputs,
  17. inputsRef,
  18. onFormChange,
  19. }: Props) => {
  20. const { t } = useTranslation()
  21. const handleFormChange = useCallback((variable: string, value: any) => {
  22. onFormChange({
  23. ...inputsRef.current,
  24. [variable]: value,
  25. })
  26. }, [onFormChange, inputsRef])
  27. const renderField = (form: any) => {
  28. const {
  29. label,
  30. variable,
  31. options,
  32. } = form
  33. if (form.type === InputVarType.textInput) {
  34. return (
  35. <Input
  36. value={inputs[variable] || ''}
  37. onChange={e => handleFormChange(variable, e.target.value)}
  38. placeholder={label}
  39. />
  40. )
  41. }
  42. if (form.type === InputVarType.number) {
  43. return (
  44. <Input
  45. type="number"
  46. value={inputs[variable] || ''}
  47. onChange={e => handleFormChange(variable, e.target.value)}
  48. placeholder={label}
  49. />
  50. )
  51. }
  52. if (form.type === InputVarType.paragraph) {
  53. return (
  54. <Textarea
  55. value={inputs[variable] || ''}
  56. onChange={e => handleFormChange(variable, e.target.value)}
  57. placeholder={label}
  58. />
  59. )
  60. }
  61. if (form.type === InputVarType.select) {
  62. return (
  63. <PortalSelect
  64. popupClassName="w-[356px] z-[1050]"
  65. value={inputs[variable] || ''}
  66. items={options.map((option: string) => ({ value: option, name: option }))}
  67. onSelect={item => handleFormChange(variable, item.value as string)}
  68. placeholder={label}
  69. />
  70. )
  71. }
  72. if (form.type === InputVarType.singleFile) {
  73. return (
  74. <FileUploaderInAttachmentWrapper
  75. value={inputs[variable] ? [inputs[variable]] : []}
  76. onChange={files => handleFormChange(variable, files[0])}
  77. fileConfig={{
  78. allowed_file_types: form.allowed_file_types,
  79. allowed_file_extensions: form.allowed_file_extensions,
  80. allowed_file_upload_methods: form.allowed_file_upload_methods,
  81. number_limits: 1,
  82. fileUploadConfig: (form as any).fileUploadConfig,
  83. }}
  84. />
  85. )
  86. }
  87. if (form.type === InputVarType.multiFiles) {
  88. return (
  89. <FileUploaderInAttachmentWrapper
  90. value={inputs[variable]}
  91. onChange={files => handleFormChange(variable, files)}
  92. fileConfig={{
  93. allowed_file_types: form.allowed_file_types,
  94. allowed_file_extensions: form.allowed_file_extensions,
  95. allowed_file_upload_methods: form.allowed_file_upload_methods,
  96. number_limits: form.max_length,
  97. fileUploadConfig: (form as any).fileUploadConfig,
  98. }}
  99. />
  100. )
  101. }
  102. }
  103. if (!inputsForms.length)
  104. return null
  105. return (
  106. <div className='px-4 py-2 flex flex-col gap-4'>
  107. {inputsForms.map(form => (
  108. <div key={form.variable}>
  109. <div className='h-6 mb-1 flex items-center gap-1 text-text-secondary system-sm-semibold'>
  110. <div className='truncate'>{form.label}</div>
  111. {!form.required && <span className='text-text-tertiary system-xs-regular'>{t('workflow.panel.optional')}</span>}
  112. </div>
  113. {renderField(form)}
  114. </div>
  115. ))}
  116. </div>
  117. )
  118. }
  119. export default AppInputsForm