Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new method onBeforeGetContent #160

Merged
merged 1 commit into from
Aug 20, 2019
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ The component accepts the following props:
| **`trigger`** | function | A function that returns a React Component or HTML element |
| **`content`** | function | A function that returns a component reference value. The content of this reference value is then used for print |
| **`copyStyles`** | boolean | Copies all &lt;style> and &lt;link type="stylesheet" /> from <head> inside the parent window into the print window. (default: true) |
| **`onBeforePrint`** | function | Optional callback function that triggers before print. Either returns void or a Promise. If the function returns a Promise the content will be printed when the Promise is resolved. Users are responsible for catching the Promise rejecting. |
| **`onBeforePrint`** | function | ptional callback function that triggers before print. Either returns void or a Promise. If the function returns a Promise the content will be printed when the Promise is resolved. Users are responsible for catching the Promise rejecting. Note: this function is run immediately prior to printing, but after the page's content has been gathered. To modify content before printing, use onBeforeGetContent instead. |
| **`onBeforeGetContent`** | function | Optional callback function that triggers before the library gathers the page's content. Either returns void or a Promise. Users are responsible for catching the Promise rejecting. This can be used to change the content on the page before printing.
| **`onAfterPrint`** | function | Optional callback function that triggers after print |
| **`removeAfterPrint`** | boolean | Remove the print iframe after action. Defaults to `false`. |
| **`pageStyle`** | string | Override default print window styling |
Expand Down
7 changes: 5 additions & 2 deletions example/ComponentToPrint/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as React from 'react';

export default class ComponentToPrint extends React.Component {
type Props = {
text: string
}
export default class ComponentToPrint<Props> extends React.Component {
canvasEl: HTMLCanvasElement = null;

componentDidMount() {
Expand All @@ -26,7 +29,7 @@ export default class ComponentToPrint extends React.Component {
</thead>
<tbody>
<tr>
<td>1</td>
<td>{this.props.text}</td>
<td>2</td>
</tr>
<tr>
Expand Down
23 changes: 21 additions & 2 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@ import * as ReactDOM from 'react-dom';
import ReactToPrint from '../src/index';
import ComponentToPrint from './ComponentToPrint';

class Example extends React.Component {
interface State {
text: string;
}

interface Props {

}

class Example extends React.Component<Props, State> {
componentRef: ComponentToPrint;

constructor(props: Props) {
super(props);
this.state = {
text: "000000000";
};
}

handleAfterPrint = () => console.log('after print!');
handleBeforePrint = () => console.log('before print!');
renderContent = () => this.componentRef;
renderTrigger = () => <button type="button">Print this out!</button>;
onBeforeGetContent = () => new Promise((resolve, reject) => {
this.setState({text: "text changed"}, () => resolve);
}));

setRef = ref => this.componentRef = ref;

Expand All @@ -20,11 +38,12 @@ class Example extends React.Component {
<ReactToPrint
trigger={this.renderTrigger}
content={this.renderContent}
onBeforeGetContent={this.onBeforeGetContent}
onBeforePrint={this.handleBeforePrint}
onAfterPrint={this.handleAfterPrint}
removeAfterPrint
/>
<ComponentToPrint ref={this.setRef}/>
<ComponentToPrint ref={this.setRef} text={this.state.text}/>
</div>
);
}
Expand Down
16 changes: 15 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface IReactToPrintProps {
content: () => React.ReactInstance;
/** Copy styles over into print window. default: true */
copyStyles?: boolean;
/** Callback function to trigger before page content is retrieved for printing */
onBeforeGetContent?: () => void | Promise<any>;
/** Callback function to trigger before print */
onBeforePrint?: () => void | Promise<any>;
/** Callback function to trigger after print */
Expand Down Expand Up @@ -208,11 +210,23 @@ export default class ReactToPrint extends React.Component<IReactToPrintProps> {

public render() {
const {
onBeforeGetContent,
trigger,
} = this.props;

return React.cloneElement(trigger(), {
onClick: this.handlePrint,
onClick: () => {
if (onBeforeGetContent) {
const onBeforeGetContentOutput = onBeforeGetContent();
if (onBeforeGetContentOutput && typeof onBeforeGetContentOutput.then === "function") {
onBeforeGetContentOutput.then(this.handlePrint);
} else {
this.handlePrint();
}
} else {
this.handlePrint();
}
},
ref: this.setRef,
});
}
Expand Down