UNPKG

wiz-frameworks

Version:

wizlong react framework

189 lines (150 loc) 9.01 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _dva = require('dva'); require('antd/lib/style/themes/default.less'); require('antd/dist/antd.css'); var _tool = require('../../tool'); var _index = require('./index'); var _index2 = _interopRequireDefault(_index); var _WizContainer2 = require('../WizContainer'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } 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 */ var WizIcon = _tool.component_tool.WizIcon; var transformFile = _tool.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 _react2['default'].createElement( _react2['default'].Fragment, null, _react2['default'].createElement( _index2['default'], _extends({ name: 'file', listType: 'text', showUploadList: true }, this.props, { onChange: this.handleChange, beforeUpload: this._beforeUpload, value: this.state.fileList, protect: false }), v ) ); } }]); return WizFileUpload; }(_WizContainer2.WizContainer); WizFileUpload.defaultProps = {}; var mapStateToProps = function mapStateToProps(state, ownProps) { return {}; }; var options = { withRef: true }; exports['default'] = (0, _dva.connect)(mapStateToProps, null, null, options)(WizFileUpload); module.exports = exports['default'];