Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
fix: change focus when zoom buttons becomes disabled to be able to st…
Browse files Browse the repository at this point in the history
…ill use arrow keys (fix issue #132) (#135)
  • Loading branch information
bastoune authored and wuweiweiwu committed Aug 30, 2018
1 parent 357a801 commit 30ba946
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/__tests__/react-image-lightbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mount } from 'enzyme';
import React from 'react';
import Modal from 'react-modal';
import Lightbox from '../index';
import { MAX_ZOOM_LEVEL, MIN_ZOOM_LEVEL, ZOOM_BUTTON_INCREMENT_SIZE } from '../constant'

// Mock the loadStyles static function to avoid
// issues with a lack of styles._insertCss
Expand Down Expand Up @@ -123,6 +124,11 @@ describe('Events', () => {
<Lightbox {...extendedCommonProps} {...mockFns} animationDisabled />
);

// Spy zoomBtn focus
const {zoomOutBtn, zoomInBtn} = wrapper.instance();
jest.spyOn(zoomOutBtn, 'focus');
jest.spyOn(zoomInBtn, 'focus');

it('Calls onAfterOpen when mounted', () => {
expect(mockFns.onAfterOpen).toHaveBeenCalledTimes(1);
expect(mockFns.onAfterOpen).toHaveBeenCalledWith();
Expand Down Expand Up @@ -174,6 +180,18 @@ describe('Events', () => {
expect(mockFns.onImageLoadError).toHaveBeenCalledTimes(0);
wrapper.setProps({ mainSrc: LOAD_FAILURE_SRC });
});

it('Calls the the ZoomIn Focus when ZoomOut is disabled', () => {
wrapper.setState({ zoomLevel: MIN_ZOOM_LEVEL + ZOOM_BUTTON_INCREMENT_SIZE });
wrapper.instance().handleZoomOutButtonClick();
expect(zoomInBtn.focus).toHaveBeenCalledTimes(1);
});

it('Calls the the ZoomOut Focus when ZoomIn is disabled', () => {
wrapper.setState({ zoomLevel: MAX_ZOOM_LEVEL - ZOOM_BUTTON_INCREMENT_SIZE });
wrapper.instance().handleZoomInButtonClick();
expect(zoomOutBtn.focus).toHaveBeenCalledTimes(1);
});
});

describe('Key bindings', () => {
Expand Down
18 changes: 16 additions & 2 deletions src/react-image-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,11 +1061,19 @@ class ReactImageLightbox extends Component {
}

handleZoomInButtonClick() {
this.changeZoom(this.state.zoomLevel + ZOOM_BUTTON_INCREMENT_SIZE);
const nextZoomLevel = this.state.zoomLevel + ZOOM_BUTTON_INCREMENT_SIZE;
this.changeZoom(nextZoomLevel);
if (nextZoomLevel === MAX_ZOOM_LEVEL) {
this.zoomOutBtn.focus();
}
}

handleZoomOutButtonClick() {
this.changeZoom(this.state.zoomLevel - ZOOM_BUTTON_INCREMENT_SIZE);
const nextZoomLevel = this.state.zoomLevel - ZOOM_BUTTON_INCREMENT_SIZE;
this.changeZoom(nextZoomLevel);
if (nextZoomLevel === MIN_ZOOM_LEVEL) {
this.zoomInBtn.focus();
}
}

handleCaptionMousewheel(event) {
Expand Down Expand Up @@ -1533,6 +1541,9 @@ class ReactImageLightbox extends Component {
? ['ril__builtinButtonDisabled']
: []),
].join(' ')}
ref={el => {
this.zoomInBtn = el
}}
disabled={
this.isAnimating() || zoomLevel === MAX_ZOOM_LEVEL
}
Expand Down Expand Up @@ -1560,6 +1571,9 @@ class ReactImageLightbox extends Component {
? ['ril__builtinButtonDisabled']
: []),
].join(' ')}
ref={el => {
this.zoomOutBtn = el
}}
disabled={
this.isAnimating() || zoomLevel === MIN_ZOOM_LEVEL
}
Expand Down

0 comments on commit 30ba946

Please sign in to comment.