Skip to content

Commit b5f82b5

Browse files
amoycodelxm23
andauthored
feat(module:image): nz-image add press left or right to switch image (#7321)
* feat(module:image): nz-image add press `left` or `right` to switch image The nz-image add nzEnableLeftRightArrow to support press `left` or `right` to switch image * feat(module:image): nz-image add press `left` or `right` to switch image The nz-image add support press `left` or `right` to switch image Co-authored-by: lxm23 <lxm2322@163.com>
1 parent 7f3c4e1 commit b5f82b5

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

components/image/doc/index.en-US.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Other attributes [<img\>](https://developer.mozilla.org/en-US/docs/Web/HTML/Elem
5454

5555
| Property | Description | Type | Default |
5656
| --- | --- | --- | --- |
57-
| nzKeyboard | Whether support press `esc` to close | `boolean` | `true` |
57+
| nzKeyboard | Whether support press `esc` to close, press `left` or `right` to switch image | `boolean` | `true` |
5858
| nzMaskClosable | Whether to close the image preview when the mask (area outside the image) is clicked | `boolean` | `true` |
5959
| nzCloseOnNavigation | Whether to close the image preview when the user goes backwards/forwards in history. Note that this usually doesn't include clicking on links (unless the user is using the HashLocationStrategy). | `boolean` | `true` |
6060
| nzZIndex | The z-index of the image preview | `number` | 1000 |

components/image/doc/index.zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import { NzImageModule } from 'ng-zorro-antd/image';
5454

5555
| 参数 | 说明 | 类型 | 默认值 |
5656
| --- | --- | --- | --- |
57-
| nzKeyboard | 是否支持键盘 esc 关闭 | `boolean` | `true` |
57+
| nzKeyboard | 是否支持键盘 esc 关闭、左右键切换图片 | `boolean` | `true` |
5858
| nzMaskClosable | 点击蒙层是否允许关闭 | `boolean` | `true` |
5959
| nzCloseOnNavigation | 当用户在历史中前进/后退时是否关闭预览。注意,这通常不包括点击链接(除非用户使用HashLocationStrategy)。 | `boolean` | `true` |
6060
| nzZIndex | 设置预览层的 z-index | `number` | 1000 |

components/image/image-preview-ref.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6-
import { ESCAPE, hasModifierKey } from '@angular/cdk/keycodes';
6+
import { ESCAPE, hasModifierKey, LEFT_ARROW, RIGHT_ARROW } from '@angular/cdk/keycodes';
77
import { OverlayRef } from '@angular/cdk/overlay';
88
import { Subject } from 'rxjs';
99
import { filter, take, takeUntil } from 'rxjs/operators';
@@ -21,10 +21,25 @@ export class NzImagePreviewRef {
2121
) {
2222
overlayRef
2323
.keydownEvents()
24-
.pipe(filter(event => (this.config.nzKeyboard as boolean) && event.keyCode === ESCAPE && !hasModifierKey(event)))
24+
.pipe(
25+
filter(
26+
event =>
27+
(this.config.nzKeyboard as boolean) &&
28+
(event.keyCode === ESCAPE || event.keyCode === LEFT_ARROW || event.keyCode === RIGHT_ARROW) &&
29+
!hasModifierKey(event)
30+
)
31+
)
2532
.subscribe(event => {
2633
event.preventDefault();
27-
this.close();
34+
if (event.keyCode === ESCAPE) {
35+
this.close();
36+
}
37+
if (event.keyCode === LEFT_ARROW) {
38+
this.prev();
39+
}
40+
if (event.keyCode === RIGHT_ARROW) {
41+
this.next();
42+
}
2843
});
2944

3045
overlayRef.detachments().subscribe(() => {

components/image/image.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6+
import { LEFT_ARROW, RIGHT_ARROW } from '@angular/cdk/keycodes';
67
import { Overlay, OverlayContainer } from '@angular/cdk/overlay';
78
import { Component, DebugElement, NgModule, ViewChild } from '@angular/core';
89
import {
@@ -27,7 +28,7 @@ import {
2728
ZoomOutOutline
2829
} from '@ant-design/icons-angular/icons';
2930

30-
import { dispatchFakeEvent } from 'ng-zorro-antd/core/testing';
31+
import { dispatchFakeEvent, dispatchKeyboardEvent } from 'ng-zorro-antd/core/testing';
3132
import { NzIconModule, NZ_ICONS } from 'ng-zorro-antd/icon';
3233
import {
3334
getFitContentPosition,
@@ -400,6 +401,19 @@ describe('Preview', () => {
400401
fixture.detectChanges();
401402
previewImageElement = getPreviewImageElement();
402403
expect(previewImageElement.src).toContain(images[1].src);
404+
405+
dispatchKeyboardEvent(overlayContainerElement, 'keydown', RIGHT_ARROW);
406+
tickChanges();
407+
previewImageElement = getPreviewImageElement();
408+
expect(previewImageElement.src).toContain(images[1].src);
409+
dispatchKeyboardEvent(overlayContainerElement, 'keydown', LEFT_ARROW);
410+
tickChanges();
411+
previewImageElement = getPreviewImageElement();
412+
expect(previewImageElement.src).toContain(images[0].src);
413+
dispatchKeyboardEvent(overlayContainerElement, 'keydown', RIGHT_ARROW);
414+
tickChanges();
415+
previewImageElement = getPreviewImageElement();
416+
expect(previewImageElement.src).toContain(images[1].src);
403417
}));
404418
});
405419

0 commit comments

Comments
 (0)