Skip to content

Commit

Permalink
feat(openOnEnter): allow to configure whether enter opens dropdown
Browse files Browse the repository at this point in the history
closes #878
  • Loading branch information
jakemdunn authored and varnastadeus committed Oct 18, 2018
1 parent 28c2ebf commit fe9d990
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ map: {
| [searchFn] | `(term: string, item: any) => boolean` | `null` | no | Allow to filter by custom search function |
| [clearSearchOnAdd] | `boolean` | `true` | no | Clears search input when item is selected. Default `true`. Default `false` when **closeOnSelect** is `false` |
| [selectOnTab] | `boolean` | `false` | no | Select marked dropdown item using tab. Default `false`|
| [openOnEnter] | `boolean` | `true` | no | Open dropdown using enter. Default `true`|
| [typeahead] | `Subject` | `-` | no | Custom autocomplete or advanced filter. |
| typeToSearchText | `string` | `Type to search` | no | Set custom text when using Typeahead |
| [virtualScroll] | `boolean` | false | no | Enable virtual scroll for better performance when rendering a lot of data |
Expand Down
6 changes: 6 additions & 0 deletions src/ng-select/ng-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,12 @@ describe('NgSelectComponent', function () {
expect(select.selectedItems[0].value).toEqual(fixture.componentInstance.cities[0]);
expect(select.isOpen).toBe(false);
});

it('should not open dropdown if [openOnEnter]="false"', fakeAsync(() => {
select.openOnEnter = false;
triggerKeyDownEvent(getNgSelectElement(fixture), KeyCode.Enter);
expect(select.isOpen).toBe(false);
}));
});
});

Expand Down
7 changes: 6 additions & 1 deletion src/ng-select/ng-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C
@Input() closeOnSelect = true;
@Input() hideSelected = false;
@Input() selectOnTab = false;
@Input() openOnEnter: boolean;
@Input() maxSelectedItems: number;
@Input() groupBy: string | Function;
@Input() groupValue: Function;
Expand Down Expand Up @@ -726,9 +727,12 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C
} else if (this.showAddTag) {
this.selectTag();
}
} else {
} else if (this.openOnEnter) {
this.open();
} else {
return;
}

$event.preventDefault();
$event.stopPropagation();
}
Expand Down Expand Up @@ -798,5 +802,6 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C
this.addTagText = this.addTagText || config.addTagText;
this.loadingText = this.loadingText || config.loadingText;
this.clearAllText = this.clearAllText || config.clearAllText;
this.openOnEnter = isDefined(this.openOnEnter) ? this.openOnEnter : config.openOnEnter;
}
}
3 changes: 2 additions & 1 deletion src/ng-select/ng-select.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ import { DefaultSelectionModelFactory } from './selection-model';
addTagText: 'Add item',
loadingText: 'Loading...',
clearAllText: 'Clear all',
disableVirtualScroll: false
disableVirtualScroll: false,
openOnEnter: true
}
}
]
Expand Down
1 change: 1 addition & 0 deletions src/ng-select/ng-select.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum KeyCode {
}

export interface NgSelectConfig {
openOnEnter?: boolean;
placeholder?: string;
notFoundText?: string;
typeToSearchText?: string;
Expand Down

0 comments on commit fe9d990

Please sign in to comment.