form.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { useTranslation } from 'react-i18next'
  2. import { useChatWithHistoryContext } from '../context'
  3. import { PortalSelect } from '@/app/components/base/select'
  4. const Form = () => {
  5. const { t } = useTranslation()
  6. const {
  7. inputsForms,
  8. newConversationInputs,
  9. handleNewConversationInputsChange,
  10. isMobile,
  11. } = useChatWithHistoryContext()
  12. const handleFormChange = (variable: string, value: string) => {
  13. handleNewConversationInputsChange({
  14. ...newConversationInputs,
  15. [variable]: value,
  16. })
  17. }
  18. const renderField = (form: any) => {
  19. const {
  20. label,
  21. required,
  22. max_length,
  23. variable,
  24. options,
  25. } = form
  26. if (form.type === 'text-input') {
  27. return (
  28. <input
  29. className='grow h-9 rounded-lg bg-gray-100 px-2.5 outline-none appearance-none'
  30. value={newConversationInputs[variable] || ''}
  31. maxLength={max_length}
  32. onChange={e => handleFormChange(variable, e.target.value)}
  33. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  34. />
  35. )
  36. }
  37. if (form.type === 'paragraph') {
  38. return (
  39. <textarea
  40. value={newConversationInputs[variable]}
  41. className='grow h-[104px] rounded-lg bg-gray-100 px-2.5 py-2 outline-none appearance-none resize-none'
  42. onChange={e => handleFormChange(variable, e.target.value)}
  43. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  44. />
  45. )
  46. }
  47. return (
  48. <PortalSelect
  49. popupClassName='w-[200px]'
  50. value={newConversationInputs[variable]}
  51. items={options.map((option: string) => ({ value: option, name: option }))}
  52. onSelect={item => handleFormChange(variable, item.value as string)}
  53. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  54. />
  55. )
  56. }
  57. if (!inputsForms.length)
  58. return null
  59. return (
  60. <div className='mb-4 py-2'>
  61. {
  62. inputsForms.map(form => (
  63. <div
  64. key={form.variable}
  65. className={`flex mb-3 last-of-type:mb-0 text-sm text-gray-900 ${isMobile && '!flex-wrap'}`}
  66. >
  67. <div className={`shrink-0 mr-2 py-2 w-[128px] ${isMobile && '!w-full'}`}>{form.label}</div>
  68. {renderField(form)}
  69. </div>
  70. ))
  71. }
  72. </div>
  73. )
  74. }
  75. export default Form