From 5bdf2440bc48547bc76a6646cce49fd8b036beb3 Mon Sep 17 00:00:00 2001 From: Xu Chao <304093931@qq.com> Date: Sun, 11 Dec 2022 11:01:15 +0800 Subject: [PATCH] fix(module:select): disabled option can be selected by Enter (#7686) --- components/select/select.component.ts | 2 +- components/select/select.spec.ts | 52 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/components/select/select.component.ts b/components/select/select.component.ts index ebc569295a..e51aa0ac94 100644 --- a/components/select/select.component.ts +++ b/components/select/select.component.ts @@ -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 { diff --git a/components/select/select.spec.ts b/components/select/select.spec.ts index 4ff8f408dc..4e64271139 100644 --- a/components/select/select.spec.ts +++ b/components/select/select.spec.ts @@ -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;