UNPKG

ayongui

Version:
196 lines (195 loc) 4.99 kB
import { j as o } from "../../../react/jsx-runtime.mjs"; import { useState as h, useRef as z } from "react"; import "../../config/style.module.less.mjs"; import { formatFileSize as E, isPromise as T, readAsDataURLImg as S } from "../../utils/index.mjs"; import tt from "../button/index.mjs"; import "../icon/index.module.less.mjs"; import et from "../icon/components/upload-frame/index.mjs"; import "../icon/components/loading-frame/loading.module.less.mjs"; import y from "../message/index.mjs"; import rt from "./components/avatar-list/index.mjs"; import nt from "./components/file-list/index.mjs"; import { trackUploadProgress as at } from "./fileUpload.mjs"; const st = { default: ({ handleButtonClick: l, size: s, uplaodText: p, disabled: m }) => /* @__PURE__ */ o.jsxs(tt, { size: s, disabled: m, onClick: l, children: [ /* @__PURE__ */ o.jsx(et, {}), " ", p ] }), avatar: ({ handleButtonClick: l, disabled: s }) => null, undefined: () => { } }, Dt = ({ style: l = {}, mode: s = "default", className: p = "", uplaodText: m = "上传文件", maxFileSize: u = null, accept: w = "", data: _ = {}, size: K, multiple: it = !1, disabled: d = !1, maxCount: c = null, uplaodRender: D = null, iconRender: j = null, defaultFileList: N = [], fileList: G = [], fileListRender: k = null, action: x, method: v = "POST", name: H = "file", withCredentials: J = !0, headers: L, beforeUpload: I = () => !0, onRemove: Q = () => !1, onChange: A = () => { }, customRequest: F = null }) => { const i = { width: 86, height: 86, ...l }; s === "avatar" && (w = "image/jpeg, image/png"); const V = [...N, ...G], [O, P] = h(V), U = z(null), [B, M] = h(null), [W, X] = h(0), g = () => { U.current.click(); }, Y = (t) => { var e; R((e = t == null ? void 0 : t.target) == null ? void 0 : e.files[0]); }, f = (t) => { P((e) => c === 1 ? [t] : c && e.length >= c ? (y.warning({ message: `最多上传${c}个文件` }), e) : [...e, t]); }, R = (t) => new Promise(async (e) => { if (u && t.size / 1e3 > u) return y.warning({ message: ` 文件超出${E(u) === 0 ? 1 : E(u)}M限制` }), I(t); try { if (!await T(I, t)) return; } catch { } let n = ""; try { if (s === "avatar") { if (!t.type.includes("image")) return y.error({ message: "avatar模式下必须是图片类型" }); const a = typeof i.width == "string" ? i.width.replace("px", "") : i.width, q = typeof i.height == "string" ? i.width.replace("px", "") : i.height; n = await S(t, a, q); } } catch { } let r = { file: t, action: "", data: {}, method: v, headers: L, avatarImgURL: n, name: t.name }; if (typeof F == "function") return F(r), f(r); if (x) try { await at({ action: x, name: H, data: _, file: t, progressCallback: (a) => { r = { ...r, percent: a, status: "done", uid: Date.now() }; }, headers: L, method: v, withCredentials: J }), f(r); } catch { r = { ...r, status: "error", uid: Date.now() }, f(r); } else r = { ...r, percent: 100, status: "done", uid: Date.now() }, f(r); A(r), X((a) => a + 1), e(); }), $ = async (t, e) => { try { if (await T(Q, t)) return; } catch { } M(() => e); }, C = (t, e) => { t.animationName === "fadeOutDown" && (P((n) => n.filter((r, a) => { if (a !== e) return A(n), !0; })), M(null)); }, Z = (t) => { t.preventDefault(); }, b = async (t) => { if (d) return; t.preventDefault(); const e = Array.from(t.dataTransfer.files); for (let n = 0; n < e.length; n++) try { R(e[n]); } catch { } }; return /* @__PURE__ */ o.jsxs("div", { className: p, onDragOver: Z, onDrop: b, children: [ /* @__PURE__ */ o.jsx( "input", { type: "file", disabled: d, ref: U, accept: w, style: { display: "none" }, onChange: Y }, W ), typeof D == "function" ? D(g) : st[s]({ handleButtonClick: g, uplaodText: m, disabled: d, size: K }), s === "avatar" ? /* @__PURE__ */ o.jsx( rt, { style: i, selectedFile: O, deleteIndex: B, iconRender: j, handleButtonClick: g, fileListRender: k, onAnimationEnd: C, handleDelete: $ } ) : /* @__PURE__ */ o.jsx( nt, { selectedFile: O, deleteIndex: B, iconRender: j, fileListRender: k, onAnimationEnd: C, handleDelete: $ } ) ] }); }; export { Dt as default };