form.tsx 4.0 KB

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