diff --git a/src/compenents/uploadImageButton/index.tsx b/src/compenents/uploadImageButton/index.tsx index c6685d2..4d53c07 100644 --- a/src/compenents/uploadImageButton/index.tsx +++ b/src/compenents/uploadImageButton/index.tsx @@ -1,9 +1,19 @@ import React, { useEffect, useState } from "react"; -import { Button, Row, Col, Modal, Image, Empty, message } from "antd"; +import { + Button, + Row, + Col, + Modal, + Image, + Empty, + message, + Pagination, +} from "antd"; import { resource, resourceCategory } from "../../api"; import styles from "./index.module.less"; import { CreateResourceCategory } from "../createResourceCategory"; import { CloseOutlined } from "@ant-design/icons"; +import { UploadImageSub } from "./uploadImageSub"; interface CategoryItem { id: number; @@ -25,14 +35,20 @@ interface ImageItem { created_at: string; } -export const UploadImageButton: React.FC = () => { +interface PropsInterface { + onSelected: (url: string) => void; +} + +export const UploadImageButton = (props: PropsInterface) => { const [showModal, setShowModal] = useState(false); const [categories, setCategories] = useState([]); const [defaultCid, setDefaultCid] = useState(0); const [refreshCategories, setRefreshCategories] = useState(1); + const [imageList, setImageList] = useState([]); + const [refresh, setRefresh] = useState(false); const [page, setPage] = useState(0); - const [size, setSize] = useState(10); + const [size, setSize] = useState(12); const [total, setTotal] = useState(0); const getCategories = () => { @@ -51,11 +67,7 @@ export const UploadImageButton: React.FC = () => { }); }; - useEffect(() => { - getCategories(); - }, [refreshCategories]); - - useEffect(() => { + const getImageList = () => { if (defaultCid === 0) { return; } @@ -68,7 +80,21 @@ export const UploadImageButton: React.FC = () => { .catch((err) => { console.log("错误,", err); }); - }, [defaultCid]); + }; + + const resetImageList = () => { + setPage(1); + setImageList([]); + setRefresh(!refresh); + }; + + useEffect(() => { + getCategories(); + }, [refreshCategories]); + + useEffect(() => { + getImageList(); + }, [defaultCid, refresh, page, size]); return ( <> @@ -83,10 +109,11 @@ export const UploadImageButton: React.FC = () => { {showModal && ( { setShowModal(false); }} - open={showModal} + open={true} width="1000px" maskClosable={false} > @@ -139,7 +166,12 @@ export const UploadImageButton: React.FC = () => { - + { + resetImageList(); + }} + > { )} {imageList.map((item) => ( - - + { + props.onSelected(item.url); + setShowModal(false); + }} + > + ))} + + {imageList.length > 0 && ( + + { + setPage(currentPage); + setSize(currentSize); + }} + defaultCurrent={page} + total={total} + /> + + )} diff --git a/src/compenents/uploadImageButton/uploadImageSub/index.tsx b/src/compenents/uploadImageButton/uploadImageSub/index.tsx new file mode 100644 index 0000000..5f3c4b7 --- /dev/null +++ b/src/compenents/uploadImageButton/uploadImageSub/index.tsx @@ -0,0 +1,79 @@ +import { Button, message, Modal } from "antd"; +import Dragger from "antd/es/upload/Dragger"; +import { useState } from "react"; +import config from "../../../js/config"; +import { getToken } from "../../../utils"; +import { InboxOutlined } from "@ant-design/icons"; + +interface PropsInterface { + categoryId: number; + onUpdate: () => void; +} + +export const UploadImageSub = (props: PropsInterface) => { + const [showModal, setShowModal] = useState(false); + + const uploadProps = { + name: "file", + multiple: true, + action: + config.app_url + + "/backend/v1/upload/image?category_id=" + + props.categoryId, + headers: { + authorization: "Bearer " + getToken(), + }, + onChange(info: any) { + const { status } = info.file; + if (status !== "uploading") { + console.log(info.file, info.fileList); + } + if (status === "done") { + message.success(`${info.file.name} 上传成功`); + } else if (status === "error") { + message.error(`${info.file.name} 上传失败`); + } + }, + showUploadList: { + showRemoveIcon: false, + showDownloadIcon: false, + }, + }; + + return ( + <> + + + {showModal && ( + { + setShowModal(false); + }} + onOk={() => { + props.onUpdate(); + setShowModal(false); + }} + > + +

+ +

+

请将图片拖拽到此处上传

+

+ 支持一次上传多个 / 支持 png,jpg,jpeg,gif 格式图片 +

+
+
+ )} + + ); +}; diff --git a/src/js/config.ts b/src/js/config.ts new file mode 100644 index 0000000..84036cb --- /dev/null +++ b/src/js/config.ts @@ -0,0 +1,3 @@ +export default { + app_url: process.env.REACT_APP_URL, +}; diff --git a/src/pages/test/index.tsx b/src/pages/test/index.tsx index 18639fc..28a2e53 100644 --- a/src/pages/test/index.tsx +++ b/src/pages/test/index.tsx @@ -1,10 +1,15 @@ +import { message } from "antd"; import React from "react"; import { UploadImageButton } from "../../compenents"; export const TestPage: React.FC = () => { return (
- + { + message.success("选择了:" + url); + }} + >
); };