Skip to content

Commit 6030a08

Browse files
authored
Merge pull request #109 from performant-software/feature/archnet600_download_button
Archnet #600 - Download Button
2 parents 39b9511 + dd9221d commit 6030a08

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/semantic-ui/DownloadButton.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
// @flow
22

3-
import React, { useState } from 'react';
3+
import React from 'react';
4+
import uuid from 'react-uuid';
45
import { Button } from 'semantic-ui-react';
56

67
type Props = {
8+
filename?: string,
79
url: string
810
};
911

10-
const DownloadButton = ({ url, ...button }: Props) => {
11-
const [loading, setLoading] = useState(false);
12-
13-
return (
12+
const DownloadButton = ({ filename, url, ...button }: Props) => (
13+
<a
14+
download={filename || uuid()}
15+
href={url}
16+
>
1417
<Button
1518
{...button}
16-
loading={loading}
17-
onClick={() => {
18-
setLoading(true);
19-
return fetch(url).then((response) => {
20-
response.blob().then((blob) => {
21-
const downloadUrl = window.URL.createObjectURL(blob);
22-
const link = document.createElement('a');
23-
link.href = downloadUrl;
24-
link.click();
25-
});
26-
}).finally(() => setLoading(false));
27-
}}
2819
/>
29-
);
30-
};
20+
</a>
21+
);
3122

3223
export default DownloadButton;

stories/components/semantic-ui/DownloadButton.stories.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ export const Default = () => (
1616
url={image}
1717
/>
1818
);
19+
20+
export const LargeFile = () => (
21+
<DownloadButton
22+
content='Download'
23+
icon='cloud download'
24+
primary
25+
url='https://speed.hetzner.de/100MB.bin'
26+
/>
27+
);
28+
29+
export const WithFilename = () => (
30+
<DownloadButton
31+
content='Download'
32+
filename='Test12345'
33+
icon='cloud download'
34+
primary
35+
url={image}
36+
/>
37+
);

0 commit comments

Comments
 (0)