index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import React, { useCallback, useEffect, useMemo, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import { Button, Cascader, Input, Modal, Popconfirm, Table } from "antd";
  4. import {
  5. A2GoodObjTit,
  6. A2GoodObjTxt,
  7. statusTxtObj,
  8. storageStatusTxtObj,
  9. } from "../../data";
  10. import { A2tableType } from "@/types";
  11. import { A2_APIresList } from "@/store/action/A2Goods";
  12. import { MessageFu } from "@/utils/message";
  13. import { useDispatch, useSelector } from "react-redux";
  14. import { API_roomTree } from "@/store/action/A4Roomset";
  15. import { RootState } from "@/store";
  16. type Props = {
  17. oldList: A2tableType[]; //外层的表格数据,用来和 isAcList 同步
  18. upTableFu: (itemArr: A2tableType[]) => void;
  19. closeFu: () => void;
  20. myType: "ZX" | "RK" | "YK" | "CK";
  21. };
  22. function A2SelectModal({ oldList, upTableFu, closeFu, myType }: Props) {
  23. const dispatch = useDispatch();
  24. // 获取下拉框树结构
  25. useEffect(() => {
  26. dispatch(API_roomTree());
  27. }, [dispatch]);
  28. const roomTree = useSelector((state: RootState) => state.A4Roomset.roomTree);
  29. // 搜索框
  30. const [txt, setTxt] = useState("");
  31. const [txtRes, setTxtRes] = useState("");
  32. // 所有藏品的数据
  33. const [listAll, setListAll] = useState<A2tableType[]>([]);
  34. // 在页面展示的藏品数据
  35. const [list, setList] = useState<A2tableType[]>([]);
  36. useEffect(() => {
  37. if (!txtRes) setList(listAll);
  38. else
  39. setList(
  40. listAll.filter((v) => v.name.includes(txtRes) || v.num.includes(txtRes))
  41. );
  42. }, [listAll, txtRes]);
  43. const getListAllFu = useCallback(async () => {
  44. const res = await A2_APIresList({ searchKey: "", type: myType });
  45. if (res.code === 0) setListAll(res.data);
  46. }, [myType]);
  47. useEffect(() => {
  48. getListAllFu();
  49. }, [getListAllFu]);
  50. // 已经添加的藏品数组
  51. const [isAcList, setIsAcList] = useState<A2tableType[]>([]);
  52. useEffect(() => {
  53. setIsAcList(oldList);
  54. }, [oldList]);
  55. const columns = useMemo(() => {
  56. let arr: any = [];
  57. arr = [
  58. {
  59. title: "编号",
  60. dataIndex: "num",
  61. },
  62. {
  63. title: "名称",
  64. dataIndex: "name",
  65. },
  66. {
  67. title: "级别",
  68. dataIndex: "dictLevel",
  69. },
  70. {
  71. title: "藏品状态",
  72. render: (item: A2tableType) =>
  73. Reflect.get(statusTxtObj, item.status) || "-",
  74. },
  75. {
  76. title: "库存状态",
  77. render: (item: A2tableType) =>
  78. Reflect.get(storageStatusTxtObj, item.storageStatus) || "-",
  79. },
  80. ];
  81. if (["YK", "CK"].includes(myType)) {
  82. arr.push({
  83. title: "库房位置",
  84. render: (item: A2tableType) => (
  85. <>
  86. <Cascader
  87. // 自定义字段
  88. fieldNames={{
  89. label: "name",
  90. value: "id",
  91. children: "children",
  92. }}
  93. style={{ width: "100%" }}
  94. options={roomTree}
  95. placeholder=""
  96. value={
  97. item.storageIds
  98. ? item.storageIds.split(",").map((v) => Number(v))
  99. : undefined
  100. }
  101. onChange={(e) => console.log(e)}
  102. />
  103. </>
  104. ),
  105. });
  106. }
  107. arr.push({
  108. title: "操作",
  109. render: (item: A2tableType) => (
  110. <>
  111. {isAcList.some((v) => v.id === item.id) ? (
  112. <Button
  113. size="small"
  114. type="text"
  115. onClick={() =>
  116. setIsAcList(isAcList.filter((v) => v.id !== item.id))
  117. }
  118. >
  119. 取消添加
  120. </Button>
  121. ) : (
  122. <Button
  123. size="small"
  124. type="text"
  125. onClick={() => setIsAcList([...isAcList, item])}
  126. >
  127. 添加
  128. </Button>
  129. )}
  130. </>
  131. ),
  132. });
  133. return arr;
  134. }, [isAcList, myType, roomTree]);
  135. return (
  136. <Modal
  137. wrapClassName={styles.A2SelectModal}
  138. open={true}
  139. title={
  140. <>
  141. 添加藏品 - {Reflect.get(A2GoodObjTxt, myType)}&emsp;
  142. <span className="A2Stit">
  143. 仅支持添加状态为 [已登记] {Reflect.get(A2GoodObjTit, myType)} 的藏品
  144. </span>
  145. </>
  146. }
  147. footer={
  148. [] // 设置footer为空,去掉 取消 确定默认按钮
  149. }
  150. >
  151. <div className="A2Cmain">
  152. {/* 搜索框 */}
  153. <div className="A2Cinput">
  154. <Input
  155. style={{ width: 260 }}
  156. placeholder="请输入藏品编号/名称,最多10字"
  157. maxLength={10}
  158. value={txt}
  159. onChange={(e) => setTxt(e.target.value.replace(/\s+/g, ""))}
  160. />
  161. &emsp;
  162. <Button type="primary" onClick={() => setTxtRes(txt)}>
  163. 搜索
  164. </Button>
  165. </div>
  166. <div className="A2CtableBox">
  167. {/* 左边的未选中的表格 */}
  168. <div className="A2Ctable">
  169. <div className="A2Ctit">
  170. 共计&nbsp;<span>{listAll.length}</span>
  171. &nbsp;个藏品。
  172. </div>
  173. <Table
  174. size="small"
  175. scroll={{ y: 400 }}
  176. dataSource={list.filter(
  177. (v) => !isAcList.map((c) => c.id).includes(v.id)
  178. )}
  179. columns={columns}
  180. rowKey="id"
  181. pagination={false}
  182. />
  183. </div>
  184. {/* 右边表格 */}
  185. <div className="A2Ctable">
  186. <div className="A2Ctit">
  187. 已添加&nbsp;<span>{isAcList.length}</span>
  188. &nbsp;个藏品。
  189. </div>
  190. <Table
  191. size="small"
  192. scroll={{ y: 400 }}
  193. dataSource={isAcList}
  194. columns={columns}
  195. rowKey="id"
  196. pagination={false}
  197. />
  198. </div>
  199. </div>
  200. {/* 按钮 */}
  201. <div className="A2Cbtn">
  202. <Button
  203. type="primary"
  204. disabled={isAcList.length <= 0}
  205. onClick={() => {
  206. upTableFu(isAcList);
  207. MessageFu.success("添加成功!");
  208. closeFu();
  209. }}
  210. >
  211. 提交
  212. </Button>
  213. &emsp;
  214. <Popconfirm
  215. title="放弃编辑后,信息将不会保存!"
  216. okText="放弃"
  217. cancelText="取消"
  218. onConfirm={closeFu}
  219. okButtonProps={{ loading: false }}
  220. >
  221. <Button>取消</Button>
  222. </Popconfirm>
  223. </div>
  224. </div>
  225. </Modal>
  226. );
  227. }
  228. const MemoA2SelectModal = React.memo(A2SelectModal);
  229. export default MemoA2SelectModal;