aurora-topu
Version:
React component library
160 lines (159 loc) • 6.21 kB
JavaScript
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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useRef, useState } from 'react';
import axios from 'axios';
import Dragger from './dragger';
import UploadList from './uploadList';
import './_style.scss';
var AuroraUpload = function (props) {
var accept = props.accept, multiple = props.multiple, defaultFileList = props.defaultFileList, action = props.action, name = props.name, headers = props.headers, withCredentials = props.withCredentials, onProgress = props.onProgress, onSuccess = props.onSuccess, onChange = props.onChange, data = props.data, onError = props.onError, beforeUpload = props.beforeUpload, drag = props.drag, children = props.children, onRemove = props.onRemove;
var fileInput = useRef(null);
var _a = useState(defaultFileList || []), fileList = _a[0], setFileList = _a[1];
var handleRemove = function (file) {
setFileList(function (prevList) {
return prevList.filter(function (item) { return item.uid !== file.uid; });
});
if (onRemove) {
onRemove(file);
}
};
/**
* 文件上传
*/
var uploadFiles = function (files) {
var postFiles = Array.from(files);
postFiles.forEach(function (file) {
if (!beforeUpload) {
post(file);
}
else {
var result = beforeUpload(file);
if (result && result instanceof Promise) {
result.then(function (processedFile) {
post(processedFile);
});
}
else if (result) {
post(file);
}
}
});
};
var handleClick = function () {
if (fileInput.current) {
fileInput.current.click();
}
};
var handleFileChange = function (e) {
var files = e.target.files;
if (!files) {
return;
}
uploadFiles(files);
if (fileInput.current) {
fileInput.current.value = '';
}
};
/**
* 更新指定uid的文件的状态
* @param updateFile
* @param updateObj
*/
var updateFileList = function (updateFile, updateObj) {
setFileList(function (prevList) {
return prevList.map(function (file) {
if (file.uid === updateFile.uid) {
return __assign(__assign({}, file), updateObj);
}
else {
return file;
}
});
});
};
/**
* 对后端进行交互,将文件上传到服务器
* @param file
*/
var post = function (file) {
var _file = {
uid: Date.now() + 'upload_file',
status: 'ready',
name: file.name,
size: file.size,
percent: 0,
raw: file,
};
setFileList(function (prevList) {
return __spreadArray([_file], prevList, true);
});
var formData = new FormData();
formData.append(name || 'file', file);
// 如果用户自己配置了额外数据data,那么就将额外数据添加到formData中
if (data) {
Object.keys(data).forEach(function (key) {
formData.append(key, data[key]);
});
}
axios
.post(action, formData, {
headers: __assign(__assign({}, headers), { 'Content-Type': 'multipart/form-data' }),
withCredentials: withCredentials,
onUploadProgress: function (e) {
if (e.total !== undefined) {
var percentage = Math.round((e.loaded * 100) / e.total) || 0;
if (percentage < 100) {
updateFileList(_file, { percent: percentage, status: 'uploading' });
}
if (onProgress) {
onProgress(percentage, file);
}
}
},
})
.then(function (resp) {
updateFileList(_file, { status: 'success', response: resp.data });
if (onSuccess) {
onSuccess(resp.data, file);
}
if (onChange) {
onChange(file);
}
})
.catch(function (err) {
updateFileList(_file, { status: 'error', error: err });
if (onError) {
onError(err, file);
}
if (onChange) {
onChange(file);
}
});
};
return (React.createElement("div", { className: 'viking-upload-component' },
React.createElement("div", { className: "viking-upload-input", style: { display: 'inline-block' }, onClick: handleClick },
drag ? (React.createElement(Dragger, { onFile: function (files) {
uploadFiles(files);
} }, children)) : (children),
React.createElement("input", { className: "viking-file-input", style: { display: 'none' }, ref: fileInput, onChange: handleFileChange, type: "file", accept: accept, multiple: multiple })),
React.createElement(UploadList, { fileList: fileList, onRemove: handleRemove })));
};
export default React.memo(AuroraUpload);