prompt-form.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { IPrompt } from '@/types/prompt';
  2. import { Form, FormInstance, Input, Select, Spin } from 'antd';
  3. import { Ref, forwardRef, useEffect, useState } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. interface IProps {
  6. prompt?: IPrompt;
  7. onFinish: (prompt: IPrompt) => void;
  8. scenes?: Array<Record<string, string>>;
  9. }
  10. export default forwardRef(function PromptForm(props: IProps, ref: Ref<FormInstance<any>> | undefined) {
  11. const { t } = useTranslation();
  12. const [form] = Form.useForm();
  13. const { prompt, onFinish, scenes } = props;
  14. const [loading, setLoading] = useState<boolean>(false);
  15. useEffect(() => {
  16. if (prompt) {
  17. form.setFieldsValue(prompt);
  18. }
  19. }, []);
  20. const submit = async () => {
  21. const values = form.getFieldsValue();
  22. setLoading(true);
  23. await onFinish(values);
  24. setLoading(false);
  25. };
  26. return (
  27. <Spin spinning={loading}>
  28. <Form
  29. form={form}
  30. ref={ref}
  31. name={`prompt-item-${prompt?.prompt_name || 'new'}`}
  32. layout='vertical'
  33. className='mt-4'
  34. onFinish={submit}
  35. >
  36. <Form.Item
  37. name='chat_scene'
  38. label={t('Prompt_Info_Scene')}
  39. rules={[{ required: true, message: t('Please_Input') + t('Prompt_Info_Scene') }]}
  40. >
  41. <Select options={scenes}></Select>
  42. </Form.Item>
  43. <Form.Item
  44. name='sub_chat_scene'
  45. label={t('Prompt_Info_Sub_Scene')}
  46. rules={[{ required: true, message: t('Please_Input') + t('Prompt_Info_Sub_Scene') }]}
  47. >
  48. <Input />
  49. </Form.Item>
  50. <Form.Item
  51. name='prompt_name'
  52. label={t('Prompt_Info_Name')}
  53. rules={[{ required: true, message: t('Please_Input') + t('Prompt_Info_Name') }]}
  54. >
  55. <Input disabled={!!prompt} />
  56. </Form.Item>
  57. <Form.Item
  58. name='content'
  59. label={t('Prompt_Info_Content')}
  60. rules={[{ required: true, message: t('Please_Input') + t('Prompt_Info_Content') }]}
  61. >
  62. <Input.TextArea rows={6} />
  63. </Form.Item>
  64. </Form>
  65. </Spin>
  66. );
  67. });