UpdateForm.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React, {useEffect} from 'react';
  2. import {Button, message, Modal} from 'antd';
  3. import {TableListItem} from '../../MemoryList/data.d';
  4. import Quill from "@/pages/Memory/components/Quill";
  5. import service from "@/pages/Memory/service";
  6. import {useSingleState} from "nice-hooks";
  7. // 表单特殊字段
  8. export interface FormValueType extends Partial<TableListItem> {
  9. }
  10. export interface UpdateFormProps {
  11. onCancel: (fresh?:boolean) => void;
  12. modalVisible: boolean;
  13. values: Partial<TableListItem>;
  14. }
  15. const UpdateForm: React.FC<UpdateFormProps> = (props) => {
  16. const [formValues, setFormValues] = useSingleState<TableListItem>({
  17. back: "",
  18. front: "",
  19. id: undefined,
  20. period: 0,
  21. remindTime: new Date(),
  22. tag: "",
  23. updateTime: "",
  24. userId: 0,...props.values});
  25. const {
  26. onCancel: hideModal,
  27. modalVisible,
  28. } = props;
  29. useEffect(()=>{
  30. console.log(props);
  31. },[])
  32. return (
  33. <Modal
  34. width={640}
  35. bodyStyle={{padding: '15px 15px 15px'}}
  36. destroyOnClose
  37. title="配置模型"
  38. visible={modalVisible}
  39. onCancel={() => hideModal(false)}
  40. footer={null}
  41. >
  42. <div>
  43. <h4>正面</h4>
  44. <Quill
  45. readonly={false}
  46. onChange={(value) => {
  47. setFormValues({front: value});
  48. }}
  49. value={formValues.front}/>
  50. <p/>
  51. <h4>反面</h4>
  52. <Quill
  53. readonly={false}
  54. onChange={(value) => {
  55. setFormValues({back: value});
  56. }}
  57. value={formValues.back}
  58. />
  59. <p/>
  60. <Button
  61. type="primary"
  62. onClick={async () => {
  63. if (formValues.front.length >= 3000 || formValues.back.length >= 3000) {
  64. message.info("文字太长了,减少一些吧!");
  65. return;
  66. }
  67. let res;
  68. if (formValues.id != undefined) {
  69. res = await service.update(formValues);
  70. } else {
  71. res = await service.insert(formValues);
  72. }
  73. if (res.success) {
  74. message.info('操作成功');
  75. if (formValues.id != undefined) {
  76. hideModal(true);
  77. } else {
  78. setFormValues({front:'',back:''})
  79. }
  80. } else {
  81. message.info(`操作失败: ${res.message}`);
  82. }
  83. }}
  84. >
  85. 确定
  86. </Button>
  87. <span> </span>
  88. <Button
  89. type="primary"
  90. onClick={() => {
  91. hideModal(false)
  92. }}
  93. >关闭</Button>
  94. </div>
  95. </Modal>
  96. );
  97. };
  98. export default UpdateForm;