form.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useEmbeddedChatbotContext } from '../context'
  4. import Input from './form-input'
  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. const Form = () => {
  9. const { t } = useTranslation()
  10. const {
  11. appParams,
  12. inputsForms,
  13. newConversationInputs,
  14. newConversationInputsRef,
  15. handleNewConversationInputsChange,
  16. isMobile,
  17. } = useEmbeddedChatbotContext()
  18. const handleFormChange = useCallback((variable: string, value: any) => {
  19. handleNewConversationInputsChange({
  20. ...newConversationInputsRef.current,
  21. [variable]: value,
  22. })
  23. }, [newConversationInputsRef, handleNewConversationInputsChange])
  24. const renderField = (form: any) => {
  25. const {
  26. label,
  27. required,
  28. variable,
  29. options,
  30. } = form
  31. if (form.type === 'text-input' || form.type === 'paragraph') {
  32. return (
  33. <Input
  34. form={form}
  35. value={newConversationInputs[variable]}
  36. onChange={handleFormChange}
  37. />
  38. )
  39. }
  40. if (form.type === 'number') {
  41. return (
  42. <input
  43. className="grow h-9 rounded-lg bg-gray-100 px-2.5 outline-none appearance-none"
  44. type="number"
  45. value={newConversationInputs[variable] || ''}
  46. onChange={e => handleFormChange(variable, e.target.value)}
  47. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  48. />
  49. )
  50. }
  51. if (form.type === 'number') {
  52. return (
  53. <input
  54. className="grow h-9 rounded-lg bg-gray-100 px-2.5 outline-none appearance-none"
  55. type="number"
  56. value={newConversationInputs[variable] || ''}
  57. onChange={e => handleFormChange(variable, e.target.value)}
  58. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  59. />
  60. )
  61. }
  62. if (form.type === InputVarType.singleFile) {
  63. return (
  64. <FileUploaderInAttachmentWrapper
  65. value={newConversationInputs[variable] ? [newConversationInputs[variable]] : []}
  66. onChange={files => handleFormChange(variable, files[0])}
  67. fileConfig={{
  68. allowed_file_types: form.allowed_file_types,
  69. allowed_file_extensions: form.allowed_file_extensions,
  70. allowed_file_upload_methods: form.allowed_file_upload_methods,
  71. number_limits: 1,
  72. fileUploadConfig: (appParams as any).system_parameters,
  73. }}
  74. />
  75. )
  76. }
  77. if (form.type === InputVarType.multiFiles) {
  78. return (
  79. <FileUploaderInAttachmentWrapper
  80. value={newConversationInputs[variable]}
  81. onChange={files => handleFormChange(variable, files)}
  82. fileConfig={{
  83. allowed_file_types: form.allowed_file_types,
  84. allowed_file_extensions: form.allowed_file_extensions,
  85. allowed_file_upload_methods: form.allowed_file_upload_methods,
  86. number_limits: form.max_length,
  87. fileUploadConfig: (appParams as any).system_parameters,
  88. }}
  89. />
  90. )
  91. }
  92. return (
  93. <PortalSelect
  94. popupClassName='w-[200px]'
  95. value={newConversationInputs[variable]}
  96. items={options.map((option: string) => ({ value: option, name: option }))}
  97. onSelect={item => handleFormChange(variable, item.value as string)}
  98. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  99. />
  100. )
  101. }
  102. if (!inputsForms.length)
  103. return null
  104. return (
  105. <div className='mb-4 py-2'>
  106. {
  107. inputsForms.map(form => (
  108. <div
  109. key={form.variable}
  110. className={`flex mb-3 last-of-type:mb-0 text-sm text-gray-900 ${isMobile && '!flex-wrap'}`}
  111. >
  112. <div className={`shrink-0 mr-2 py-2 w-[128px] ${isMobile && '!w-full'}`}>{form.label}</div>
  113. {renderField(form)}
  114. </div>
  115. ))
  116. }
  117. </div>
  118. )
  119. }
  120. export default Form