safety-eval-service-frontend/src/components/AttachmentUpload/index.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2026-07-01 17:57:42 +08:00
import { Form, Image, Upload } from "antd";
import { useState } from "react";
2026-07-02 11:00:31 +08:00
import { PlusOutlined } from "@ant-design/icons";
const isImage = (url) =>
/\.(png|jpe?g|gif|bmp|webp|svg)(\?.*)?$/i.test(url || "");
2026-07-01 17:57:42 +08:00
2026-07-02 11:00:31 +08:00
export default function AttachmentUpload({ name, label, disabled = false }) {
2026-07-01 17:57:42 +08:00
const [previewImage, setPreviewImage] = useState("");
return (
<>
<Form.Item
name={name}
label={label}
valuePropName="fileList"
getValueProps={(value) => {
if (Array.isArray(value)) {
return { fileList: value };
}
return {
fileList: value
? value.split(",").map((item) => ({
url: item,
uid: item,
status: "done",
name: "附件",
}))
: [],
};
}}
getValueFromEvent={({ fileList }) => {
2026-07-02 15:01:19 +08:00
console.log(fileList, 'fileList')
2026-07-01 17:57:42 +08:00
return (
fileList?.map((file) => ({
url: file.response?.data?.url || file.url,
uid: file.uid,
status: "done",
name: file.name,
2026-07-02 15:01:19 +08:00
percent: file.percent,
2026-07-01 17:57:42 +08:00
})) || []
);
}}
>
<Upload
disabled={disabled}
listType="picture-card"
2026-07-02 14:51:26 +08:00
action={`${window.process.env.app.API_HOST}/safetyEval/file/upload`}
2026-07-01 17:57:42 +08:00
onPreview={(file) => {
if (isImage(file.url || file.thumbUrl)) {
setPreviewImage(file.url || file.thumbUrl);
} else {
window.open(file.url || file.thumbUrl);
}
}}
>
2026-07-02 11:00:31 +08:00
<button style={{ border: 0, background: "none" }} type="button">
<PlusOutlined />
<div style={{ marginTop: 8 }}>上传附件</div>
</button>
2026-07-01 17:57:42 +08:00
</Upload>
</Form.Item>
<Image
style={{ display: "none" }}
preview={{
visible: !!previewImage,
src: previewImage,
onVisibleChange: (visible) => {
if (!visible) setPreviewImage("");
},
}}
/>
</>
);
2026-07-02 11:00:31 +08:00
}