Skip to content

Commit

Permalink
fix(module:select): disabled option can be selected by Enter (#7686)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyship committed Dec 11, 2022
1 parent d9f9092 commit 5bdf244
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components/select/select.component.ts
Expand Up @@ -451,7 +451,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
case ENTER:
e.preventDefault();
if (this.nzOpen) {
if (isNotNil(this.activatedValue)) {
if (isNotNil(this.activatedValue) && activatedIndex !== -1) {
this.onItemClick(this.activatedValue);
}
} else {
Expand Down
52 changes: 52 additions & 0 deletions components/select/select.spec.ts
Expand Up @@ -276,6 +276,58 @@ describe('select', () => {
expect(selectElement.querySelector('input')!.getAttribute('disabled')).toBe('');
}));

it('should select option by enter', fakeAsync(() => {
const flushChanges = (): void => {
fixture.detectChanges();
flush();
fixture.detectChanges();
};
component.listOfOption = [
{ nzValue: 'value', nzLabel: 'label' },
{ nzValue: 'disabledValue', nzLabel: 'disabledLabel', nzDisabled: true }
];
component.nzShowSearch = true;
component.nzOpen = true;

fixture.detectChanges();
const inputElement = selectElement.querySelector('input')!;
inputElement.value = 'label';

dispatchFakeEvent(inputElement, 'input');
flushChanges();
expect(component.searchValueChange).toHaveBeenCalledWith('label');

dispatchKeyboardEvent(inputElement, 'keydown', ENTER, inputElement);
flushChanges();
expect(component.value).toBe('value');
}));

it('should nzDisabled option works', fakeAsync(() => {
const flushChanges = (): void => {
fixture.detectChanges();
flush();
fixture.detectChanges();
};
component.listOfOption = [
{ nzValue: 'value', nzLabel: 'label' },
{ nzValue: 'disabledValue', nzLabel: 'disabledLabel', nzDisabled: true }
];
component.nzShowSearch = true;
component.nzOpen = true;

fixture.detectChanges();
const inputElement = selectElement.querySelector('input')!;
inputElement.value = 'disabled';

dispatchFakeEvent(inputElement, 'input');
flushChanges();
expect(component.searchValueChange).toHaveBeenCalledWith('disabled');

dispatchKeyboardEvent(inputElement, 'keydown', ENTER, inputElement);
flushChanges();
expect(component.value).not.toBe('disabledValue');
}));

it('should nzBackdrop works', fakeAsync(() => {
component.nzOpen = true;
component.nzBackdrop = true;
Expand Down

0 comments on commit 5bdf244

Please sign in to comment.