Skip to content

Commit fc47f37

Browse files
fix: adding fileList to onChange for multi file uploads (#613)
* fix: missing exports (#559) * fix: adding fileList to onChange for multi file uploads --------- Co-authored-by: Daniele Zurico <daniele.zurico@gmail.com>
1 parent 982cb3b commit fc47f37

File tree

7 files changed

+112
-65
lines changed

7 files changed

+112
-65
lines changed
+43-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
1-
import * as React from 'react';
21
import { MultiUpload } from '@capgeminiuk/dcx-react-library';
32

43
export const MultiUploadDemo = () => {
54
//@ts-ignore
6-
const onChangeHandler: (file: File | null) => void = (file: File) => {
7-
if (file) {
8-
const date = new Date(file.lastModified).toLocaleDateString('en-us');
9-
alert(`${file.name} was uploaded, it was last modified at ${date}`);
5+
const onChangeHandler: (files: FileList | null) => void = (
6+
files: FileList
7+
) => {
8+
if (files) {
9+
Array.from(files).forEach((file: File) => {
10+
const date = new Date(file.lastModified).toLocaleDateString('en-us');
11+
console.log(
12+
`${file.name} was uploaded, it was last modified at ${date}`
13+
);
14+
});
1015
}
1116
};
1217

1318
return (
14-
<MultiUpload
15-
name="form-input"
16-
acceptedFormats=".docx"
17-
hint={{
18-
text: 'please upload file here',
19-
}}
20-
label="CV"
21-
onChange={onChangeHandler}
22-
fileData={true}
23-
/>
19+
<>
20+
<h1>Basic</h1>
21+
<label htmlFor="basic" style={{ display: 'none' }}>
22+
Basic Upload
23+
</label>
24+
<MultiUpload
25+
id="basic"
26+
name="form-input"
27+
acceptedFormats=".docx"
28+
hint={{
29+
text: 'please upload file here',
30+
}}
31+
label="CV"
32+
onChange={onChangeHandler}
33+
fileData={true}
34+
/>
35+
<h1>Multi</h1>
36+
<label htmlFor="multi" style={{ display: 'none' }}>
37+
Multi Upload
38+
</label>
39+
<MultiUpload
40+
id="multi"
41+
name="form-input"
42+
acceptedFormats=".docx"
43+
hint={{
44+
text: 'please upload file here',
45+
}}
46+
label="Multi Upload"
47+
multiple
48+
onChange={onChangeHandler}
49+
fileData={true}
50+
/>
51+
</>
2452
);
2553
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,4 @@
135135
"@cesium133/forgjs": "1.1.10",
136136
"imask": "^6.4.3"
137137
}
138-
}
138+
}

src/multiUpload/MultiUpload.tsx

+23-23
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ type MultiUploadProps = {
4848
*/
4949
fileData?: boolean;
5050
/**
51-
* multi upload onChangeEvent
51+
* multi upload onChangeEvent fileList of uploaded files
5252
*/
53-
onChange?: (file: File | null) => void;
53+
onChange?: (file: FileList | null) => void;
5454
};
5555

5656
export const MultiUpload = ({
@@ -67,34 +67,34 @@ export const MultiUpload = ({
6767
fileData,
6868
onChange,
6969
}: MultiUploadProps) => {
70-
const [selectedFile, setSelectedFile] = useState<File | null>(null);
70+
const [selectedFiles, setSelectedFile] = useState<FileList | null>(null);
7171

7272
useEffect(() => {
73-
if (selectedFile) {
74-
onChange && onChange(selectedFile);
73+
if (selectedFiles) {
74+
onChange && onChange(selectedFiles);
7575
}
76-
}, [selectedFile]);
76+
}, [selectedFiles]);
7777

78-
const onChangeHandler: (
79-
event: React.ChangeEvent<HTMLInputElement>
80-
) => void = (event: React.ChangeEvent<HTMLInputElement>) => {
81-
const { files } = event.target;
82-
if (files) {
83-
setSelectedFile(files[0]);
84-
}
85-
};
78+
const onChangeHandler: (event: React.ChangeEvent<HTMLInputElement>) => void =
79+
(event: React.ChangeEvent<HTMLInputElement>) => {
80+
const { files } = event.target;
81+
82+
if (files) {
83+
setSelectedFile(files);
84+
}
85+
};
8686

8787
const renderFileData = () =>
88-
selectedFile && (
89-
<div>
90-
<p>{`File Name: ${selectedFile.name}`}</p>
91-
<p>{`File Type: ${selectedFile.type}`}</p>
92-
<p>{`Last Modified: ${new Date(
93-
selectedFile.lastModified
94-
).toLocaleDateString('en-us')}`}</p>
88+
selectedFiles &&
89+
Array.from(selectedFiles).map((file: File, index: number) => (
90+
<div key={index}>
91+
<p>{`File Name: ${file.name}`}</p>
92+
<p>{`File Type: ${file.type}`}</p>
93+
<p>{`Last Modified: ${new Date(file.lastModified).toLocaleDateString(
94+
'en-us'
95+
)}`}</p>
9596
</div>
96-
);
97-
97+
));
9898
return (
9999
<div className={className}>
100100
<label {...labelProperties} aria-controls={id} htmlFor={id}>

src/multiUpload/__test__/MultiUpload.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('MultiUpload', () => {
9494
);
9595

9696
expect(onChangeHandler).toHaveBeenCalled();
97-
expect(onChangeHandler).toHaveBeenCalledWith(file);
97+
expect(onChangeHandler).toHaveBeenCalledWith([file]);
9898
});
9999

100100
it('should render a multi upload with an onChange handler but not call it', async () => {

stories/MultiUpload/ClassBased.stories.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ export default {
1717
export const Basic = {
1818
name: 'Basic',
1919
render: function ({ onChange, ...args }) {
20-
const handleChange = (file) => {
21-
onChange(file);
22-
alert(
23-
`${file.name} was uploaded, it was last modified at ${new Date(file.lastModified).toLocaleDateString('en-us')}`
24-
);
20+
const handleChange = (files) => {
21+
onChange(files);
22+
Array.from(files).forEach((file) => {
23+
const date = new Date(file.lastModified).toLocaleDateString('en-us');
24+
console.log(
25+
file.name + 'was uploaded, it was last modified at ' + date
26+
);
27+
});
2528
};
29+
2630
return <MultiUpload {...args} onChange={handleChange} />;
2731
},
2832
args: {
@@ -43,12 +47,16 @@ export const Basic = {
4347
export const Hint = {
4448
name: 'Hint',
4549
render: function ({ onChange, ...args }) {
46-
const handleChange = (file) => {
47-
onChange(file);
48-
alert(
49-
`${file.name} was uploaded, it was last modified at ${new Date(file.lastModified).toLocaleDateString('en-us')}`
50-
);
50+
const handleChange = (files) => {
51+
onChange(files);
52+
Array.from(files).forEach((file) => {
53+
const date = new Date(file.lastModified).toLocaleDateString('en-us');
54+
console.log(
55+
file.name + 'was uploaded, it was last modified at ' + date
56+
);
57+
});
5158
};
59+
5260
return <MultiUpload {...args} onChange={handleChange} />;
5361
},
5462
args: {
@@ -73,12 +81,16 @@ export const Hint = {
7381
export const Error = {
7482
name: 'Error',
7583
render: function ({ onChange, ...args }) {
76-
const handleChange = (file) => {
77-
onChange(file);
78-
alert(
79-
`${file.name} was uploaded, it was last modified at ${new Date(file.lastModified).toLocaleDateString('en-us')}`
80-
);
84+
const handleChange = (files) => {
85+
onChange(files);
86+
Array.from(files).forEach((file) => {
87+
const date = new Date(file.lastModified).toLocaleDateString('en-us');
88+
console.log(
89+
file.name + 'was uploaded, it was last modified at ' + date
90+
);
91+
});
8192
};
93+
8294
return <MultiUpload {...args} onChange={handleChange} />;
8395
},
8496
args: {
@@ -91,7 +103,6 @@ export const Error = {
91103
className: 'govuk-file-upload govuk-file-upload--error',
92104
'aria-describedby': 'govuk-file-upload govuk-file-upload--error',
93105
},
94-
label: 'Upload a file',
95106
labelProperties: {
96107
className: 'govuk-label',
97108
},

stories/MultiUpload/UnStyled.stories.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ export default {
1111
};
1212
export const Unstyled = {
1313
render: function ({ onChange, ...args }) {
14-
const handleChange = (file) => {
15-
onChange(file);
16-
alert(
17-
`${file.name} was uploaded, it was last modified at ${new Date(file.lastModified).toLocaleDateString('en-us')}`
18-
);
14+
const handleChange = (files) => {
15+
onChange(files);
16+
Array.from(files).forEach((file) => {
17+
const date = new Date(file.lastModified).toLocaleDateString('en-us');
18+
console.log(
19+
file.name + 'was uploaded, it was last modified at ' + date
20+
);
21+
});
1922
};
23+
2024
return <MultiUpload {...args} onChange={handleChange} />;
2125
},
2226
args: {

stories/liveEdit/MultiUploadLive.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import { MultiUpload } from '../../src/multiUpload/MultiUpload';
44

55
const MultiUploadDemo = `
66
function MultiUploadDemo() {
7-
const onChangeHandler = (file) => {
8-
if (file) {
9-
const date = new Date(file.lastModified).toLocaleDateString("en-us");
10-
alert(file.name + 'was uploaded, it was last modified at' + date);
7+
const onChangeHandler: (files) => void = (
8+
files: FileList
9+
) => {
10+
if (files) {
11+
Array.from(files).forEach((file: File) => {
12+
const date = new Date(file.lastModified).toLocaleDateString("en-us");
13+
console.log(file.name + ' was uploaded, it was last modified at ' + date);
14+
});
1115
}
1216
};
1317
return (

0 commit comments

Comments
 (0)