Skip to content

Commit 4bd86ca

Browse files
authored
fix(module:select): fix keyboard event error when option data is empty (#7222)
close #7242 * fix(module:select): fix keyboard event error when option data is null * chore: move the logic place * test(module:select): add test case
1 parent ad547fb commit 4bd86ca

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

components/select/select.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,14 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
403403
switch (e.keyCode) {
404404
case UP_ARROW:
405405
e.preventDefault();
406-
if (this.nzOpen) {
406+
if (this.nzOpen && listOfFilteredOptionNotDisabled.length > 0) {
407407
const preIndex = activatedIndex > 0 ? activatedIndex - 1 : listOfFilteredOptionNotDisabled.length - 1;
408408
this.activatedValue = listOfFilteredOptionNotDisabled[preIndex].nzValue;
409409
}
410410
break;
411411
case DOWN_ARROW:
412412
e.preventDefault();
413-
if (this.nzOpen) {
413+
if (this.nzOpen && listOfFilteredOptionNotDisabled.length > 0) {
414414
const nextIndex = activatedIndex < listOfFilteredOptionNotDisabled.length - 1 ? activatedIndex + 1 : 0;
415415
this.activatedValue = listOfFilteredOptionNotDisabled[nextIndex].nzValue;
416416
} else {

components/select/select.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,24 @@ describe('select', () => {
331331
expect(component.openChange).toHaveBeenCalledWith(false);
332332
expect(component.openChange).toHaveBeenCalledTimes(3);
333333
}));
334+
335+
it('should not throw error with keydown up arrow and down arrow event when listOfOption is empty', fakeAsync(() => {
336+
const flushChanges = (): void => {
337+
fixture.detectChanges();
338+
flush();
339+
fixture.detectChanges();
340+
};
341+
component.listOfOption = [];
342+
component.nzOpen = true;
343+
flushChanges();
344+
const inputElement = selectElement.querySelector('input')!;
345+
dispatchKeyboardEvent(inputElement, 'keydown', UP_ARROW, inputElement);
346+
flushChanges();
347+
dispatchKeyboardEvent(inputElement, 'keydown', DOWN_ARROW, inputElement);
348+
flushChanges();
349+
expect(component.valueChange).toHaveBeenCalledTimes(0);
350+
}));
351+
334352
it('should mouseenter activated option work', fakeAsync(() => {
335353
const flushChanges = (): void => {
336354
fixture.detectChanges();

0 commit comments

Comments
 (0)