-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
September + October improvements (#418)
* Add `logMessages` function, fix small bug * Style examples, add example using functional component to print * Added style tip about page orientation * Added functional ComponentToPrint example, added a styling pitfall * Add pitfall example regarding printing component arrays (#323) * Upgrade all `devDependencies` * Change class-only error to link how to use functional components * Fix print to PDF filename not working in all major browsers #391 * Add to README about known issues when printing from mobile WebViews
- Loading branch information
1 parent
02c8ce1
commit c224cf7
Showing
11 changed files
with
1,382 additions
and
2,966 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
examples/FunctionalComponentWithFunctionalComponentToPrint/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as React from "react"; | ||
|
||
import { FunctionalComponentToPrint } from "../ComponentToPrint"; | ||
import ReactToPrint from "../../src/index"; | ||
|
||
export const FunctionalComponentWithFunctionalComponentToPrint = () => { | ||
const componentRef = React.useRef(null); | ||
|
||
const onBeforeGetContentResolve = React.useRef<(() => void) | null>(null); | ||
|
||
const [loading, setLoading] = React.useState(false); | ||
const [text, setText] = React.useState("old boring text"); | ||
|
||
const handleAfterPrint = React.useCallback(() => { | ||
console.log("`onAfterPrint` called"); // tslint:disable-line no-console | ||
}, []); | ||
|
||
const handleBeforePrint = React.useCallback(() => { | ||
console.log("`onBeforePrint` called"); // tslint:disable-line no-console | ||
}, []); | ||
|
||
const handleOnBeforeGetContent = React.useCallback(() => { | ||
console.log("`onBeforeGetContent` called"); // tslint:disable-line no-console | ||
setLoading(true); | ||
setText("Loading new text..."); | ||
|
||
return new Promise<void>((resolve) => { | ||
onBeforeGetContentResolve.current = resolve; | ||
|
||
setTimeout(() => { | ||
setLoading(false); | ||
setText("New, Updated Text!"); | ||
resolve(); | ||
}, 2000); | ||
}); | ||
}, [setLoading, setText]); | ||
|
||
React.useEffect(() => { | ||
if (text === "New, Updated Text!" && typeof onBeforeGetContentResolve.current === "function") { | ||
onBeforeGetContentResolve.current(); | ||
} | ||
}, [onBeforeGetContentResolve.current, text]); | ||
|
||
const reactToPrintContent = React.useCallback(() => { | ||
return componentRef.current; | ||
}, [componentRef.current]); | ||
|
||
const reactToPrintTrigger = React.useCallback(() => { | ||
// NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop | ||
// to the root node of the returned component as it will be overwritten. | ||
|
||
// Bad: the `onClick` here will be overwritten by `react-to-print` | ||
// return <button onClick={() => alert('This will not work')}>Print this out!</button>; | ||
|
||
// Good | ||
return <button>Print a Functional Component (using `forwardRef`) using a Functional Component</button>; // eslint-disable-line max-len | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<ReactToPrint | ||
content={reactToPrintContent} | ||
documentTitle="AwesomeFileName" | ||
onAfterPrint={handleAfterPrint} | ||
onBeforeGetContent={handleOnBeforeGetContent} | ||
onBeforePrint={handleBeforePrint} | ||
removeAfterPrint | ||
trigger={reactToPrintTrigger} | ||
/> | ||
{loading && <p className="indicator">onBeforeGetContent: Loading...</p>} | ||
<FunctionalComponentToPrint ref={componentRef} text={text} /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,5 @@ | |
background: rgba(76, 175, 80, 0.3); | ||
} | ||
|
||
|
||
|
Oops, something went wrong.