Skip to content

Commit

Permalink
feat: navigate images on keyboard clicks (#593)
Browse files Browse the repository at this point in the history
Co-authored-by: deepakglobant <[email protected]>
  • Loading branch information
deepakglobant and deepakglobant authored Oct 11, 2023
1 parent 722ee46 commit f70751a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function useFetchInspection({
processInspection,
resetState,
isInspectionReady,
vinNumber,
pictures,
damages,
setDamages,
Expand Down Expand Up @@ -59,6 +60,7 @@ export default function useFetchInspection({
isError,
retry,
isInspectionReady,
vinNumber,
pictures,
damages,
setDamages,
Expand Down
36 changes: 35 additions & 1 deletion packages/inspection-report/src/components/Gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,40 @@ function Gallery({ pictures }) {
}
}, [gestureState]);

useEffect(() => {
if (focusedPhoto) {
const handleKeyboardChange = (event) => {
if (event.defaultPrevented) {
return; // Do nothing if the event was already processed
}

const currentPictureIndex = pictures.findIndex(pic => pic.id === focusedPhoto.id);
switch (event.key) {
case "ArrowLeft":
if (currentPictureIndex - 1 >= 0) {
setFocusedPhoto(pictures[currentPictureIndex - 1]);
}
break;
case "ArrowRight":
if (currentPictureIndex + 1 < pictures.length) {
setFocusedPhoto(pictures[currentPictureIndex + 1]);
}
break;
default:
return; // Quit when this doesn't handle the key event.
}

// Cancel the default action to avoid it being handled twice
event.preventDefault();
};

window.addEventListener('keydown', handleKeyboardChange);
return () => {
window.removeEventListener('keydown', handleKeyboardChange);
};
}
}, [pictures, focusedPhoto]);

return (
<View style={[
styles.container,
Expand Down Expand Up @@ -206,7 +240,7 @@ function Gallery({ pictures }) {
source={{ uri: focusedPhoto?.url }}
style={{
flex: 1,
cursor: isDesktopMode ? 'auto' : isZoomed ? 'zoom-out' : 'zoom-in',
cursor: !isDesktopMode ? 'auto' : isZoomed ? 'zoom-out' : 'zoom-in',
transform: [{ scale }, { translateX: transform.x }, { translateY: transform.y }],
}}
resizeMode={isDesktopMode ? 'cover' : 'contain'}
Expand Down

0 comments on commit f70751a

Please sign in to comment.