form-generation.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type { FC } from 'react'
  2. import { useContext } from 'use-context-selector'
  3. import type { CodeBasedExtensionForm } from '@/models/common'
  4. import I18n from '@/context/i18n'
  5. import { PortalSelect } from '@/app/components/base/select'
  6. import Textarea from '@/app/components/base/textarea'
  7. import type { ModerationConfig } from '@/models/debug'
  8. type FormGenerationProps = {
  9. forms: CodeBasedExtensionForm[]
  10. value: ModerationConfig['config']
  11. onChange: (v: Record<string, string>) => void
  12. }
  13. const FormGeneration: FC<FormGenerationProps> = ({
  14. forms,
  15. value,
  16. onChange,
  17. }) => {
  18. const { locale } = useContext(I18n)
  19. const handleFormChange = (type: string, v: string) => {
  20. onChange({ ...value, [type]: v })
  21. }
  22. return (
  23. <>
  24. {
  25. forms.map((form, index) => (
  26. <div
  27. key={index}
  28. className='py-2'
  29. >
  30. <div className='flex items-center h-9 text-sm font-medium text-text-primary'>
  31. {locale === 'zh-Hans' ? form.label['zh-Hans'] : form.label['en-US']}
  32. </div>
  33. {
  34. form.type === 'text-input' && (
  35. <input
  36. value={value?.[form.variable] || ''}
  37. className='block px-3 w-full h-9 bg-components-input-bg-normal rounded-lg text-sm text-text-primary outline-none appearance-none'
  38. placeholder={form.placeholder}
  39. onChange={e => handleFormChange(form.variable, e.target.value)}
  40. />
  41. )
  42. }
  43. {
  44. form.type === 'paragraph' && (
  45. <div className='relative'>
  46. <Textarea
  47. className='resize-none'
  48. value={value?.[form.variable] || ''}
  49. placeholder={form.placeholder}
  50. onChange={e => handleFormChange(form.variable, e.target.value)}
  51. />
  52. </div>
  53. )
  54. }
  55. {
  56. form.type === 'select' && (
  57. <PortalSelect
  58. value={value?.[form.variable]}
  59. items={form.options.map((option) => {
  60. return {
  61. name: option.label[locale === 'zh-Hans' ? 'zh-Hans' : 'en-US'],
  62. value: option.value,
  63. }
  64. })}
  65. onSelect={item => handleFormChange(form.variable, item.value as string)}
  66. popupClassName='w-[576px] !z-[102]'
  67. />
  68. )
  69. }
  70. </div>
  71. ))
  72. }
  73. </>
  74. )
  75. }
  76. export default FormGeneration