shineout
Version:
Shein 前端组件库
152 lines (127 loc) • 5.08 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { isValidElement } from 'react';
import Spin from '../Spin';
import { PureComponent } from '../component';
import { uploadClass } from './styles';
import Button from '../Button';
import Upload from './Upload';
import { isRTL } from '../config';
var SPIN = function SPIN(color) {
return React.createElement("span", {
className: uploadClass('bg-spin')
}, React.createElement(Spin, {
size: 10,
name: "ring",
color: color
}));
};
var handleKeyDown = function handleKeyDown(e) {
if (e.keyCode === 13) e.target.click();
};
var DefaultProps = {
type: 'primary',
size: 'default',
outline: false
};
var Progress =
/*#__PURE__*/
function (_PureComponent) {
_inheritsLoose(Progress, _PureComponent);
function Progress(props) {
var _this;
_this = _PureComponent.call(this, props) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleStart", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleOver", void 0);
_this.state = {
progress: -1
};
_this.handleError = _this.handleError.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.handleSuccess = _this.handleSuccess.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.handleProgress = _this.handleProgress.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.handleStart = _this.handleChange.bind(_assertThisInitialized(_assertThisInitialized(_this)), 0);
_this.handleOver = _this.handleChange.bind(_assertThisInitialized(_assertThisInitialized(_this)), -1);
_this.handleUpload = _this.handleUpload.bind(_assertThisInitialized(_assertThisInitialized(_this)));
return _this;
}
var _proto = Progress.prototype;
_proto.handleChange = function handleChange(p) {
this.setState({
progress: p
});
};
_proto.handleProgress = function handleProgress(file) {
this.handleChange(file.process);
};
_proto.handleError = function handleError(xhr, file) {
var onError = this.props.onError;
var msg;
if (onError) msg = onError(xhr, file);
this.handleOver();
return msg;
};
_proto.handleSuccess = function handleSuccess(value) {
var onSuccess = this.props.onSuccess;
var result = value;
if (onSuccess) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
// @ts-ignore
result = onSuccess.apply(void 0, [value].concat(args));
}
this.handleOver();
return result;
};
_proto.handleUpload = function handleUpload(e) {
var uploading = this.state.progress >= 0;
if (uploading) e.stopPropagation();
};
_proto.renderLoadingView = function renderLoadingView(color) {
var _this$props = this.props,
placeholder = _this$props.placeholder,
loading = _this$props.loading;
return isValidElement(loading) ? React.createElement("span", null, loading) : React.createElement("span", null, SPIN(color), typeof loading === 'string' ? loading : placeholder);
};
_proto.render = function render() {
var _style;
var _this$props2 = this.props,
placeholder = _this$props2.placeholder,
type = _this$props2.type,
size = _this$props2.size,
outline = _this$props2.outline,
others = _objectWithoutPropertiesLoose(_this$props2, ["placeholder", "type", "size", "outline"]);
var uploading = this.state.progress >= 0;
var style = (_style = {}, _style[isRTL() ? 'left' : 'right'] = uploading ? 100 - this.state.progress + "%" : '100%', _style);
return React.createElement(Upload, _extends({}, others, {
limit: undefined,
onProgress: this.handleProgress,
onStart: this.handleStart,
showUploadList: false,
onError: this.handleError,
onSuccess: this.handleSuccess
}), React.createElement(Button, {
tabIndex: others.disabled ? -1 : 0,
disabled: others.disabled,
className: uploadClass('button', uploading && 'uploading'),
type: type,
size: size,
outline: outline,
onClick: this.handleUpload,
onKeyDown: handleKeyDown
}, uploading && [React.createElement("div", {
key: "cover",
className: uploadClass('cover')
}), React.createElement("div", {
key: "bg",
style: style,
className: uploadClass('bg')
}, this.renderLoadingView('#fff'))], React.createElement("span", null, uploading ? this.renderLoadingView() : placeholder)));
};
return Progress;
}(PureComponent);
_defineProperty(Progress, "defaultProps", DefaultProps);
export default Progress;