UNPKG

9.74 kBTypeScriptView Raw
1/// <reference types="react" />
2
3import * as React from 'react';
4import CommonProps from '../util';
5import { ProgressProps } from '../progress';
6
7interface HTMLAttributesWeak extends React.HTMLAttributes<HTMLElement> {
8 onError?: any;
9 onSelect?: any;
10 defaultValue?: any;
11 onChange?: any;
12}
13
14export interface CardProps extends HTMLAttributesWeak, CommonProps {
15 /**
16 * 上传的地址
17 */
18 action?: string;
19
20 /**
21 * 是否支持多选文件,`ie10+` 支持。开启后按住 ctrl 可选择多个文件
22 */
23 multiple?: boolean;
24
25 /**
26 * 展示下载按钮
27 */
28 showDownload?: boolean;
29
30 /**
31 * 接受上传的文件类型 (image/png, image/jpg, .doc, .ppt) 详见 [input accept attribute](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input#attr-accept)
32 */
33 accept?: string;
34
35 /**
36 * 上传额外传参
37 */
38 data?: any | (() => void);
39
40 /**
41 * 设置上传的请求头部
42 */
43 headers?: any;
44
45 /**
46 * 是否允许请求携带 cookie
47 */
48 withCredentials?: boolean;
49
50 /**
51 * 可选参数, 详见 [beforeUpload](#beforeUpload)
52 */
53 beforeUpload?: (file: {}, options: {}) => boolean | {} | any;
54
55 /**
56 * 上传中
57 */
58 onProgress?: () => void;
59
60 /**
61 * 可选参数,上传成功回调函数,参数为请求下响应信息以及文件
62 */
63 onSuccess?: (file: {}, value: Array<any>) => void;
64
65 /**
66 * 可选参数,上传失败回调函数,参数为上传失败的信息、响应信息以及文件
67 */
68 onError?: (file: {}, value: Array<any>) => void;
69
70 /**
71 * 子元素
72 */
73 children?: React.ReactNode;
74
75 /**
76 * 设置上传超时,单位ms
77 */
78 timeout?: number;
79
80 /**
81 * 上传方法
82 */
83 method?: 'post' | 'put';
84
85 /**
86 * 自定义上传方法
87 */
88 request?: (option: {}) => void;
89
90 /**
91 * 文件名字段
92 */
93 name?: string;
94
95 /**
96 * 选择文件回调
97 */
98 onSelect?: () => void;
99
100 /**
101 * 放文件
102 */
103 onDrop?: () => void;
104
105 /**
106 * 样式前缀
107 */
108 prefix?: string;
109
110 /**
111 * 文件列表
112 */
113 value?: Array<any>;
114
115 /**
116 * 默认文件列表
117 */
118 defaultValue?: Array<any>;
119
120 /**
121 * 上传按钮形状
122 */
123 shape?: 'card';
124
125 /**
126 * 上传列表的样式
127 */
128 listType?: 'text' | 'image' | 'card';
129
130 /**
131 * 数据格式化函数,配合自定义 action 使用,参数为服务器的响应数据,详见 [formatter](#formater)
132 */
133 formatter?: (response: {}, file: any) => void;
134
135 /**
136 * 最大文件上传个数
137 */
138 limit?: number;
139
140 /**
141 * 可选参数,是否支持拖拽上传,`ie10+` 支持。
142 */
143 dragable?: boolean;
144
145 /**
146 * 可选参数,是否本地预览
147 */
148 useDataURL?: boolean;
149
150 /**
151 * 可选参数,是否禁用上传功能
152 */
153 disabled?: boolean;
154
155 /**
156 * 改变时候的回调
157 */
158 onChange?: (value: File[]) => void;
159
160 /**
161 * 可选参数, 用于校验文件,afterSelect仅在 autoUpload=false 的时候生效,autoUpload=true时,可以使用beforeUpload完全可以替代该功能.
162 */
163 afterSelect?: (file: {}) => boolean;
164
165 /**
166 * 点击移除的回调
167 */
168 onRemove?: () => void;
169
170 /**
171 * 自定义class
172 */
173 className?: string;
174
175 /**
176 * 自定义内联样式
177 */
178 style?: React.CSSProperties;
179
180 /**
181 * 自动上传
182 */
183 autoUpload?: boolean;
184
185 /**
186 * 透传给Progress props
187 */
188 progressProps?: ProgressProps;
189
190 /**
191 * 点击图片回调
192 */
193 onPreview?: () => void;
194
195 /**
196 * 取消上传的回调
197 */
198 onCancel?: () => void;
199 /**
200 * 调用系统设备媒体
201 */
202 capture?: string;
203}
204
205export class Card extends React.Component<CardProps, any> {}
206
207export class Dragger extends React.Component<UploadProps, any> {}
208
209export interface SelecterProps extends HTMLAttributesWeak, CommonProps {
210 /**
211 * 是否禁用上传功能
212 */
213 disabled?: boolean;
214
215 /**
216 * 是否支持多选文件,`ie10+` 支持。开启后按住 ctrl 可选择多个文件
217 */
218 multiple?: boolean;
219
220 /**
221 * 是否支持拖拽上传,`ie10+` 支持。
222 */
223 dragable?: boolean;
224
225 /**
226 * 接受上传的文件类型 (image/png, image/jpg, .doc, .ppt) 详见 [input accept attribute](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input#attr-accept)
227 */
228 accept?: string;
229
230 /**
231 * 文件选择回调
232 */
233 onSelect?: (e: React.ChangeEvent<HTMLInputElement>) => void;
234
235 /**
236 * 拖拽经过回调
237 */
238 onDragOver?: () => void;
239
240 /**
241 * 拖拽离开回调
242 */
243 onDragLeave?: () => void;
244
245 /**
246 * 拖拽完成回调
247 */
248 onDrop?: () => void;
249
250 /**
251 * 是否支持上传文件夹,仅在 chorme 下生效
252 */
253 webkitdirectory?: boolean;
254}
255
256export class Selecter extends React.Component<SelecterProps, any> {}
257
258export class Uploader {
259 /**
260 * @param options 配置
261 */
262 constructor(options?: any);
263
264 /**
265 * 配置选项
266 * @param options 配置
267 */
268 setOptions(options: any): void;
269 /**
270 * 开始上传
271 * @param files 文件列表
272 */
273 startUpload(files: Array<any>): void;
274 /**
275 * 中断某个文件上传
276 * @param file 文件
277 */
278 abort(file: any): void;
279}
280
281export interface UploadProps extends HTMLAttributesWeak, CommonProps {
282 /**
283 * 上传的地址
284 */
285 action?: string;
286
287 /**
288 * 接受上传的文件类型 (image/png, image/jpg, .doc, .ppt) 详见 [input accept attribute](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input#attr-accept)
289 */
290 accept?: string;
291
292 /**
293 * 上传额外传参
294 */
295 data?: any | (() => void);
296
297 /**
298 * 是否支持多选文件,`ie10+` 支持。开启后按住 ctrl 可选择多个文件
299 */
300 multiple?: boolean;
301
302 /**
303 * 设置上传的请求头部
304 */
305 headers?: any;
306
307 /**
308 * 是否允许请求携带 cookie
309 */
310 withCredentials?: boolean;
311
312 /**
313 * 可选参数, 详见 [beforeUpload](#beforeUpload)
314 */
315 beforeUpload?: (file: any, options: any) => boolean | {} | any;
316
317 /**
318 * 上传中
319 */
320 onProgress?: () => void;
321
322 /**
323 * 可选参数,上传成功回调函数,参数为请求下响应信息以及文件
324 */
325 onSuccess?: (file: any, value: Array<any>) => void;
326
327 /**
328 * 可选参数,上传失败回调函数,参数为上传失败的信息、响应信息以及文件
329 */
330 onError?: (file: any, value: Array<any>) => void;
331
332 /**
333 * 子元素
334 */
335 children?: React.ReactNode;
336
337 /**
338 * 设置上传超时,单位ms
339 */
340 timeout?: number;
341
342 /**
343 * 上传方法
344 */
345 method?: 'post' | 'put';
346
347 /**
348 * 自定义上传方法
349 */
350 request?: (option: any) => any;
351
352 /**
353 * 文件名字段
354 */
355 name?: string;
356
357 /**
358 * 选择文件回调
359 */
360 onSelect?: (uploadFiles: Array<any>, value: Array<any>) => void;
361
362 /**
363 * 放文件
364 */
365 onDrop?: () => void;
366
367 /**
368 * 样式前缀
369 */
370 prefix?: string;
371
372 /**
373 * 文件列表
374 */
375 value?: Array<any>;
376
377 /**
378 * 默认文件列表
379 */
380 defaultValue?: Array<any>;
381
382 /**
383 * 上传按钮形状
384 */
385 shape?: 'card';
386
387 /**
388 * 上传列表的样式
389 */
390 listType?: 'text' | 'image' | 'card';
391
392 /**
393 * 数据格式化函数,配合自定义 action 使用,参数为服务器的响应数据,详见 [formatter](#formater)
394 */
395 formatter?: (response: any, file: any) => void;
396
397 /**
398 * 最大文件上传个数
399 */
400 limit?: number;
401
402 /**
403 * 可选参数,是否支持拖拽上传,`ie10+` 支持。
404 */
405 dragable?: boolean;
406
407 /**
408 * 可选参数,是否本地预览
409 */
410 useDataURL?: boolean;
411
412 /**
413 * 可选参数,是否禁用上传功能
414 */
415 disabled?: boolean;
416
417 /**
418 * 上传文件改变时的状态
419 */
420 onChange?: (value: File[]) => void;
421
422 /**
423 * 可选参数, 用于校验文件,afterSelect仅在 autoUpload=false 的时候生效,autoUpload=true时,可以使用beforeUpload完全可以替代该功能.
424 */
425 afterSelect?: (file: any) => boolean;
426
427 /**
428 * 移除文件回调函数
429 */
430 onRemove?: (file: any) => boolean | any;
431
432 /**
433 * 自定义额外渲染
434 */
435 extraRender?: (file: File) => any;
436 /**
437 * 自定义文件名渲染
438 */
439 fileNameRender?: (file: File) => any;
440 /**
441 * 自定义操作区域渲染
442 */
443 actionRender?: (file: File) => any;
444 /**
445 * 自定义class
446 */
447 className?: string;
448
449 /**
450 * 自定义内联样式
451 */
452 style?: React.CSSProperties;
453
454 /**
455 * 自动上传
456 */
457 autoUpload?: boolean;
458
459 /**
460 * 透传给Progress props
461 */
462 progressProps?: ProgressProps;
463
464 /**
465 * 是否为预览态
466 */
467 isPreview?: boolean;
468
469 /**
470 * 预览态模式下渲染的内容
471 */
472 renderPreview?: (value: number) => void;
473
474 /**
475 * 文件对象的 key name
476 */
477 fileKeyName?: string;
478 /**
479 * 点击文件名时触发 onPreview
480 * @version 1.24
481 */
482 previewOnFileName?: boolean,
483}
484
485export default class Upload extends React.Component<UploadProps, any> {
486 static Card: typeof Card;
487 static Dragger: typeof Dragger;
488 static Selecter: typeof Selecter;
489 static Uploader: typeof Uploader;
490}