1 | import React from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import { Button } from 'reactstrap';
|
4 | import UploadProgressBar from './UploadProgressBar';
|
5 |
|
6 | const fileTypeIconMap = {
|
7 | png: 'file-image',
|
8 | jpg: 'file-image',
|
9 | jpeg: 'file-image',
|
10 | gif: 'file-image',
|
11 | ppt: 'file-powerpoint',
|
12 | pptx: 'file-powerpoint',
|
13 | xls: 'file-excel',
|
14 | xlsx: 'file-excel',
|
15 | doc: 'file-word',
|
16 | docx: 'file-word',
|
17 | txt: 'doc-alt',
|
18 | text: 'doc-alt',
|
19 | zip: 'file-archive',
|
20 | '7zip': 'file-archive',
|
21 | xml: 'file-code',
|
22 | html: 'file-code',
|
23 | pdf: 'file-pdf',
|
24 | };
|
25 |
|
26 | const FileRow = ({ onRemove, children, file }) => {
|
27 | const remove = () => {
|
28 | onRemove(file.id);
|
29 | };
|
30 |
|
31 | const progressBar = () => <UploadProgressBar upload={file} />;
|
32 |
|
33 | const ext = file.file.name
|
34 | .split('.')
|
35 | .pop()
|
36 | .toUpperCase();
|
37 |
|
38 | const icon = fileTypeIconMap[ext.toLowerCase()] || 'doc';
|
39 |
|
40 | if (typeof children === 'function') {
|
41 | return children({
|
42 | file,
|
43 | metadata: file.options.metadata,
|
44 | name: file.file.name,
|
45 | remove,
|
46 | ext,
|
47 | icon,
|
48 | progressBar,
|
49 | });
|
50 | }
|
51 |
|
52 | return (
|
53 | <tr>
|
54 | <td className="align-middle" style={{ width: '10%' }}>
|
55 | <i className={`icon icon-${icon}`} title={`${ext} File Icon`}>
|
56 | <span className="sr-only">{ext} File Icon</span>
|
57 | </i>{' '}
|
58 | </td>
|
59 | <td className="align-middle" style={{ width: '35%' }}>
|
60 | <div className="text-truncate" title={file.file.name}>
|
61 | {file.file.name}
|
62 | </div>
|
63 | </td>
|
64 | <td className="align-middle" style={{ width: '45%' }}>
|
65 | <UploadProgressBar upload={file} />
|
66 | </td>
|
67 | <td className="align-middle" style={{ width: '10%' }}>
|
68 | <Button
|
69 | data-testid="remove-file-btn"
|
70 | color="link"
|
71 | className="text-danger px-0"
|
72 | onClick={remove}
|
73 | >
|
74 | <i className="icon icon-trash-empty">
|
75 | <span className="sr-only">
|
76 | Remove
|
77 | {file.file.name}
|
78 | </span>
|
79 | </i>
|
80 | </Button>
|
81 | </td>
|
82 | </tr>
|
83 | );
|
84 | };
|
85 |
|
86 | FileRow.propTypes = {
|
87 | onRemove: PropTypes.func.isRequired,
|
88 | children: PropTypes.func,
|
89 | file: PropTypes.shape({
|
90 | id: PropTypes.string.isRequired,
|
91 | file: PropTypes.shape({
|
92 | name: PropTypes.string.isRequired,
|
93 | }).isRequired,
|
94 | options: PropTypes.object,
|
95 | }),
|
96 | };
|
97 |
|
98 | export default FileRow;
|