Skip to content

Commit e02f1f4

Browse files
authored
feat(module:cascader): toggle checkbox of option by ENTER key in multiple mode (#9457)
1 parent 4499725 commit e02f1f4

2 files changed

Lines changed: 72 additions & 5 deletions

File tree

‎components/cascader/cascader.component.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,11 @@ export class NzCascaderComponent
10221022
const columnIndex = Math.max(this.cascaderService.activatedNodes.length - 1, 0);
10231023
const node = this.cascaderService.activatedNodes[columnIndex];
10241024
if (node && !node.isDisabled) {
1025-
this.inSearchingMode
1026-
? this.cascaderService.setSearchOptionSelected(node)
1027-
: this.cascaderService.setNodeActivated(node, columnIndex, true);
1025+
this.nzMultiple
1026+
? this.onOptionCheck(node, columnIndex, true)
1027+
: this.inSearchingMode
1028+
? this.cascaderService.setSearchOptionSelected(node)
1029+
: this.cascaderService.setNodeActivated(node, columnIndex, true);
10281030
}
10291031
}
10301032

@@ -1047,7 +1049,7 @@ export class NzCascaderComponent
10471049
break;
10481050
}
10491051
const nextOption = options[nextIndex];
1050-
if (!nextOption || nextOption.isDisabled) {
1052+
if (!nextOption || nextOption.isDisabled || nextOption.isDisableCheckbox) {
10511053
continue;
10521054
}
10531055
this.cascaderService.setNodeActivated(nextOption, columnIndex);
@@ -1069,7 +1071,7 @@ export class NzCascaderComponent
10691071
const length = this.cascaderService.activatedNodes.length;
10701072
const options = this.cascaderService.columns[length];
10711073
if (options && options.length) {
1072-
const nextOpt = options.find(o => !o.isDisabled);
1074+
const nextOpt = options.find(o => !o.isDisabled && !o.isDisableCheckbox);
10731075
if (nextOpt) {
10741076
this.cascaderService.setNodeActivated(nextOpt, length);
10751077
}

‎components/cascader/cascader.spec.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,71 @@ describe('cascader', () => {
20852085
expect(testComponent.onChanges).toHaveBeenCalledWith([['light']]);
20862086
}));
20872087

2088+
it('should support ENTER key to toggle option checked state in multiple mode', fakeAsync(() => {
2089+
cascader.componentInstance.setMenuVisible(true);
2090+
fixture.detectChanges();
2091+
tick(600);
2092+
fixture.detectChanges();
2093+
getItemAtColumnAndRow(1, 1)!.click();
2094+
fixture.detectChanges();
2095+
const optionEl = getItemAtColumnAndRow(2, 1)!;
2096+
const checkboxEl = getCheckboxAtColumnAndRow(2, 1)!;
2097+
// Initially, the option should not be checked
2098+
expect(checkboxEl.classList).not.toContain('ant-cascader-checkbox-checked');
2099+
dispatchKeyboardEvent(cascader.nativeElement, 'keydown', RIGHT_ARROW);
2100+
fixture.detectChanges();
2101+
expect(optionEl.classList).toContain('ant-cascader-menu-item-active');
2102+
// Press ENTER to toggle the option checked state
2103+
dispatchKeyboardEvent(cascader.nativeElement, 'keydown', ENTER);
2104+
fixture.detectChanges();
2105+
// The option should now be checked
2106+
expect(checkboxEl.classList).toContain('ant-cascader-checkbox-checked');
2107+
// Press ENTER again to toggle the option unchecked state
2108+
dispatchKeyboardEvent(cascader.nativeElement, 'keydown', ENTER);
2109+
fixture.detectChanges();
2110+
// The option should now be unchecked
2111+
expect(checkboxEl.classList).not.toContain('ant-cascader-checkbox-checked');
2112+
}));
2113+
2114+
it('should not activate option with isDisableCheckbox by pressing RIGHT ARROW key', fakeAsync(() => {
2115+
cascader.componentInstance.setMenuVisible(true);
2116+
fixture.detectChanges();
2117+
tick(600);
2118+
fixture.detectChanges();
2119+
getItemAtColumnAndRow(1, 2)!.click();
2120+
fixture.detectChanges();
2121+
getItemAtColumnAndRow(2, 1)!.click();
2122+
fixture.detectChanges();
2123+
// Try to navigate to a disabled checkbox option using keyboard
2124+
dispatchKeyboardEvent(cascader.nativeElement, 'keydown', RIGHT_ARROW);
2125+
fixture.detectChanges();
2126+
// The option with disableCheckbox should not be activatable via keyboard
2127+
expect(getItemAtColumnAndRow(3, 1)!.classList).not.toContain('ant-cascader-menu-item-active');
2128+
expect(getCheckboxAtColumnAndRow(3, 1)!.classList).toContain('ant-cascader-checkbox-disabled');
2129+
// activate item (3, 2)
2130+
expect(getItemAtColumnAndRow(3, 2)!.classList).toContain('ant-cascader-menu-item-active');
2131+
expect(getCheckboxAtColumnAndRow(3, 2)!.classList).not.toContain('ant-cascader-checkbox-disabled');
2132+
}));
2133+
2134+
it('should not activate option with isDisableCheckbox in moveUpOrDown method', fakeAsync(() => {
2135+
cascader.componentInstance.setMenuVisible(true);
2136+
fixture.detectChanges();
2137+
tick(600);
2138+
fixture.detectChanges();
2139+
getItemAtColumnAndRow(1, 2)!.click();
2140+
fixture.detectChanges();
2141+
getItemAtColumnAndRow(2, 1)!.click();
2142+
fixture.detectChanges();
2143+
getItemAtColumnAndRow(3, 2)!.click();
2144+
fixture.detectChanges();
2145+
expect(getItemAtColumnAndRow(3, 2)!.classList).toContain('ant-cascader-menu-item-active');
2146+
// Up key
2147+
dispatchKeyboardEvent(cascader.nativeElement, 'keydown', UP_ARROW);
2148+
// The option with disableCheckbox should not be activatable via keyboard
2149+
expect(getItemAtColumnAndRow(3, 1)!.classList).not.toContain('ant-cascader-menu-item-active');
2150+
expect(getItemAtColumnAndRow(3, 2)!.classList).toContain('ant-cascader-menu-item-active');
2151+
}));
2152+
20882153
describe('should cascade work when the value of ngModel includes nodes that are not existed in options', () => {
20892154
it('should remove item work', fakeAsync(() => {
20902155
setValues(2);

0 commit comments

Comments
 (0)