siam-ui-utils 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/dropzone-uploader/index.jsx +144 -132
package/package.json
CHANGED
|
@@ -1,138 +1,150 @@
|
|
|
1
1
|
/* eslint-disable react/prop-types */
|
|
2
|
-
import React, { useEffect, useState } from 'react'
|
|
3
|
-
import { getDroppedOrSelectedFiles } from 'html5-file-selector'
|
|
4
|
-
import Dropzone from 'react-dropzone-uploader'
|
|
5
|
-
import { NotificationManager } from 'react-notifications'
|
|
6
|
-
import 'react-dropzone-uploader/dist/styles.css'
|
|
7
|
-
import
|
|
8
|
-
import { pdfImage } from '../constants-svg'
|
|
9
|
-
import
|
|
2
|
+
import React, { useEffect, useState } from 'react';
|
|
3
|
+
import { getDroppedOrSelectedFiles } from 'html5-file-selector';
|
|
4
|
+
import Dropzone from 'react-dropzone-uploader';
|
|
5
|
+
import { NotificationManager } from 'react-notifications';
|
|
6
|
+
import 'react-dropzone-uploader/dist/styles.css';
|
|
7
|
+
import IconButtonSvg from '../iconos/icon-button-svg';
|
|
8
|
+
import { pdfImage } from '../constants-svg';
|
|
9
|
+
import './dropzone-uploader.css';
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
console.log('dropzone1 :>> ');
|
|
12
|
+
|
|
13
|
+
const maxSize = 7; //en MB
|
|
12
14
|
export const DropzoneUploader = ({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
onChangeFiles,
|
|
16
|
+
totalFiles,
|
|
17
|
+
maxFiles = 3,
|
|
18
|
+
accept = 'image/*, application/pdf',
|
|
19
|
+
capture = null,
|
|
20
|
+
nameFileLabel = 'Subir archivos',
|
|
21
|
+
maxFileSize = maxSize,
|
|
20
22
|
}) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
23
|
+
console.log('dropzone2 :>> ');
|
|
24
|
+
|
|
25
|
+
const [files, setFiles] = useState([]);
|
|
26
|
+
const [totalFilesDU, setTotalFilesDU] = useState(totalFiles);
|
|
27
|
+
const handleChangeStatus = ({ file }, status) => {
|
|
28
|
+
if (status == 'done') {
|
|
29
|
+
const filesTemp = files;
|
|
30
|
+
filesTemp.push(file);
|
|
31
|
+
setFiles(filesTemp);
|
|
32
|
+
onChangeFiles(files);
|
|
33
|
+
setTotalFilesDU(files.length);
|
|
34
|
+
} else if (status == 'removed') {
|
|
35
|
+
const filesTemp = files;
|
|
36
|
+
const index = files.indexOf(file);
|
|
37
|
+
if (index > -1) {
|
|
38
|
+
filesTemp.splice(index, 1);
|
|
39
|
+
}
|
|
40
|
+
setFiles(filesTemp);
|
|
41
|
+
onChangeFiles(files);
|
|
42
|
+
setTotalFilesDU(files.length);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const getFilesFromEvent = (e) => {
|
|
46
|
+
console.log('dropzone3 :>> ');
|
|
47
|
+
|
|
48
|
+
return new Promise((resolve) => {
|
|
49
|
+
getDroppedOrSelectedFiles(e).then((chosenFiles) => {
|
|
50
|
+
resolve(chosenFiles.map((f) => f.fileObject));
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
const CustomPreview = ({ fileWithMeta }) => {
|
|
55
|
+
console.log('dropzone4 :>> ');
|
|
56
|
+
|
|
57
|
+
const { meta, remove } = fileWithMeta;
|
|
58
|
+
if (meta.size / (1024 * 1024) > maxFileSize) {
|
|
59
|
+
NotificationManager.error(
|
|
60
|
+
'El archivo supera el tamaño máximo',
|
|
61
|
+
'Advertencia',
|
|
62
|
+
);
|
|
63
|
+
remove();
|
|
64
|
+
}
|
|
65
|
+
return (
|
|
66
|
+
<div className="dropzone-upload-previewContainer">
|
|
67
|
+
{meta.type === 'application/pdf' ? (
|
|
68
|
+
<>
|
|
69
|
+
<IconButtonSvg
|
|
70
|
+
className="form-control flex"
|
|
71
|
+
svg={pdfImage}
|
|
72
|
+
svgOver={pdfImage}
|
|
73
|
+
height="1.6rem"
|
|
74
|
+
width="1.6rem"
|
|
75
|
+
title={meta.name}
|
|
76
|
+
/>
|
|
77
|
+
<p>
|
|
78
|
+
{meta.name.length > 17
|
|
79
|
+
? meta.name.substring(0, 17) + '...'
|
|
80
|
+
: meta.name}
|
|
81
|
+
</p>
|
|
82
|
+
</>
|
|
83
|
+
) : (
|
|
84
|
+
<>
|
|
85
|
+
<img
|
|
86
|
+
className="dropzone-upload-previewImage"
|
|
87
|
+
src={meta.previewUrl}
|
|
88
|
+
alt={meta.name}
|
|
89
|
+
/>
|
|
90
|
+
<p>
|
|
91
|
+
{meta.name.length > 17
|
|
92
|
+
? meta.name.substring(0, 17) + '...'
|
|
93
|
+
: meta.name}
|
|
94
|
+
</p>
|
|
95
|
+
</>
|
|
96
|
+
)}
|
|
97
|
+
<p>{(meta.size / 1024).toFixed(2)} .Kb</p>
|
|
98
|
+
<button
|
|
99
|
+
onClick={remove}
|
|
100
|
+
className="dropzone-upload-delete-btn simple-icon-trash"
|
|
101
|
+
></button>
|
|
102
|
+
</div>
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const Input = ({ accept, onFiles, getFilesFromEvent }) => {
|
|
107
|
+
console.log('dropzone4 :>> ');
|
|
108
|
+
|
|
109
|
+
const text =
|
|
110
|
+
totalFilesDU > 0
|
|
111
|
+
? `${maxFiles - totalFilesDU} archivo/s más`
|
|
112
|
+
: nameFileLabel;
|
|
113
|
+
return (
|
|
114
|
+
<label className="dropzone-upload-button">
|
|
115
|
+
{text}
|
|
116
|
+
<input
|
|
117
|
+
style={{ display: 'none' }}
|
|
118
|
+
type="file"
|
|
119
|
+
accept={accept}
|
|
120
|
+
capture={capture}
|
|
121
|
+
multiple
|
|
122
|
+
onChange={(e) => {
|
|
123
|
+
getFilesFromEvent(e).then((chosenFiles) => {
|
|
124
|
+
onFiles(chosenFiles);
|
|
125
|
+
});
|
|
126
|
+
}}
|
|
127
|
+
/>
|
|
128
|
+
</label>
|
|
129
|
+
);
|
|
130
|
+
};
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
setTotalFilesDU(totalFiles);
|
|
133
|
+
}, [totalFiles]);
|
|
97
134
|
|
|
98
|
-
|
|
99
|
-
const text =
|
|
100
|
-
totalFilesDU > 0
|
|
101
|
-
? `${maxFiles - totalFilesDU} archivo/s más`
|
|
102
|
-
: nameFileLabel
|
|
103
|
-
return (
|
|
104
|
-
<label className='dropzone-upload-button'>
|
|
105
|
-
{text}
|
|
106
|
-
<input
|
|
107
|
-
style={{ display: 'none' }}
|
|
108
|
-
type='file'
|
|
109
|
-
accept={accept}
|
|
110
|
-
capture={capture}
|
|
111
|
-
multiple
|
|
112
|
-
onChange={(e) => {
|
|
113
|
-
getFilesFromEvent(e).then((chosenFiles) => {
|
|
114
|
-
onFiles(chosenFiles)
|
|
115
|
-
})
|
|
116
|
-
}}
|
|
117
|
-
/>
|
|
118
|
-
</label>
|
|
119
|
-
)
|
|
120
|
-
}
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
setTotalFilesDU(totalFiles)
|
|
123
|
-
}, [totalFiles])
|
|
135
|
+
console.log('dropzone5 :>> ');
|
|
124
136
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
137
|
+
return (
|
|
138
|
+
<Dropzone
|
|
139
|
+
disableUpload={false}
|
|
140
|
+
maxFiles={totalFilesDU < maxFiles ? maxFiles : 0}
|
|
141
|
+
maxSize={maxFileSize} // en megas
|
|
142
|
+
InputComponent={Input}
|
|
143
|
+
PreviewComponent={CustomPreview}
|
|
144
|
+
classNames={{ dropzone: 'dropzone-upload-frame' }}
|
|
145
|
+
onChangeStatus={handleChangeStatus}
|
|
146
|
+
getFilesFromEvent={getFilesFromEvent}
|
|
147
|
+
accept={accept}
|
|
148
|
+
/>
|
|
149
|
+
);
|
|
150
|
+
};
|