form.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. const Form = () => {
  7. const { t } = useTranslation()
  8. const {
  9. inputsForms,
  10. newConversationInputs,
  11. handleNewConversationInputsChange,
  12. isMobile,
  13. } = useChatWithHistoryContext()
  14. const handleFormChange = useCallback((variable: string, value: string) => {
  15. handleNewConversationInputsChange({
  16. ...newConversationInputs,
  17. [variable]: value,
  18. })
  19. }, [newConversationInputs, handleNewConversationInputsChange])
  20. const renderField = (form: any) => {
  21. const {
  22. label,
  23. required,
  24. variable,
  25. options,
  26. } = form
  27. if (form.type === 'text-input' || form.type === 'paragraph') {
  28. return (
  29. <Input
  30. form={form}
  31. value={newConversationInputs[variable]}
  32. onChange={handleFormChange}
  33. />
  34. )
  35. }
  36. return (
  37. <PortalSelect
  38. popupClassName='w-[200px]'
  39. value={newConversationInputs[variable]}
  40. items={options.map((option: string) => ({ value: option, name: option }))}
  41. onSelect={item => handleFormChange(variable, item.value as string)}
  42. placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`}
  43. />
  44. )
  45. }
  46. if (!inputsForms.length)
  47. return null
  48. return (
  49. <div className='mb-4 py-2'>
  50. {
  51. inputsForms.map(form => (
  52. <div
  53. key={form.variable}
  54. className={`flex mb-3 last-of-type:mb-0 text-sm text-gray-900 ${isMobile && '!flex-wrap'}`}
  55. >
  56. <div className={`shrink-0 mr-2 py-2 w-[128px] ${isMobile && '!w-full'}`}>{form.label}</div>
  57. {renderField(form)}
  58. </div>
  59. ))
  60. }
  61. </div>
  62. )
  63. }
  64. export default Form