form-generation.tsx 2.6 KB

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