index.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Button, Form, FormInstance, Input, Modal, Popconfirm } from "antd";
  2. import React, { useCallback, useEffect, useRef } from "react";
  3. import TextArea from "antd/es/input/TextArea";
  4. import { getSmartDetailAPI, smartSaveAPI } from "@/store/action/B6Smart";
  5. import { MessageFu } from "@/utils/message";
  6. import { SmartSaveDataType } from "@/types";
  7. import styles from "./index.module.scss";
  8. type Props = {
  9. id: number;
  10. closeMoalFu: () => void;
  11. addTableFu: () => void;
  12. editTableFu: () => void;
  13. };
  14. function SmartAutoAdd({ id, closeMoalFu, addTableFu, editTableFu }: Props) {
  15. // 表单的ref
  16. const FormBoxRef = useRef<FormInstance>(null);
  17. // 是编辑,通过id获取详情
  18. const getInfoByIdFu = useCallback(async (id: number) => {
  19. const res = await getSmartDetailAPI(id);
  20. FormBoxRef.current?.setFieldsValue(res.data);
  21. }, []);
  22. useEffect(() => {
  23. if (id > 0) getInfoByIdFu(id);
  24. }, [getInfoByIdFu, id]);
  25. // 没有通过校验
  26. const onFinishFailed = useCallback(() => {}, []);
  27. // 通过校验点击确定
  28. const onFinish = useCallback(
  29. async (value: SmartSaveDataType) => {
  30. const obj: SmartSaveDataType = {
  31. ...value,
  32. id: id > 0 ? id : null,
  33. type: "play",
  34. };
  35. const res = await smartSaveAPI(obj);
  36. if (res.code === 0) {
  37. MessageFu.success(id > 0 ? "编辑成功!" : "新增成功!");
  38. if (id < 0) addTableFu();
  39. else editTableFu();
  40. closeMoalFu();
  41. }
  42. console.log("通过校验,点击确定");
  43. },
  44. [addTableFu, closeMoalFu, editTableFu, id]
  45. );
  46. return (
  47. <Modal
  48. wrapClassName={styles.SmartAutoAdd}
  49. destroyOnClose
  50. open={true}
  51. title={id > 0 ? "编辑播报" : "新增播报"}
  52. footer={
  53. [] // 设置footer为空,去掉 取消 确定默认按钮
  54. }
  55. >
  56. <div className="main">
  57. <Form
  58. ref={FormBoxRef}
  59. name="basic"
  60. labelCol={{ span: 2 }}
  61. onFinish={onFinish}
  62. onFinishFailed={onFinishFailed}
  63. autoComplete="off"
  64. >
  65. <Form.Item
  66. label="标题"
  67. name="name"
  68. rules={[{ required: true, message: "请输入标题!" }]}
  69. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  70. >
  71. <Input maxLength={25} showCount placeholder="请输入内容" />
  72. </Form.Item>
  73. <Form.Item
  74. label="正文"
  75. name="description"
  76. rules={[{ required: true, message: "请输入简介!" }]}
  77. getValueFromEvent={(e) => e.target.value.trim()}
  78. >
  79. <TextArea
  80. rows={10}
  81. placeholder="请输入内容"
  82. showCount
  83. maxLength={2000}
  84. />
  85. </Form.Item>
  86. {/* 确定和取消按钮 */}
  87. <br />
  88. <Form.Item wrapperCol={{ offset: 10, span: 20 }}>
  89. <Button type="primary" htmlType="submit">
  90. 提交
  91. </Button>
  92. &emsp; &emsp;
  93. <Popconfirm
  94. title="放弃编辑后,信息将不会保存!"
  95. okText="放弃"
  96. cancelText="取消"
  97. onConfirm={closeMoalFu}
  98. >
  99. <Button>取消</Button>
  100. </Popconfirm>
  101. </Form.Item>
  102. </Form>
  103. </div>
  104. </Modal>
  105. );
  106. }
  107. const MemoSmartAutoAdd = React.memo(SmartAutoAdd);
  108. export default MemoSmartAutoAdd;