Skip to content

Commit 55f4ae0

Browse files
author
Omar Flores Grimontt
committed
Added download support, and validations for print and download
1 parent b1493ba commit 55f4ae0

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@omarefg/react-file-preview",
3-
"version": "0.1.15",
3+
"version": "0.1.17",
44
"description": "Component to render and preview some kind of documents",
55
"main": "lib/index.js",
66
"private": false,

src/components/Pdf.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ErrorBoundary } from './ErrorBoundary'
66
import { PdfMenu } from './PdfMenu'
77
import { INCREASE_PERCENTAGE } from '../utils'
88

9-
export const Pdf = ({ path, ErrorComponent, onError, showPdfMenu, style, onPrint }) => {
9+
export const Pdf = ({ path, ErrorComponent, onError, showPdfMenu, style, onPrint, allowPrint, allowDownload, onDownload }) => {
1010
const {
1111
state,
1212
container,
@@ -39,6 +39,10 @@ export const Pdf = ({ path, ErrorComponent, onError, showPdfMenu, style, onPrint
3939
print={print}
4040
page={page}
4141
onPrint={onPrint}
42+
pdf={pdf}
43+
allowDownload={allowDownload}
44+
allowPrint={allowPrint}
45+
onDownload={onDownload}
4246
/>
4347
)}
4448
{!pdf && (

src/components/PdfMenu.jsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
faChevronLeft,
88
faChevronRight,
99
faPrint,
10+
faDownload,
1011
} from '@fortawesome/free-solid-svg-icons'
1112

1213
import styles from '../styles/PdfMenu.module.scss'
@@ -21,8 +22,14 @@ export const PdfMenu = props => {
2122
print,
2223
page,
2324
onPrint,
25+
pdf,
26+
allowPrint,
27+
allowDownload,
28+
onDownload,
2429
} = props
2530

31+
const printHandler = () => (onPrint ? onPrint(pdf) : print())
32+
2633
return (
2734
<div className={styles['buttons-container']}>
2835
<button
@@ -71,14 +78,26 @@ export const PdfMenu = props => {
7178
>
7279
<FontAwesomeIcon icon={faChevronRight}/>
7380
</button>
74-
<button
75-
onClick={onPrint || print}
76-
type='button'
77-
className={styles.button}
78-
title='Print'
79-
>
80-
<FontAwesomeIcon icon={faPrint}/>
81-
</button>
81+
{allowPrint && (
82+
<button
83+
onClick={printHandler}
84+
type='button'
85+
className={styles.button}
86+
title='Print'
87+
>
88+
<FontAwesomeIcon icon={faPrint}/>
89+
</button>
90+
)}
91+
{allowDownload && (
92+
<button
93+
onClick={onDownload}
94+
type='button'
95+
className={styles.button}
96+
title='Download'
97+
>
98+
<FontAwesomeIcon icon={faDownload}/>
99+
</button>
100+
)}
82101
</div>
83102
)
84103
}

src/components/Viewer.jsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export const Viewer = props => {
2626
onPrint,
2727
isLoading,
2828
supportXlsx,
29+
allowDownload,
30+
allowPrint,
31+
onDownload,
2932
} = props
3033

3134
if (isLoading) {
@@ -101,6 +104,9 @@ export const Viewer = props => {
101104
showPdfMenu={showPdfMenu}
102105
style={style}
103106
onPrint={onPrint}
107+
allowDownload={allowDownload}
108+
allowPrint={allowPrint}
109+
onDownload={onDownload}
104110
/>
105111
)
106112
}
@@ -167,6 +173,9 @@ Viewer.propTypes = {
167173
className: string,
168174
isLoading: bool,
169175
supportXlsx: bool,
176+
allowPrint: bool,
177+
allowDownload: bool,
178+
onDownload: func,
170179
}
171180

172181
Viewer.defaultProps = {
@@ -182,4 +191,7 @@ Viewer.defaultProps = {
182191
className: null,
183192
isLoading: false,
184193
supportXlsx: false,
194+
allowPrint: false,
195+
allowDownload: false,
196+
onDownload: null,
185197
}

0 commit comments

Comments
 (0)