UNPKG

@sms-frontend/components

Version:

SMS Design React UI Library.

202 lines (201 loc) 9.96 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = __importDefault(require("react")); var react_transition_group_1 = require("react-transition-group"); var request_1 = __importDefault(require("./request")); var interface_1 = require("./interface"); var is_1 = require("../_util/is"); var trigger_node_1 = __importDefault(require("./trigger-node")); var Uploader = /** @class */ (function (_super) { __extends(Uploader, _super); function Uploader(props) { var _this = _super.call(this, props) || this; // 提供 ref 调用 _this.upload = function (file) { _this.doUpload(file); }; // 提供 ref 调用。终止 _this.abort = function (file) { var req = _this.state.uploadRequests[file.uid]; if (req) { req.abort && req.abort(); _this.updateFileStatus(__assign(__assign({}, file), { status: interface_1.STATUS.fail })); _this.deleteReq(file.uid); } }; // 重新上传 。提供 ref 调用 _this.reupload = function (file) { _this.doUpload(__assign(__assign({}, file), { percent: 0, status: interface_1.STATUS.uploading })); }; _this.deleteReq = function (uid) { var newValue = __assign({}, _this.state.uploadRequests); delete newValue[uid]; _this.setState({ uploadRequests: newValue, }); }; // 提供 ref 调用 // 删除上传(手动上传时,文件会出现在上传列表,但属于init状态) _this.delete = _this.deleteReq; _this.updateFileStatus = function (file) { var onFileStatusChange = _this.props.onFileStatusChange; onFileStatusChange && onFileStatusChange(file); }; _this.getTargetFile = function (file) { var key = 'uid' in file ? 'uid' : 'name'; var targetFile = _this.props.fileList.find(function (item) { return item[key] === file[key]; }); return targetFile; }; // 执行上传 _this.doUpload = function (file) { var _a; var _b = _this.props, action = _b.action, headers = _b.headers, name = _b.name, data = _b.data, withCredentials = _b.withCredentials, customRequest = _b.customRequest; var onProgress = function (percent, event) { var targetFile = _this.getTargetFile(file); if (targetFile) { targetFile.status = interface_1.STATUS.uploading; targetFile.percent = percent; _this.props.onProgress && _this.props.onProgress(targetFile, event); } }; var onSuccess = function (response) { var targetFile = _this.getTargetFile(file); if (targetFile) { targetFile.status = interface_1.STATUS.success; // 传入的响应将会作为 response 字段被附加到上传列表中对应的文件 targetFile.response = response; _this.updateFileStatus(targetFile); } _this.deleteReq(file.uid); }; var onError = function (response) { var targetFile = _this.getTargetFile(file); if (targetFile) { targetFile.status = interface_1.STATUS.fail; // 传入的响应将会作为 response 字段被附加到上传列表中对应的文件 targetFile.response = response; _this.updateFileStatus(targetFile); } _this.deleteReq(file.uid); }; var options = { onProgress: onProgress, onSuccess: onSuccess, onError: onError, headers: headers, name: name, file: file.originFile, data: data, withCredentials: withCredentials, }; // 更新上传状态 _this.updateFileStatus(file); var request; if (action) { request = (0, request_1.default)(__assign(__assign({}, options), { action: action })); } else if (customRequest) { request = customRequest(options); } _this.setState({ uploadRequests: __assign(__assign({}, _this.state.uploadRequests), (_a = {}, _a[file.uid] = request, _a)), }); }; _this.handleFiles = function (files) { var _a = _this.props, limit = _a.limit, fileList = _a.fileList, onExceedLimit = _a.onExceedLimit, autoUpload = _a.autoUpload; if ((0, is_1.isNumber)(limit) && limit < fileList.length + files.length) { return onExceedLimit && onExceedLimit(files, fileList); } var asyncUpload = function (file, index) { var upload = { uid: "" + String(+new Date()) + index, originFile: file, percent: 0, status: interface_1.STATUS.init, name: file.name, }; // 更新上传状态为 init _this.updateFileStatus(upload); if (autoUpload) { /** * 需要setTimeout,否则一次上传较多文件时,可能出现第i个文件和第i+1个文件同时更新上传列表中的状态, * 状态被相互覆盖的情况。 */ setTimeout(function () { _this.doUpload(__assign(__assign({}, upload), { status: interface_1.STATUS.uploading })); }, 0); } }; files.forEach(function (file, index) { if ((0, is_1.isFunction)(_this.props.beforeUpload)) { // 只有在beforeUpload返回值 === false 时,取消上传操作 Promise.resolve(_this.props.beforeUpload(file, files)) .then(function (val) { if (val !== false) { var newFile = (0, is_1.isFile)(val) ? val : file; asyncUpload(newFile, index); } }) .catch(function (e) { console.error(e); }); } else { asyncUpload(file, index); } }); }; _this.state = { uploadRequests: {}, }; return _this; } Uploader.prototype.render = function () { var _this = this; var _a = this.props, accept = _a.accept, multiple = _a.multiple, children = _a.children, prefixCls = _a.prefixCls, tip = _a.tip, disabled = _a.disabled, drag = _a.drag, listType = _a.listType, fileList = _a.fileList, directory = _a.directory, limit = _a.limit; return (react_1.default.createElement(react_1.default.Fragment, null, react_1.default.createElement("input", __assign({ key: "trigger-input", ref: function (node) { return (_this.inputRef = node); }, style: { display: 'none' }, type: "file", accept: accept, multiple: multiple }, (directory ? { webkitdirectory: 'webkitdirectory' } : {}), { onChange: function (e) { var files = e.target.files; if (files) { _this.handleFiles([].slice.call(files)); _this.inputRef.value = ''; } } })), react_1.default.createElement(react_transition_group_1.CSSTransition, { key: "trigger-node", in: !((0, is_1.isNumber)(limit) && limit <= fileList.length), timeout: 100, unmountOnExit: true, classNames: "fadeIn" }, react_1.default.createElement(trigger_node_1.default, { directory: directory, tip: tip, multiple: multiple, accept: accept, disabled: disabled, drag: drag, listType: listType, onDragFiles: this.handleFiles, onClick: function () { !disabled && _this.inputRef && _this.inputRef.click(); }, prefixCls: prefixCls }, children)), tip && listType !== 'picture-card' && !drag ? (react_1.default.createElement("div", { key: "trigger-tip", className: prefixCls + "-trigger-tip" }, tip)) : null)); }; return Uploader; }(react_1.default.Component)); exports.default = Uploader;