Skip to content

feat(module:transfer): support multiple row selection with Shift key #9092

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

Merged
merged 1 commit into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions components/transfer/transfer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Component,
ElementRef,
EventEmitter,
HostListener,
Input,
OnChanges,
OnDestroy,
Expand Down Expand Up @@ -220,9 +221,31 @@

// left
leftDataSource: TransferItem[] = [];
lastLeftCheckedIndex?: number;

// right
rightDataSource: TransferItem[] = [];
lastRightCheckedIndex?: number;

isShiftPressed = false;

@HostListener('window:keydown.shift')
onTriggerShiftDown(): void {
this.isShiftPressed = true;
}

@HostListener('window:keyup.shift')
onTriggerShiftUp(): void {
this.isShiftPressed = false;
}

@HostListener('mousedown', ['$event'])
onTriggerMouseDown(event: MouseEvent): void {
const isInsideTransfer = (event.target as HTMLElement).closest('.ant-transfer-list');

Check warning on line 244 in components/transfer/transfer.component.ts

View check run for this annotation

Codecov / codecov/patch

components/transfer/transfer.component.ts#L243-L244

Added lines #L243 - L244 were not covered by tests
if (event.shiftKey && isInsideTransfer) {
event.preventDefault();

Check warning on line 246 in components/transfer/transfer.component.ts

View check run for this annotation

Codecov / codecov/patch

components/transfer/transfer.component.ts#L246

Added line #L246 was not covered by tests
}
}

private splitDataSource(): void {
this.leftDataSource = [];
Expand All @@ -249,6 +272,23 @@
handleRightSelect = (item: TransferItem): void => this.handleSelect('right', !!item.checked, item);

handleSelect(direction: TransferDirection, checked: boolean, item?: TransferItem): void {
if (item) {
const datasource = direction === 'left' ? this.leftDataSource : this.rightDataSource;
const currentIndex = datasource.findIndex(i => i.key === item.key);
const lastCheckedIndex = this[direction === 'left' ? 'lastLeftCheckedIndex' : 'lastRightCheckedIndex'] ?? -1;
if (this.isShiftPressed && lastCheckedIndex > -1) {
const start = Math.min(lastCheckedIndex, currentIndex);
const end = Math.max(lastCheckedIndex, currentIndex);
for (let i = start; i <= end; i++) {
const item = datasource[i];
if (!item.disabled) {
item.checked = checked;
}
}
this.markForCheckAllList();
}
this[direction === 'left' ? 'lastLeftCheckedIndex' : 'lastRightCheckedIndex'] = currentIndex;
}
const list = this.getCheckedData(direction);
const count = list.filter(i => !i.disabled).length;
this.updateOperationStatus(direction, count);
Expand Down Expand Up @@ -301,11 +341,7 @@
}
targetDatasource.splice(0, 0, ...list);
this.updateOperationStatus(oppositeDirection);
this.nzChange.emit({
from: oppositeDirection,
to: direction,
list
});
this.nzChange.emit({ from: oppositeDirection, to: direction, list });
this.markForCheckAllList();
}

Expand Down
49 changes: 23 additions & 26 deletions components/transfer/transfer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ describe('transfer', () => {
let pageObject: TransferPageObject<AbstractTestTransferComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [provideNzIconsTesting(), provideNoopAnimations()]
});
TestBed.configureTestingModule({ providers: [provideNzIconsTesting(), provideNoopAnimations()] });
fixture = TestBed.createComponent(TestTransferComponent);
debugElement = fixture.debugElement;
instance = debugElement.componentInstance;
Expand Down Expand Up @@ -210,6 +208,22 @@ describe('transfer', () => {
expect(instance.comp.rightDataSource.filter(w => w.checked).length).toBe(0);
});

it('should be checkboxes are toggle select via shift key', () => {
expect(instance.comp.rightDataSource.filter(w => w.checked).length).toBe(0);
pageObject.checkItem('right', 0);
expect(instance.comp.rightDataSource.filter(w => w.checked).length).toBe(1);
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Shift' }));
expect(instance.comp.isShiftPressed).toBeTrue();
fixture.detectChanges();
const multiSelectEndIndex = 9;
pageObject.checkItem('right', multiSelectEndIndex);
expect(instance.comp.rightDataSource.filter(w => w.checked).length).toBe(
COUNT - LEFTCOUNT - DISABLED - multiSelectEndIndex + 1
);
window.dispatchEvent(new KeyboardEvent('keyup', { key: 'Shift' }));
expect(instance.comp.isShiftPressed).toBeFalse();
});

describe('#notFoundContent', () => {
it('should be the left and right list have data', () => {
instance.nzDataSource = [{ title: `content0`, direction: 'right' }, { title: `content1` }];
Expand Down Expand Up @@ -680,22 +694,12 @@ class TestTransferComponent implements OnInit, AbstractTestTransferComponent {
})
class TestTransferCustomRenderComponent implements OnInit, AbstractTestTransferComponent {
@ViewChild('comp', { static: false }) comp!: NzTransferComponent;
nzDataSource: Array<{
key: string;
title: string;
description: string;
direction: TransferDirection;
icon: string;
}> = [];
nzDataSource: Array<{ key: string; title: string; description: string; direction: TransferDirection; icon: string }> =
[];

ngOnInit(): void {
const ret: Array<{
key: string;
title: string;
description: string;
direction: TransferDirection;
icon: string;
}> = [];
const ret: Array<{ key: string; title: string; description: string; direction: TransferDirection; icon: string }> =
[];
for (let i = 0; i < COUNT; i++) {
ret.push({
key: i.toString(),
Expand All @@ -710,21 +714,14 @@ class TestTransferCustomRenderComponent implements OnInit, AbstractTestTransferC
}

// https://github.com/NG-ZORRO/ng-zorro-antd/issues/996
@Component({
imports: [NzTransferModule],
template: `<nz-transfer [nzDataSource]="list"></nz-transfer>`
})
@Component({ imports: [NzTransferModule], template: `<nz-transfer [nzDataSource]="list"></nz-transfer>` })
class Test996Component implements OnInit {
@ViewChild(NzTransferComponent, { static: true }) comp!: NzTransferComponent;
list: NzSafeAny[] = [];

ngOnInit(): void {
for (let i = 0; i < 2; i++) {
this.list.push({
key: i.toString(),
title: `content${i + 1}`,
disabled: i % 3 < 1
});
this.list.push({ key: i.toString(), title: `content${i + 1}`, disabled: i % 3 < 1 });
}

[0, 1].forEach(idx => (this.list[idx].direction = 'right'));
Expand Down
Loading