form-generation.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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-gray-900'>
  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-gray-100 rounded-lg text-sm text-gray-900 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