wiz-frameworks
Version:
wizlong react framework
169 lines (143 loc) • 6.64 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* @description 文件上传
* @author 3173
* @date 2020-06-04
*/
import React from 'react';
import { connect } from 'dva';
import 'antd/es/style/themes/default.less';
import 'antd/dist/antd.css';
import { component_tool, utils_tool } from '../../tool';
import WizOssUpload from './index';
import { WizContainer } from '../WizContainer';
var WizIcon = component_tool.WizIcon;
var transformFile = utils_tool.transformFile;
var WizFileUpload = function (_WizContainer) {
_inherits(WizFileUpload, _WizContainer);
function WizFileUpload(props) {
_classCallCheck(this, WizFileUpload);
var _this = _possibleConstructorReturn(this, (WizFileUpload.__proto__ || Object.getPrototypeOf(WizFileUpload)).call(this, props));
_this.componentName = 'WizFileUpload';
_this.propsAndStateOnChange = {
value: _this._value.bind(_this)
};
_this.handleChange = function (files) {
var limit = _this.props.limit;
var fileList = [].concat(_toConsumableArray(files));
fileList = files.slice(limit ? limit : -1);
var file = fileList[fileList.length - 1];
var onChange = _this.props.onChange;
if (file) {
_this.setState({ fileList: [file] });
} else {
_this.setState({
fileList: []
});
}
onChange && onChange(file ? [file] : []);
};
_this._beforeUpload = function (file, startUpload, removeFile) {
var beforeUpload = _this.props.beforeUpload;
var flag = true;
if (beforeUpload) {
flag = beforeUpload(file);
}
if (flag) {
_this.setState({
fileList: []
}, function () {
return startUpload(file);
});
}
return false;
};
_this.state = {
fileList: []
};
return _this;
}
_createClass(WizFileUpload, [{
key: '_componentWillMount',
value: function _componentWillMount() {
var _props = this.props,
value = _props.value,
onChange = _props.onChange;
if (value && value.length) {
// if(!value[0].uid){
// value[0].uid='0'
// }
var uidChange = false;
value.forEach(function (element) {
if (!element.uid) {
element.uid = new Date().getTime();
uidChange = true;
}
});
this.setState({
fileList: value
});
if (uidChange) {
onChange && onChange(value);
}
}
}
}, {
key: '_value',
value: function _value(nextProps, nextState) {
if (!nextProps['value'] || !nextProps['value']['length']) {
nextState['fileList'] = [];
} else {
nextProps['value'].forEach(function (element) {
if (!element.uid) {
element.uid = new Date().getTime();
}
});
nextState['fileList'] = nextProps['value'];
}
return true;
}
}, {
key: '_render',
value: function _render() {
var children = this.props.children;
var _state = this.state,
fileList = _state.fileList,
visible = _state.visible;
var v = void 0;
if (children) {
v = children;
}
return React.createElement(
React.Fragment,
null,
React.createElement(
WizOssUpload,
_extends({
name: 'file',
listType: 'text',
showUploadList: true
}, this.props, {
onChange: this.handleChange,
beforeUpload: this._beforeUpload,
value: this.state.fileList,
protect: false
}),
v
)
);
}
}]);
return WizFileUpload;
}(WizContainer);
WizFileUpload.defaultProps = {};
var mapStateToProps = function mapStateToProps(state, ownProps) {
return {};
};
var options = { withRef: true };
export default connect(mapStateToProps, null, null, options)(WizFileUpload);