Skip to content

Commit

Permalink
feat: add state change event to DocumentPreview component (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
fukudasjp authored Mar 11, 2022
1 parent b46e8db commit 08b2c1a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ interface Props extends WithErrorBoundaryProps {
* React component rendered as a fallback when no preview is available
*/
fallbackComponent?: ComponentProps<typeof SimpleDocument>['fallbackComponent'];
/**
* Callback to receive changes in document preview state
*/
onChange?: (state: { currentPage?: number }) => void;
}

const SCALE_FACTOR = 1.2;
Expand All @@ -66,7 +70,8 @@ const DocumentPreview: FC<Props> = ({
highlight,
messages = defaultMessages,
didCatch,
fallbackComponent
fallbackComponent,
onChange
}) => {
const { selectedResult } = useContext(SearchContext);

Expand Down Expand Up @@ -96,6 +101,11 @@ const DocumentPreview: FC<Props> = ({

const [pdfPageCount, setPdfPageCount] = useState(0);

// notify state change
useEffect(() => {
onChange?.({ currentPage });
}, [currentPage, onChange]);

const base = `${settings.prefix}--document-preview`;

if (isFetching) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { ComponentType, FC } from 'react';
import { storiesOf } from '@storybook/react';
import { radios, boolean, number } from '@storybook/addon-knobs';
import { action } from '@storybook/addon-actions';
import { QueryResult, QueryResultPassage } from 'ibm-watson/discovery/v2';
import { SearchContext } from 'components/DiscoverySearch/DiscoverySearch';
import DocumentPreview from '../DocumentPreview';
Expand All @@ -26,7 +27,7 @@ storiesOf('DocumentPreview', module)
const [file, doc] = docSelection();
return (
<Wrapper>
<DocumentPreview document={doc} file={file} />
<DocumentPreview document={doc} file={file} onChange={action('change')} />
</Wrapper>
);
})
Expand All @@ -38,7 +39,12 @@ storiesOf('DocumentPreview', module)

return (
<Wrapper>
<DocumentPreview document={docWithPassage} highlight={highlight} file={file} />
<DocumentPreview
document={docWithPassage}
highlight={highlight}
file={file}
onChange={action('change')}
/>
</Wrapper>
);
})
Expand All @@ -49,7 +55,12 @@ storiesOf('DocumentPreview', module)

return (
<Wrapper>
<DocumentPreview file={file} document={docWithTable} highlight={highlight} />
<DocumentPreview
file={file}
document={docWithTable}
highlight={highlight}
onChange={action('change')}
/>
</Wrapper>
);
})
Expand Down Expand Up @@ -92,7 +103,11 @@ storiesOf('DocumentPreview', module)
} as any
}
>
<DocumentPreview document={docArtEffects} fileFetchTimeout={fileFetchTimeout} />
<DocumentPreview
document={docArtEffects}
fileFetchTimeout={fileFetchTimeout}
onChange={action('change')}
/>
</SearchContext.Provider>
</Wrapper>
);
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ __metadata:
languageName: node
linkType: hard

"@ibm-watson/discovery-react-components@^1.5.0-beta.26, @ibm-watson/discovery-react-components@workspace:packages/discovery-react-components":
"@ibm-watson/discovery-react-components@^1.5.0-beta.27, @ibm-watson/discovery-react-components@workspace:packages/discovery-react-components":
version: 0.0.0-use.local
resolution: "@ibm-watson/discovery-react-components@workspace:packages/discovery-react-components"
dependencies:
Expand Down Expand Up @@ -10664,7 +10664,7 @@ __metadata:
dependencies:
"@carbon/icons": ^10.5.0
"@cypress/webpack-preprocessor": ^5.11.0
"@ibm-watson/discovery-react-components": ^1.5.0-beta.26
"@ibm-watson/discovery-react-components": ^1.5.0-beta.27
"@ibm-watson/discovery-styles": ^1.5.0-beta.24
"@testing-library/cypress": ^7.0.7
"@types/proper-url-join": ^2
Expand Down

0 comments on commit 08b2c1a

Please sign in to comment.