A2Table1.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import React, { useCallback, useMemo } from "react";
  2. import styles from "./index.module.scss";
  3. import { Button, Popconfirm, Table } from "antd";
  4. import { useSelector } from "react-redux";
  5. import { RootState } from "@/store";
  6. import { A2Tab1_1 } from "@/types/api/A2Dict";
  7. import { A2_APIdel, A2_APIsort } from "@/store/action/A2Dict";
  8. import { MessageFu } from "@/utils/message";
  9. type Props = {
  10. editFu: (item: A2Tab1_1) => void;
  11. upTaleFu: () => void;
  12. type: "job" | "status";
  13. };
  14. function A2Table1({ editFu, upTaleFu, type }: Props) {
  15. const tableArr = useSelector(
  16. (state: RootState) => state.A2Dict.A2Tab1_1Obj[type]
  17. );
  18. // 点击删除
  19. const delById = useCallback(
  20. async (id: number) => {
  21. const res = await A2_APIdel(id);
  22. if (res.code === 0) {
  23. MessageFu.success("删除成功!");
  24. upTaleFu();
  25. }
  26. },
  27. [upTaleFu]
  28. );
  29. // 点击排序
  30. const sortFu = useCallback(
  31. async (flag: 1 | -1, id: number, index: number) => {
  32. const arr = tableArr.filter((v) => v.name !== "其它");
  33. const newId = arr[index + flag].id;
  34. const res = await A2_APIsort(id, newId);
  35. if (res.code === 0) {
  36. MessageFu.success("排序修改成功!");
  37. upTaleFu();
  38. }
  39. },
  40. [tableArr, upTaleFu]
  41. );
  42. // 职能-项目经理-商务经理的 按钮过滤
  43. const jobBtnFlag = useCallback(
  44. (name: string, index: number) => {
  45. let flag = true; // true 为 显示按钮
  46. if (type === "job") {
  47. if (name === "项目经理" || name === "商务经理") flag = false;
  48. if (index === 2) flag = false;
  49. } else if (index === 0) flag = false;
  50. return flag;
  51. },
  52. [type]
  53. );
  54. const columns = useMemo(() => {
  55. return [
  56. {
  57. width: 100,
  58. title: "序号",
  59. render: (item: A2Tab1_1, __: any, index: number) =>
  60. item.name !== "其它" ? index + 1 : "-",
  61. },
  62. {
  63. title: "字典名称",
  64. dataIndex: "name",
  65. },
  66. {
  67. title: "说明",
  68. render: (item: A2Tab1_1) =>
  69. item.description ? (
  70. item.description.length >= 30 ? (
  71. <span style={{ cursor: "pointer" }} title={item.description}>
  72. {item.description.substring(0, 30) + "..."}
  73. </span>
  74. ) : (
  75. item.description
  76. )
  77. ) : (
  78. "(空)"
  79. ),
  80. },
  81. {
  82. title: "操作",
  83. render: (item: A2Tab1_1, _: any, index: number) => (
  84. <>
  85. {item.name !== "其它" && jobBtnFlag(item.name, index) ? (
  86. <Button
  87. size="small"
  88. type="text"
  89. onClick={() => sortFu(-1, item.id, index)}
  90. >
  91. 上移
  92. </Button>
  93. ) : null}
  94. {index < tableArr.length - 2 &&
  95. item.name !== "其它" &&
  96. jobBtnFlag(item.name, -1) ? (
  97. <Button
  98. size="small"
  99. type="text"
  100. onClick={() => sortFu(1, item.id, index)}
  101. >
  102. 下移
  103. </Button>
  104. ) : null}
  105. <Button size="small" type="text" onClick={() => editFu(item)}>
  106. 编辑
  107. </Button>
  108. {item.name !== "其它" && jobBtnFlag(item.name, -1) ? (
  109. <Popconfirm
  110. title="删除后无法恢复,是否删除?"
  111. okText="删除"
  112. cancelText="取消"
  113. onConfirm={() => delById(item.id)}
  114. okButtonProps={{ loading: false }}
  115. >
  116. <Button size="small" type="text" danger>
  117. 删除
  118. </Button>
  119. </Popconfirm>
  120. ) : null}
  121. </>
  122. ),
  123. },
  124. ];
  125. }, [delById, editFu, jobBtnFlag, sortFu, tableArr.length]);
  126. return (
  127. <div className={styles.A2Table1}>
  128. <Table
  129. size="small"
  130. dataSource={tableArr}
  131. columns={columns}
  132. rowKey="id"
  133. pagination={false}
  134. />
  135. </div>
  136. );
  137. }
  138. const MemoA2Table1 = React.memo(A2Table1);
  139. export default MemoA2Table1;