Skip to content
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

fix(module:select): fix keyboard event error when option data is empty #7222

Merged
merged 3 commits into from Feb 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/select/select.component.ts
Expand Up @@ -403,14 +403,14 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
switch (e.keyCode) {
case UP_ARROW:
e.preventDefault();
if (this.nzOpen) {
if (this.nzOpen && listOfFilteredOptionNotDisabled.length > 0) {
const preIndex = activatedIndex > 0 ? activatedIndex - 1 : listOfFilteredOptionNotDisabled.length - 1;
this.activatedValue = listOfFilteredOptionNotDisabled[preIndex].nzValue;
}
break;
case DOWN_ARROW:
e.preventDefault();
if (this.nzOpen) {
if (this.nzOpen && listOfFilteredOptionNotDisabled.length > 0) {
const nextIndex = activatedIndex < listOfFilteredOptionNotDisabled.length - 1 ? activatedIndex + 1 : 0;
this.activatedValue = listOfFilteredOptionNotDisabled[nextIndex].nzValue;
} else {
Expand Down
18 changes: 18 additions & 0 deletions components/select/select.spec.ts
Expand Up @@ -331,6 +331,24 @@ describe('select', () => {
expect(component.openChange).toHaveBeenCalledWith(false);
expect(component.openChange).toHaveBeenCalledTimes(3);
}));

it('should not throw error with keydown up arrow and down arrow event when listOfOption is empty', fakeAsync(() => {
const flushChanges = (): void => {
fixture.detectChanges();
flush();
fixture.detectChanges();
};
component.listOfOption = [];
component.nzOpen = true;
flushChanges();
const inputElement = selectElement.querySelector('input')!;
dispatchKeyboardEvent(inputElement, 'keydown', UP_ARROW, inputElement);
flushChanges();
dispatchKeyboardEvent(inputElement, 'keydown', DOWN_ARROW, inputElement);
flushChanges();
expect(component.valueChange).toHaveBeenCalledTimes(0);
}));

it('should mouseenter activated option work', fakeAsync(() => {
const flushChanges = (): void => {
fixture.detectChanges();
Expand Down