Skip to content

Commit

Permalink
Added download support, and validations for print and download
Browse files Browse the repository at this point in the history
  • Loading branch information
Omar Flores Grimontt committed Sep 13, 2019
1 parent b1493ba commit 55f4ae0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@omarefg/react-file-preview",
"version": "0.1.15",
"version": "0.1.17",
"description": "Component to render and preview some kind of documents",
"main": "lib/index.js",
"private": false,
Expand Down
6 changes: 5 additions & 1 deletion src/components/Pdf.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ErrorBoundary } from './ErrorBoundary'
import { PdfMenu } from './PdfMenu'
import { INCREASE_PERCENTAGE } from '../utils'

export const Pdf = ({ path, ErrorComponent, onError, showPdfMenu, style, onPrint }) => {
export const Pdf = ({ path, ErrorComponent, onError, showPdfMenu, style, onPrint, allowPrint, allowDownload, onDownload }) => {
const {
state,
container,
Expand Down Expand Up @@ -39,6 +39,10 @@ export const Pdf = ({ path, ErrorComponent, onError, showPdfMenu, style, onPrint
print={print}
page={page}
onPrint={onPrint}
pdf={pdf}
allowDownload={allowDownload}
allowPrint={allowPrint}
onDownload={onDownload}
/>
)}
{!pdf && (
Expand Down
35 changes: 27 additions & 8 deletions src/components/PdfMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
faChevronLeft,
faChevronRight,
faPrint,
faDownload,
} from '@fortawesome/free-solid-svg-icons'

import styles from '../styles/PdfMenu.module.scss'
Expand All @@ -21,8 +22,14 @@ export const PdfMenu = props => {
print,
page,
onPrint,
pdf,
allowPrint,
allowDownload,
onDownload,
} = props

const printHandler = () => (onPrint ? onPrint(pdf) : print())

return (
<div className={styles['buttons-container']}>
<button
Expand Down Expand Up @@ -71,14 +78,26 @@ export const PdfMenu = props => {
>
<FontAwesomeIcon icon={faChevronRight}/>
</button>
<button
onClick={onPrint || print}
type='button'
className={styles.button}
title='Print'
>
<FontAwesomeIcon icon={faPrint}/>
</button>
{allowPrint && (
<button
onClick={printHandler}
type='button'
className={styles.button}
title='Print'
>
<FontAwesomeIcon icon={faPrint}/>
</button>
)}
{allowDownload && (
<button
onClick={onDownload}
type='button'
className={styles.button}
title='Download'
>
<FontAwesomeIcon icon={faDownload}/>
</button>
)}
</div>
)
}
12 changes: 12 additions & 0 deletions src/components/Viewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const Viewer = props => {
onPrint,
isLoading,
supportXlsx,
allowDownload,
allowPrint,
onDownload,
} = props

if (isLoading) {
Expand Down Expand Up @@ -101,6 +104,9 @@ export const Viewer = props => {
showPdfMenu={showPdfMenu}
style={style}
onPrint={onPrint}
allowDownload={allowDownload}
allowPrint={allowPrint}
onDownload={onDownload}
/>
)
}
Expand Down Expand Up @@ -167,6 +173,9 @@ Viewer.propTypes = {
className: string,
isLoading: bool,
supportXlsx: bool,
allowPrint: bool,
allowDownload: bool,
onDownload: func,
}

Viewer.defaultProps = {
Expand All @@ -182,4 +191,7 @@ Viewer.defaultProps = {
className: null,
isLoading: false,
supportXlsx: false,
allowPrint: false,
allowDownload: false,
onDownload: null,
}

0 comments on commit 55f4ae0

Please sign in to comment.