uploadList.jsx 2.8 KB
Newer Older
A
afc163 已提交
1
import React from 'react';
J
jljsj 已提交
2
import Animate from 'rc-animate';
A
afc163 已提交
3
import Icon from '../icon';
E
elrrrrrrr 已提交
4
const prefixCls = 'ant-upload';
A
afc163 已提交
5
import { Line } from '../progress';
A
afc163 已提交
6
import classNames from 'classnames';
E
elrrrrrrr 已提交
7

8 9 10 11 12 13 14 15 16
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
const previewFile = function(file, callback) {
  const reader = new FileReader();
  reader.onloadend = function() {
    callback(reader.result);
  };
  reader.readAsDataURL(file);
};

A
afc163 已提交
17 18 19
export default React.createClass({
  getDefaultProps() {
    return {
A
afc163 已提交
20
      listType: 'text',  // or picture
X
xiaosheng.lj 已提交
21 22 23 24 25
      items: [],
      progressAttr: {
        strokeWidth: 3,
        showInfo: false
      }
A
afc163 已提交
26 27
    };
  },
A
afc163 已提交
28
  handleClose(file) {
A
afc163 已提交
29
    this.props.onRemove(file);
A
afc163 已提交
30
  },
A
afc163 已提交
31
  componentDidUpdate() {
A
afc163 已提交
32 33 34
    if (this.props.listType !== 'picture') {
      return;
    }
35
    this.props.items.forEach(file => {
A
afc163 已提交
36 37 38 39
      if (typeof document === 'undefined' ||
          typeof window === 'undefined' ||
          !window.FileReader || !window.File ||
          (!file.originFileObj instanceof File) ||
40 41 42 43 44 45 46 47 48 49
          file.thumbUrl !== undefined) {
        return;
      }
      file.thumbUrl = '';
      previewFile(file.originFileObj, (previewDataUrl) => {
        file.thumbUrl = previewDataUrl;
        this.forceUpdate();
      });
    });
  },
A
afc163 已提交
50
  render() {
51
    let list = this.props.items.map(file => {
X
xiaosheng.lj 已提交
52
      let progress;
A
afc163 已提交
53 54 55
      let icon = <Icon type="paper-clip" />;

      if (this.props.listType === 'picture') {
A
afc163 已提交
56
        icon = (file.status === 'uploading' || (!file.thumbUrl && !file.url))
A
afc163 已提交
57 58 59
          ? <Icon className={prefixCls + '-list-item-thumbnail'} type="picture" />
          : <a className={prefixCls + '-list-item-thumbnail'}
               href={file.url}
A
afc163 已提交
60
               target="_blank"><img src={file.thumbUrl || file.url} alt={file.name} /></a>;
A
afc163 已提交
61
      }
X
xiaosheng.lj 已提交
62
      if (file.status === 'uploading') {
63 64 65 66 67
        progress = (
          <div className={prefixCls + '-list-item-progress'}>
            <Line {...this.props.progressAttr} percent={file.percent} />
          </div>
        );
X
xiaosheng.lj 已提交
68
      }
A
afc163 已提交
69 70
      const infoUploadingClass = classNames({
        [`${prefixCls}-list-item`]: true,
A
afc163 已提交
71
        [`${prefixCls}-list-item-${file.status}`]: true,
A
afc163 已提交
72
      });
A
afc163 已提交
73
      return (
A
afc163 已提交
74
        <div className={infoUploadingClass} key={file.uid}>
X
xiaosheng.lj 已提交
75
          <div className={prefixCls + '-list-item-info'}>
A
afc163 已提交
76 77
            {icon}
            <span className={prefixCls + '-list-item-name'}>{file.name}</span>
A
afc163 已提交
78
            <Icon type="cross" onClick={this.handleClose.bind(this, file)} />
X
xiaosheng.lj 已提交
79 80
          </div>
          { progress }
A
afc163 已提交
81 82
        </div>
      );
A
afc163 已提交
83
    });
A
afc163 已提交
84 85 86 87
    const listClassNames = classNames({
      [`${prefixCls}-list`]: true,
      [`${prefixCls}-list-${this.props.listType}`]: true,
    });
88 89 90 91 92 93 94
    return (
      <div className={listClassNames}>
        <Animate transitionName={prefixCls + '-margin-top'}>
          {list}
        </Animate>
      </div>
    );
A
afc163 已提交
95 96
  }
});