Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions example/src/components/MultiUploadDemo.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
import * as React from 'react';
import { MultiUpload } from '@capgeminiuk/dcx-react-library';

export const MultiUploadDemo = () => {
//@ts-ignore
const onChangeHandler: (file: File | null) => void = (file: File) => {
if (file) {
const date = new Date(file.lastModified).toLocaleDateString('en-us');
alert(`${file.name} was uploaded, it was last modified at ${date}`);
const onChangeHandler: (files: FileList | null) => void = (
files: FileList
) => {
if (files) {
Array.from(files).forEach((file: File) => {
const date = new Date(file.lastModified).toLocaleDateString('en-us');
console.log(
`${file.name} was uploaded, it was last modified at ${date}`
);
});
}
};

return (
<MultiUpload
name="form-input"
acceptedFormats=".docx"
hint={{
text: 'please upload file here',
}}
label="CV"
onChange={onChangeHandler}
fileData={true}
/>
<>
<h1>Basic</h1>
<label htmlFor="basic" style={{ display: 'none' }}>
Basic Upload
</label>
<MultiUpload
id="basic"
name="form-input"
acceptedFormats=".docx"
hint={{
text: 'please upload file here',
}}
label="CV"
onChange={onChangeHandler}
fileData={true}
/>
<h1>Multi</h1>
<label htmlFor="multi" style={{ display: 'none' }}>
Multi Upload
</label>
<MultiUpload
id="multi"
name="form-input"
acceptedFormats=".docx"
hint={{
text: 'please upload file here',
}}
label="Multi Upload"
multiple
onChange={onChangeHandler}
fileData={true}
/>
</>
);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@
"@cesium133/forgjs": "1.1.10",
"imask": "^6.4.3"
}
}
}
46 changes: 23 additions & 23 deletions src/multiUpload/MultiUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type MultiUploadProps = {
*/
fileData?: boolean;
/**
* multi upload onChangeEvent
* multi upload onChangeEvent fileList of uploaded files
*/
onChange?: (file: File | null) => void;
onChange?: (file: FileList | null) => void;
};

export const MultiUpload = ({
Expand All @@ -67,34 +67,34 @@ export const MultiUpload = ({
fileData,
onChange,
}: MultiUploadProps) => {
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const [selectedFiles, setSelectedFile] = useState<FileList | null>(null);

useEffect(() => {
if (selectedFile) {
onChange && onChange(selectedFile);
if (selectedFiles) {
onChange && onChange(selectedFiles);
}
}, [selectedFile]);
}, [selectedFiles]);

const onChangeHandler: (
event: React.ChangeEvent<HTMLInputElement>
) => void = (event: React.ChangeEvent<HTMLInputElement>) => {
const { files } = event.target;
if (files) {
setSelectedFile(files[0]);
}
};
const onChangeHandler: (event: React.ChangeEvent<HTMLInputElement>) => void =
(event: React.ChangeEvent<HTMLInputElement>) => {
const { files } = event.target;

if (files) {
setSelectedFile(files);
}
};

const renderFileData = () =>
selectedFile && (
<div>
<p>{`File Name: ${selectedFile.name}`}</p>
<p>{`File Type: ${selectedFile.type}`}</p>
<p>{`Last Modified: ${new Date(
selectedFile.lastModified
).toLocaleDateString('en-us')}`}</p>
selectedFiles &&
Array.from(selectedFiles).map((file: File, index: number) => (
<div key={index}>
<p>{`File Name: ${file.name}`}</p>
<p>{`File Type: ${file.type}`}</p>
<p>{`Last Modified: ${new Date(file.lastModified).toLocaleDateString(
'en-us'
)}`}</p>
</div>
);

));
return (
<div className={className}>
<label {...labelProperties} aria-controls={id} htmlFor={id}>
Expand Down
2 changes: 1 addition & 1 deletion src/multiUpload/__test__/MultiUpload.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('MultiUpload', () => {
);

expect(onChangeHandler).toHaveBeenCalled();
expect(onChangeHandler).toHaveBeenCalledWith(file);
expect(onChangeHandler).toHaveBeenCalledWith([file]);
});

it('should render a multi upload with an onChange handler but not call it', async () => {
Expand Down
43 changes: 27 additions & 16 deletions stories/MultiUpload/ClassBased.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ export default {
export const Basic = {
name: 'Basic',
render: function ({ onChange, ...args }) {
const handleChange = (file) => {
onChange(file);
alert(
`${file.name} was uploaded, it was last modified at ${new Date(file.lastModified).toLocaleDateString('en-us')}`
);
const handleChange = (files) => {
onChange(files);
Array.from(files).forEach((file) => {
const date = new Date(file.lastModified).toLocaleDateString('en-us');
console.log(
file.name + 'was uploaded, it was last modified at ' + date
);
});
};

return <MultiUpload {...args} onChange={handleChange} />;
},
args: {
Expand All @@ -43,12 +47,16 @@ export const Basic = {
export const Hint = {
name: 'Hint',
render: function ({ onChange, ...args }) {
const handleChange = (file) => {
onChange(file);
alert(
`${file.name} was uploaded, it was last modified at ${new Date(file.lastModified).toLocaleDateString('en-us')}`
);
const handleChange = (files) => {
onChange(files);
Array.from(files).forEach((file) => {
const date = new Date(file.lastModified).toLocaleDateString('en-us');
console.log(
file.name + 'was uploaded, it was last modified at ' + date
);
});
};

return <MultiUpload {...args} onChange={handleChange} />;
},
args: {
Expand All @@ -73,12 +81,16 @@ export const Hint = {
export const Error = {
name: 'Error',
render: function ({ onChange, ...args }) {
const handleChange = (file) => {
onChange(file);
alert(
`${file.name} was uploaded, it was last modified at ${new Date(file.lastModified).toLocaleDateString('en-us')}`
);
const handleChange = (files) => {
onChange(files);
Array.from(files).forEach((file) => {
const date = new Date(file.lastModified).toLocaleDateString('en-us');
console.log(
file.name + 'was uploaded, it was last modified at ' + date
);
});
};

return <MultiUpload {...args} onChange={handleChange} />;
},
args: {
Expand All @@ -91,7 +103,6 @@ export const Error = {
className: 'govuk-file-upload govuk-file-upload--error',
'aria-describedby': 'govuk-file-upload govuk-file-upload--error',
},
label: 'Upload a file',
labelProperties: {
className: 'govuk-label',
},
Expand Down
14 changes: 9 additions & 5 deletions stories/MultiUpload/UnStyled.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ export default {
};
export const Unstyled = {
render: function ({ onChange, ...args }) {
const handleChange = (file) => {
onChange(file);
alert(
`${file.name} was uploaded, it was last modified at ${new Date(file.lastModified).toLocaleDateString('en-us')}`
);
const handleChange = (files) => {
onChange(files);
Array.from(files).forEach((file) => {
const date = new Date(file.lastModified).toLocaleDateString('en-us');
console.log(
file.name + 'was uploaded, it was last modified at ' + date
);
});
};

return <MultiUpload {...args} onChange={handleChange} />;
},
args: {
Expand Down
12 changes: 8 additions & 4 deletions stories/liveEdit/MultiUploadLive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { MultiUpload } from '../../src/multiUpload/MultiUpload';

const MultiUploadDemo = `
function MultiUploadDemo() {
const onChangeHandler = (file) => {
if (file) {
const date = new Date(file.lastModified).toLocaleDateString("en-us");
alert(file.name + 'was uploaded, it was last modified at' + date);
const onChangeHandler: (files) => void = (
files: FileList
) => {
if (files) {
Array.from(files).forEach((file: File) => {
const date = new Date(file.lastModified).toLocaleDateString("en-us");
console.log(file.name + ' was uploaded, it was last modified at ' + date);
});
}
};
return (
Expand Down