fileListItem.js 3.67 KB
Newer Older
周宏民's avatar
周宏民 committed
1 2 3 4 5
/*
 * @Title:
 * @Author: hongmye
 * @Date: 2022-03-21 14:44:49
 */
周宏民's avatar
周宏民 committed
6 7 8
import { DeleteOutlined, DownloadOutlined, PictureOutlined } from '@ant-design/icons';
import { Button, ConfigProvider } from 'antd';
import { useContext, useEffect, useState } from 'react';
周宏民's avatar
周宏民 committed
9
import iconFile from './icon_file.png';
周宏民's avatar
周宏民 committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
function FileListItem(props) {
  const [fileList, setFileList] = useState([]);
  const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls();

  const imgType = ['image/png', 'image/jpg', 'image/jpeg'];
  const onPreview = (item) => {
    props.onPreview(item);
  };
  const onDownLoad = (item) => {
    const a = document.createElement('a');
    a.setAttribute('download', item.name);
    a.setAttribute('href', item.path);
    document.documentElement.appendChild(a);
    a.target = '_black';
    a.click();
  };
  useEffect(() => {
    setFileList(props.list || []);
  }, [props.list]);
  return (
    <>
      {fileList.map((item, index) => {
        return (
          <div key={index} className={`${prefixCls}-upload-list ${prefixCls}-upload-list-picture`}>
            <div className={`${prefixCls}-upload-list-picture-container`}>
              <div className={`${prefixCls}-upload-list-item`}>
                <div className={`${prefixCls}-upload-list-item-info`}>
                  <span className={`${prefixCls}-upload-span`}>
                    <div
                      className={`${prefixCls}-upload-list-item-thumbnail ${prefixCls}-upload-list-item-file`}
                    >
                      {/* {imgType.includes(item.type) ? <FileImageTwoTone /> : <FileTwoTone />} */}
                      <img src={iconFile} />
周宏民's avatar
周宏民 committed
44
                    </div>
周宏民's avatar
周宏民 committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
                    <span
                      className={`${prefixCls}-upload-list-item-name`}
                      title={item.name}
                      style={{ cursor: 'pointer' }}
                    >
                      {item.name}
                    </span>
                    <span className={`${prefixCls}-upload-list-item-card-actions picture`}>
                      {imgType.includes(item.type) && (
                        <Button
                          title="预览"
                          type="text"
                          onClick={(e) => {
                            e && e.stopPropagation();
                            onPreview(item);
                          }}
                          style={{ padding: '4px 8px' }}
                        >
                          <PictureOutlined />
                        </Button>
                      )}
                      <Button
                        title="下载"
                        type="text"
                        onClick={(e) => {
                          e && e.stopPropagation();
                          onDownLoad(item);
                        }}
                        style={{ padding: '4px 8px' }}
                      >
                        <DownloadOutlined />
                      </Button>
                      {props.type === 'edit' && (
                        <Button
                          title="删除文件"
                          type="text"
                          onClick={(e) => {
                            e && e.stopPropagation();
                            props.onDel(item);
                          }}
                          style={{ padding: '4px 8px' }}
                        >
                          <DeleteOutlined />
                        </Button>
                      )}
                    </span>
                  </span>
                </div>
              </div>
            </div>
          </div>
        );
      })}
    </>
  );
周宏民's avatar
周宏民 committed
100
}
周宏民's avatar
周宏民 committed
101
export default FileListItem;