UNPKG

6.14 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6
7var _class, _temp2;
8
9import React, { Component } from 'react';
10import PropTypes from 'prop-types';
11import { func } from '../../util';
12import Uploader from './uploader';
13import Selecter from './selecter';
14
15var Html5Uploader = (_temp2 = _class = function (_Component) {
16 _inherits(Html5Uploader, _Component);
17
18 function Html5Uploader() {
19 var _temp, _this, _ret;
20
21 _classCallCheck(this, Html5Uploader);
22
23 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
24 args[_key] = arguments[_key];
25 }
26
27 return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.getUploadOptions = function (props) {
28 return {
29 action: props.action,
30 name: props.name,
31 timeout: props.timeout,
32 method: props.method,
33 beforeUpload: props.beforeUpload,
34 onProgress: props.onProgress,
35 onSuccess: props.onSuccess,
36 onError: props.onError,
37 withCredentials: props.withCredentials,
38 headers: props.headers,
39 data: props.data,
40 request: props.request
41 };
42 }, _temp), _possibleConstructorReturn(_this, _ret);
43 }
44
45 Html5Uploader.prototype.componentDidMount = function componentDidMount() {
46 var props = this.props;
47
48 var options = this.getUploadOptions(props);
49 this.uploader = new Uploader(options);
50 };
51
52 Html5Uploader.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
53 var preOptions = this.getUploadOptions(prevProps);
54 var options = this.getUploadOptions(this.props);
55
56 var keys = Object.keys(options);
57
58 for (var i = 0; i < keys.length; i++) {
59 var key = keys[i];
60 if (options[key] !== preOptions[key]) {
61 this.uploader.setOptions(options);
62 return;
63 }
64 }
65 };
66
67 Html5Uploader.prototype.componentWillUnmount = function componentWillUnmount() {
68 this.abort();
69 };
70
71 Html5Uploader.prototype.abort = function abort(file) {
72 this.uploader.abort(file);
73 };
74
75 Html5Uploader.prototype.startUpload = function startUpload(fileList) {
76 this.uploader.startUpload(fileList);
77 };
78
79 Html5Uploader.prototype.render = function render() {
80 var _props = this.props,
81 accept = _props.accept,
82 multiple = _props.multiple,
83 webkitdirectory = _props.webkitdirectory,
84 children = _props.children,
85 id = _props.id,
86 disabled = _props.disabled,
87 dragable = _props.dragable,
88 style = _props.style,
89 className = _props.className,
90 onSelect = _props.onSelect,
91 onDragOver = _props.onDragOver,
92 onDragLeave = _props.onDragLeave,
93 onDrop = _props.onDrop,
94 name = _props.name,
95 others = _objectWithoutProperties(_props, ['accept', 'multiple', 'webkitdirectory', 'children', 'id', 'disabled', 'dragable', 'style', 'className', 'onSelect', 'onDragOver', 'onDragLeave', 'onDrop', 'name']);
96
97 return React.createElement(
98 Selecter,
99 _extends({}, others, {
100 id: id,
101 accept: accept,
102 multiple: multiple,
103 webkitdirectory: webkitdirectory,
104 dragable: dragable,
105 disabled: disabled,
106 className: className,
107 style: style,
108 onSelect: onSelect,
109 onDragOver: onDragOver,
110 onDragLeave: onDragLeave,
111 onDrop: onDrop,
112 name: name
113 }),
114 children
115 );
116 };
117
118 return Html5Uploader;
119}(Component), _class.propTypes = _extends({}, Selecter.propTypes, {
120 /**
121 * 上传的地址
122 */
123 action: PropTypes.string,
124 /**
125 * 接受上传的文件类型 (image/png, image/jpg, .doc, .ppt) 详见 [input accept attribute](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input#attr-accept)
126 */
127 accept: PropTypes.string,
128 /**
129 * 上传额外传参
130 */
131 data: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
132 /**
133 * 设置上传的请求头部
134 */
135 headers: PropTypes.object,
136 /**
137 * 是否允许请求携带 cookie
138 */
139 withCredentials: PropTypes.bool,
140 /**
141 * 上传文件之前
142 * @param {Object} file 文件对象
143 * @return {Boolean} `false` 停止上传
144 */
145 beforeUpload: PropTypes.func,
146 /**
147 * 正在上传文件的钩子,参数为上传的事件以及文件
148 */
149 onProgress: PropTypes.func,
150 /**
151 * 上传成功回调函数,参数为请求下响应信息以及文件
152 */
153 onSuccess: PropTypes.func,
154 /**
155 * 上传失败回调函数,参数为上传失败的信息、响应信息以及文件
156 */
157 onError: PropTypes.func,
158 children: PropTypes.node,
159 /**
160 * 上传超时,单位ms
161 */
162 timeout: PropTypes.number,
163 /**
164 * 上传方法
165 */
166 method: PropTypes.oneOf(['post', 'put']),
167 request: PropTypes.func
168}), _class.defaultProps = _extends({}, Selecter.defaultProps, {
169 name: 'file',
170 multiple: false,
171 withCredentials: true,
172 beforeUpload: func.noop,
173 onSelect: func.noop,
174 onDragOver: func.noop,
175 onDragLeave: func.noop,
176 onDrop: func.noop,
177 onProgress: func.noop,
178 onSuccess: func.noop,
179 onError: func.noop,
180 onAbort: func.noop,
181 method: 'post'
182}), _temp2);
183Html5Uploader.displayName = 'Html5Uploader';
184export { Html5Uploader as default };
\No newline at end of file