Skip to content

Commit 5bdf244

Browse files
authored
fix(module:select): disabled option can be selected by Enter (#7686)
1 parent d9f9092 commit 5bdf244

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

components/select/select.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterCon
451451
case ENTER:
452452
e.preventDefault();
453453
if (this.nzOpen) {
454-
if (isNotNil(this.activatedValue)) {
454+
if (isNotNil(this.activatedValue) && activatedIndex !== -1) {
455455
this.onItemClick(this.activatedValue);
456456
}
457457
} else {

components/select/select.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,58 @@ describe('select', () => {
276276
expect(selectElement.querySelector('input')!.getAttribute('disabled')).toBe('');
277277
}));
278278

279+
it('should select option by enter', fakeAsync(() => {
280+
const flushChanges = (): void => {
281+
fixture.detectChanges();
282+
flush();
283+
fixture.detectChanges();
284+
};
285+
component.listOfOption = [
286+
{ nzValue: 'value', nzLabel: 'label' },
287+
{ nzValue: 'disabledValue', nzLabel: 'disabledLabel', nzDisabled: true }
288+
];
289+
component.nzShowSearch = true;
290+
component.nzOpen = true;
291+
292+
fixture.detectChanges();
293+
const inputElement = selectElement.querySelector('input')!;
294+
inputElement.value = 'label';
295+
296+
dispatchFakeEvent(inputElement, 'input');
297+
flushChanges();
298+
expect(component.searchValueChange).toHaveBeenCalledWith('label');
299+
300+
dispatchKeyboardEvent(inputElement, 'keydown', ENTER, inputElement);
301+
flushChanges();
302+
expect(component.value).toBe('value');
303+
}));
304+
305+
it('should nzDisabled option works', fakeAsync(() => {
306+
const flushChanges = (): void => {
307+
fixture.detectChanges();
308+
flush();
309+
fixture.detectChanges();
310+
};
311+
component.listOfOption = [
312+
{ nzValue: 'value', nzLabel: 'label' },
313+
{ nzValue: 'disabledValue', nzLabel: 'disabledLabel', nzDisabled: true }
314+
];
315+
component.nzShowSearch = true;
316+
component.nzOpen = true;
317+
318+
fixture.detectChanges();
319+
const inputElement = selectElement.querySelector('input')!;
320+
inputElement.value = 'disabled';
321+
322+
dispatchFakeEvent(inputElement, 'input');
323+
flushChanges();
324+
expect(component.searchValueChange).toHaveBeenCalledWith('disabled');
325+
326+
dispatchKeyboardEvent(inputElement, 'keydown', ENTER, inputElement);
327+
flushChanges();
328+
expect(component.value).not.toBe('disabledValue');
329+
}));
330+
279331
it('should nzBackdrop works', fakeAsync(() => {
280332
component.nzOpen = true;
281333
component.nzBackdrop = true;

0 commit comments

Comments
 (0)