@grc/business
Version:
更新sider选项 : forceSubMenuRender
100 lines (99 loc) • 3.92 kB
JavaScript
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 (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import React, { PureComponent } from "react";
import { getClassNames, httpClient } from "@grc/base/components/utils";
var UploadComponent = /** @class */ (function (_super) {
__extends(UploadComponent, _super);
function UploadComponent(props, context) {
var _this = _super.call(this, props, context) || this;
// input元素
_this._inputRef = null;
// 准备上传的文件
_this._file = null;
_this._saveInputField = _this._saveInputField.bind(_this);
_this._onChange = _this._onChange.bind(_this);
return _this;
}
/**
* 获取input元素
*/
UploadComponent.prototype._saveInputField = function (node) {
this._inputRef = node;
};
/**
* 文件变化
*/
UploadComponent.prototype._onChange = function (event) {
var onChange = this.props.onChange;
var files = event.target.files;
this._file = files ? files[0] : null;
onChange && onChange(this._file);
};
/**
* 上传
* @param data 其他参数
*/
UploadComponent.prototype.upload = function (data) {
if (!this._file)
return false;
var _a = this.props, _b = _a.name, name = _b === void 0 ? "file" : _b, action = _a.action, onFail = _a.onFail, onSuccess = _a.onSuccess, withCredentials = _a.withCredentials, formateResponse = _a.formateResponse;
var formData = new FormData();
var file = this._file;
formData.append(name, file);
if (data)
Object.keys(data)
.forEach(function (key) {
formData.append(key, data[key]);
});
httpClient.post(action, formData, {
withCredentials: withCredentials
})
.subscribe(function (result) {
if (typeof formateResponse !== "function")
formateResponse = function (response) { return response; };
var formateResult = formateResponse.call(void 0, result);
file.result = formateResult.result;
file.response = formateResult.response;
if (formateResult.isSuccess) {
onSuccess && onSuccess(file, formateResult.result);
}
else {
onFail && onFail(file, formateResult.result, formateResult.response);
}
});
};
/**
* 重置
*/
UploadComponent.prototype.reset = function () {
this._file = null;
if (!this._inputRef)
return;
this._inputRef.value = "";
};
UploadComponent.prototype.render = function () {
var _a;
var _b = this.props, prefixCls = _b.prefixCls, disabled = _b.disabled, children = _b.children;
var wrapperClassName = getClassNames((_a = {},
_a[prefixCls + "-upload"] = true,
_a.disabled = !!disabled,
_a));
return (React.createElement("div", { className: wrapperClassName },
React.createElement("div", { className: prefixCls + "-upload_inner" },
React.createElement("input", { type: "file", ref: this._saveInputField, multiple: false, disabled: disabled, onChange: this._onChange }),
children)));
};
return UploadComponent;
}(PureComponent));
export default UploadComponent;