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

Include destination array in onItemClick callbacks #814

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -235,7 +235,7 @@ Loads a document passed using `file` prop.
|inputRef|A prop that behaves like [ref](https://reactjs.org/docs/refs-and-the-dom.html), but it's passed to main `<div>` rendered by `<Document>` component.|n/a|<ul><li>Function:<br />`(ref) => { this.myDocument = ref; }`</li><li>Ref created using `React.createRef`:<br />`this.ref = React.createRef();`<br />…<br />`inputRef={this.ref}`</li><li>Ref created using `React.useRef`:<br />`const ref = React.useRef();`<br />…<br />`inputRef={ref}`</li></ul>|
|loading|What the component should display while loading.|`"Loading PDF…"`|<ul><li>String:<br />`"Please wait!"`</li><li>React element:<br />`<div>Please wait!</div>`</li><li>Function:<br />`this.renderLoader`</li></ul>|
|noData|What the component should display in case of no data.|`"No PDF file specified."`|<ul><li>String:<br />`"Please select a file."`</li><li>React element:<br />`<div>Please select a file.</div>`</li><li>Function:<br />`this.renderNoData`</li></ul>|
|onItemClick|Function called when an outline item has been clicked. Usually, you would like to use this callback to move the user wherever they requested to.|n/a|`({ pageNumber }) => alert('Clicked an item from page ' + pageNumber + '!')`|
|onItemClick|Function called when an outline item has been clicked. Usually, you would like to use this callback to move the user wherever they requested to.|n/a|`({ pageNumber, destination }) => alert('Clicked an item from page ' + pageNumber + '!')`<ul><li>`pageNumber`: The page number that the item is pointing to</li><li>`destination`: Original destination array from pdf.js, in the format `[pageRef, { name: 'XYZ' \| 'FitXXX' }, ...args]`</li></ul>|
|onLoadError|Function called in case of an error while loading a document.|n/a|`(error) => alert('Error while loading document! ' + error.message)`|
|onLoadProgress|Function called, potentially multiple times, as the loading progresses.|n/a|`({ loaded, total }) => alert('Loading a document: ' + (loaded / total) * 100 + '%');`|
|onLoadSuccess|Function called when the document is successfully loaded.|n/a|`(pdf) => alert('Loaded a file with ' + pdf.numPages + ' pages!')`|
Expand Down Expand Up @@ -291,7 +291,7 @@ Displays an outline (table of contents). Should be placed inside `<Document />`.
|----|----|----|----|
|className|Class name(s) that will be added to rendered element along with the default `react-pdf__Outline`.|n/a|<ul><li>String:<br />`"custom-class-name-1 custom-class-name-2"`</li><li>Array of strings:<br />`["custom-class-name-1", "custom-class-name-2"]`</li></ul>|
|inputRef|A prop that behaves like [ref](https://reactjs.org/docs/refs-and-the-dom.html), but it's passed to main `<div>` rendered by `<Outline>` component.|n/a|<ul><li>Function:<br />`(ref) => { this.myOutline = ref; }`</li><li>Ref created using `React.createRef`:<br />`this.ref = React.createRef();`<br />…<br />`inputRef={this.ref}`</li><li>Ref created using `React.useRef`:<br />`const ref = React.useRef();`<br />…<br />`inputRef={ref}`</li></ul>|
|onItemClick|Function called when an outline item has been clicked. Usually, you would like to use this callback to move the user wherever they requested to.|n/a|`({ pageNumber }) => alert('Clicked an item from page ' + pageNumber + '!')`|
|onItemClick|Function called when an outline item has been clicked. Usually, you would like to use this callback to move the user wherever they requested to.|n/a|`({ pageNumber }) => alert('Clicked an item from page ' + pageNumber + '!')`<ul><li>`pageNumber`: The page number that the item is pointing to</li><li>`destination`: Original destination array from pdf.js, in the format `[pageRef, { name: 'XYZ' \| 'FitXXX' }, ...args]`</li></ul>|
|onLoadError|Function called in case of an error while retrieving the outline.|n/a|`(error) => alert('Error while retrieving the outline! ' + error.message)`|
|onLoadSuccess|Function called when the outline is successfully retrieved.|n/a|`(outline) => alert('The outline has been successfully retrieved.')`|

Expand Down
4 changes: 2 additions & 2 deletions src/Document.jsx
Expand Up @@ -44,13 +44,13 @@ export default class Document extends PureComponent {
}

viewer = {
scrollPageIntoView: ({ pageNumber }) => {
scrollPageIntoView: ({ pageNumber, destination }) => {
// Handling jumping to internal links target
const { onItemClick } = this.props;

// First, check if custom handling of onItemClick was provided
if (onItemClick) {
onItemClick({ pageNumber });
onItemClick({ pageNumber, destination });
return;
}

Expand Down
8 changes: 5 additions & 3 deletions src/Document.spec.jsx
Expand Up @@ -463,12 +463,13 @@ describe('Document', () => {
await onLoadSuccessPromise;

const pageNumber = 6;
const destination = ['destination'];

// Simulate clicking on an outline item
component.instance().viewer.scrollPageIntoView({ pageNumber });
component.instance().viewer.scrollPageIntoView({ pageNumber, destination });

expect(onItemClick).toHaveBeenCalledTimes(1);
expect(onItemClick).toHaveBeenCalledWith({ pageNumber });
expect(onItemClick).toHaveBeenCalledWith({ pageNumber, destination });
});

it('attempts to find a page and scroll it into view if onItemClick is not given', async () => {
Expand All @@ -487,12 +488,13 @@ describe('Document', () => {

const scrollIntoView = jest.fn();
const pageNumber = 6;
const destination = ['destination'];

// Register fake page in Document viewer
component.instance().pages[pageNumber - 1] = { scrollIntoView };

// Simulate clicking on an outline item
component.instance().viewer.scrollPageIntoView({ pageNumber });
component.instance().viewer.scrollPageIntoView({ pageNumber, destination });

expect(scrollIntoView).toHaveBeenCalledTimes(1);
});
Expand Down
1 change: 1 addition & 0 deletions src/LinkService.js
Expand Up @@ -87,6 +87,7 @@ export default class LinkService {

this.pdfViewer.scrollPageIntoView({
pageNumber,
destination: dest,
});
});
});
Expand Down
3 changes: 2 additions & 1 deletion src/Outline.jsx
Expand Up @@ -104,13 +104,14 @@ export class OutlineInternal extends PureComponent {
if (onLoadError) onLoadError(error);
}

onItemClick = ({ pageIndex, pageNumber }) => {
onItemClick = ({ pageIndex, pageNumber, destination }) => {
const { onItemClick } = this.props;

if (onItemClick) {
onItemClick({
pageIndex,
pageNumber,
destination,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions src/OutlineItem.jsx
Expand Up @@ -84,6 +84,7 @@ export class OutlineItemInternal extends PureComponent {
onClick({
pageIndex,
pageNumber,
destination: this.destination,
});
});
}
Expand Down
5 changes: 4 additions & 1 deletion test/Test.jsx
Expand Up @@ -88,7 +88,10 @@ export default function Test() {
const onPageRenderSuccess = useCallback((page) => console.log('Rendered a page', page), []);

const onItemClick = useCallback(
({ pageNumber: nextPageNumber }) => setPageNumber(nextPageNumber),
(item) => {
console.log('Clicked an item', item);
setPageNumber(item.pageNumber);
},
[],
);

Expand Down