index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import React, { useCallback, useEffect, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import { Button, Checkbox, Empty, Modal } from "antd";
  4. import { A2_APIgetDownList } from "@/store/action/A1Project";
  5. import { A1getStorage, A1setStorage } from "@/utils/storage";
  6. import { MessageFu } from "@/utils/message";
  7. type SelectType = {
  8. id: number;
  9. label: string;
  10. done: boolean;
  11. };
  12. type Props = {
  13. downId: { id: number; txt: string };
  14. closeFu: () => void;
  15. };
  16. function A1Down({ downId, closeFu }: Props) {
  17. const getInfoFu = useCallback(async (id: number) => {
  18. const res = await A2_APIgetDownList(id);
  19. if (res.code === 0) {
  20. // setCheckedList(res.data || []);
  21. let arr = [
  22. { id: 1, label: "需求规格说明书", done: false },
  23. { id: 2, label: "重要文件1", done: false },
  24. { id: 3, label: "未分类", done: false },
  25. ];
  26. const storeIds = A1getStorage();
  27. if (storeIds) {
  28. arr = arr.map((v) => ({
  29. ...v,
  30. done: storeIds.includes(v.id) ? true : false,
  31. }));
  32. }
  33. setCheckedList(arr);
  34. }
  35. }, []);
  36. useEffect(() => {
  37. getInfoFu(downId.id);
  38. }, [getInfoFu, downId.id]);
  39. const [checkedList, setCheckedList] = useState([] as SelectType[]);
  40. // 全选的状态
  41. const [indeterminate, setIndeterminate] = useState(false);
  42. const [checkAll, setCheckAll] = useState(false);
  43. const onCheckAllChange = useCallback(
  44. (val: boolean) => {
  45. const newArr = checkedList.map((v) => ({
  46. ...v,
  47. done: val,
  48. }));
  49. setCheckedList(newArr);
  50. setCheckAll(val);
  51. },
  52. [checkedList]
  53. );
  54. const onChange = useCallback(
  55. (val: boolean, id: number) => {
  56. setCheckedList(
  57. checkedList.map((v) => ({
  58. ...v,
  59. done: v.id === id ? val : v.done,
  60. }))
  61. );
  62. },
  63. [checkedList]
  64. );
  65. useEffect(() => {
  66. const newArr = checkedList.filter((v) => v.done);
  67. setIndeterminate(!!newArr.length && newArr.length < checkedList.length);
  68. setCheckAll(newArr.length === checkedList.length);
  69. }, [checkedList]);
  70. // 点击提交
  71. const btnOkFu = useCallback(() => {
  72. console.log("-------", checkedList);
  73. }, [checkedList]);
  74. // 点击保存设置 到 本地
  75. const clickStoreFu = useCallback(() => {
  76. const arr = checkedList.filter((v) => v.done).map((v) => v.id);
  77. A1setStorage(arr);
  78. MessageFu.success("保存设置到本地成功!");
  79. }, [checkedList]);
  80. return (
  81. <Modal
  82. wrapClassName={styles.A1Down}
  83. destroyOnClose
  84. open={true}
  85. title={downId.txt}
  86. footer={
  87. [] // 设置footer为空,去掉 取消 确定默认按钮
  88. }
  89. >
  90. <div className="A1DownMain">
  91. {checkedList.length ? (
  92. <>
  93. <h3>请选择下载的项目文件类型</h3>
  94. <Checkbox
  95. indeterminate={indeterminate}
  96. onChange={(e) => onCheckAllChange(e.target.checked)}
  97. checked={checkAll}
  98. >
  99. 全选
  100. </Checkbox>
  101. </>
  102. ) : null}
  103. {checkedList.length ? (
  104. <div className="A1DownMainC">
  105. {checkedList.map((v) => (
  106. <div className="A1DownMainCRow" key={v.id} title={v.label}>
  107. <Checkbox
  108. onChange={(e) => onChange(e.target.checked, v.id)}
  109. checked={v.done}
  110. >
  111. {v.label}
  112. </Checkbox>
  113. </div>
  114. ))}
  115. </div>
  116. ) : (
  117. <>
  118. <br />
  119. <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
  120. </>
  121. )}
  122. <div className="A1DownBtn">
  123. {checkedList.length ? (
  124. <div className="A1DownBtn1" onClick={clickStoreFu}>
  125. <Button disabled={checkedList.filter((v) => v.done).length < 1}>
  126. 保存设置
  127. </Button>
  128. </div>
  129. ) : (
  130. <span></span>
  131. )}
  132. <div className="A1DownBtn2">
  133. <Button onClick={closeFu}>取消</Button>
  134. &emsp;
  135. <Button
  136. type="primary"
  137. onClick={btnOkFu}
  138. disabled={checkedList.filter((v) => v.done).length < 1}
  139. >
  140. 提交
  141. </Button>
  142. </div>
  143. </div>
  144. </div>
  145. </Modal>
  146. );
  147. }
  148. const MemoA1Down = React.memo(A1Down);
  149. export default MemoA1Down;