form.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useChatWithHistoryContext } 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. } = useChatWithHistoryContext()
  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 === InputVarType.singleFile) {
  52. return (
  53. <FileUploaderInAttachmentWrapper
  54. value={newConversationInputs[variable] ? [newConversationInputs[variable]] : []}
  55. onChange={files => handleFormChange(variable, files[0])}
  56. fileConfig={{
  57. allowed_file_types: form.allowed_file_types,
  58. allowed_file_extensions: form.allowed_file_extensions,
  59. allowed_file_upload_methods: form.allowed_file_upload_methods,
  60. number_limits: 1,
  61. fileUploadConfig: (appParams as any).system_parameters,
  62. }}
  63. />
  64. )
  65. }
  66. if (form.type === InputVarType.multiFiles) {
  67. return (
  68. <FileUploaderInAttachmentWrapper
  69. value={newConversationInputs[variable]}
  70. onChange={files => handleFormChange(variable, files)}
  71. fileConfig={{
  72. allowed_file_types: form.allowed_file_types,
  73. allowed_file_extensions: form.allowed_file_extensions,
  74. allowed_file_upload_methods: form.allowed_file_upload_methods,
  75. number_limits: form.max_length,
  76. fileUploadConfig: (appParams as any).system_parameters,
  77. }}
  78. />
  79. )
  80. }
  81. return (
  82. <PortalSelect
  83. popupClassName='w-[200px]'
  84. value={newConversationInputs[variable]}
  85. items={options.map((option: string) => ({ value: option, name: option }))}
  86. onSelect={item => handleFormChange(variable, item.value as string)}
  87. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  88. />
  89. )
  90. }
  91. if (!inputsForms.length)
  92. return null
  93. return (
  94. <div className='mb-4 py-2'>
  95. {
  96. inputsForms.map(form => (
  97. <div
  98. key={form.variable}
  99. className={`flex mb-3 last-of-type:mb-0 text-sm text-gray-900 ${isMobile && '!flex-wrap'}`}
  100. >
  101. <div className={`shrink-0 mr-2 py-2 w-[128px] ${isMobile && '!w-full'}`}>{form.label}</div>
  102. {renderField(form)}
  103. </div>
  104. ))
  105. }
  106. </div>
  107. )
  108. }
  109. export default Form