@jdcfe/yep-react
Version:
一套移动端的React组件库
145 lines (123 loc) • 5.15 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import * as React from 'react';
import classNames from 'classnames';
import noop from '../_utils/noop';
var ImagePicker = /*#__PURE__*/function (_React$PureComponent) {
_inherits(ImagePicker, _React$PureComponent);
var _super = _createSuper(ImagePicker);
function ImagePicker(props) {
var _this;
_classCallCheck(this, ImagePicker);
_this = _super.call(this, props);
_this.state = {};
_this.fileChange = _this.fileChange.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(ImagePicker, [{
key: "removeImagePriv",
value: function removeImagePriv(index) {
var _this$props$removeIma = this.props.removeImage,
removeImage = _this$props$removeIma === void 0 ? function () {} : _this$props$removeIma;
removeImage(index);
}
}, {
key: "fileChange",
value: function fileChange(e) {
var _this$props = this.props,
name = _this$props.name,
formDataAction = _this$props.formDataAction,
customSize = _this$props.customSize,
fileLimitCallback = _this$props.fileLimitCallback;
var file = e.target.files[0];
var imgFormData = new FormData(); // 判断上传文件的类型,若不是jpeg jpg png gif类型,不执行上传操作
if (file && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
return;
}
imgFormData.append("".concat(name), file && file); // 若customSize设置为0表示不限制图片大小,不设置或者设置为其他值表示采用默认图片大小
if (customSize !== 0) {
fileLimitCallback && fileLimitCallback(file && file);
if (file && file.size > customSize * 1024) {
return;
}
}
var reader = new FileReader();
reader.onload = function (e) {
var currentTarget = e.currentTarget;
var dataUrl = currentTarget && currentTarget.result;
if (!dataUrl) {
return;
}
formDataAction && formDataAction(imgFormData, {
url: dataUrl
});
};
file && reader.readAsDataURL(file);
if (this.input) {
this.input.value = '';
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
files = _this$props2.files,
name = _this$props2.name,
height = _this$props2.height,
width = _this$props2.width;
var wrapperCls = classNames('Yep-upload-wrapper');
return /*#__PURE__*/React.createElement("div", {
className: wrapperCls
}, files && files.map(function (imgItem, index) {
return /*#__PURE__*/React.createElement("div", {
className: "upload-flex-item",
key: index
}, /*#__PURE__*/React.createElement("div", {
className: "upload-flex-item-del",
onClick: function onClick() {
_this2.removeImagePriv(index);
}
}), /*#__PURE__*/React.createElement("img", {
className: "upload-flex-item-img",
src: "".concat(imgItem.url),
style: {
width: width,
height: height
}
}));
}), /*#__PURE__*/React.createElement("div", {
className: "upload-flex-item upload-add"
}, /*#__PURE__*/React.createElement("div", {
className: "upload-add-picker",
style: {
width: width,
height: height
}
}, /*#__PURE__*/React.createElement("input", {
ref: function ref(input) {
_this2.input = input;
},
type: "file",
name: name,
accept: "image/*",
onChange: this.fileChange
}))));
}
}]);
return ImagePicker;
}(React.PureComponent);
export { ImagePicker as default };
ImagePicker.defaultProps = {
files: [],
width: '75px',
height: '75px',
customSize: 1024,
removeImage: noop
};