Skip to content

Commit

Permalink
fix: The enter key works wrong when used immediately after filtering #60
Browse files Browse the repository at this point in the history
  • Loading branch information
optimistex committed Apr 14, 2018
1 parent 5315d56 commit f219709
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/app/lib/ngx-select/ngx-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ describe('NgxSelectComponent', () => {
formControlInput(1).dispatchEvent(createKeyboardEvent('input', 'keyR'));
fixture.detectChanges();
expect(selectItemList(1).length).toBe(3);
expect(selectItemList(1)[0]).toEqual(selectItemActive(1));
});

it('with lazy load items', () => {
Expand Down
12 changes: 9 additions & 3 deletions src/app/lib/ngx-select/ngx-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface INgxSelectComponentMouseEvent extends MouseEvent {

enum ENavigation {
first, previous, next, last,
firstSelected
firstSelected, firstIfOptionActiveInvisible
}

function propertyExists(obj: Object, propertyName: string) {
Expand Down Expand Up @@ -182,11 +182,12 @@ export class NgxSelectComponent implements INgxSelectOptions, ControlValueAccess
.combineLatest(this.subjOptionsSelected, this.subjSearchText,
(options: TSelectOption[], selectedOptions: NgxSelectOption[], search: string) => {
this.optionsFiltered = this.filterOptions(search, options, selectedOptions);
this.cacheOptionsFilteredFlat = null;
this.navigateOption(ENavigation.firstIfOptionActiveInvisible);
return selectedOptions;
}
)
.flatMap((selectedOptions: NgxSelectOption[]) => {
this.cacheOptionsFilteredFlat = null;
return this.optionsFilteredFlat().filter((flatOptions: NgxSelectOption[]) =>
this.autoSelectSingleOption && flatOptions.length === 1 && !selectedOptions.length
);
Expand Down Expand Up @@ -273,6 +274,10 @@ export class NgxSelectComponent implements INgxSelectOptions, ControlValueAccess
navigated.index = options.indexOf(this.subjOptionsSelected.value[0]);
}
break;
case ENavigation.firstIfOptionActiveInvisible:
const idxOfOptionActive = options.indexOf(this.optionActive);
navigated.index = idxOfOptionActive > 0 ? idxOfOptionActive : 0;
break;
}
navigated.activeOption = options[navigated.index];
return navigated;
Expand Down Expand Up @@ -427,7 +432,8 @@ export class NgxSelectComponent implements INgxSelectOptions, ControlValueAccess
}

protected optionActivate(navigated: INgxOptionNavigated): void {
if (!navigated.activeOption || !navigated.activeOption.disabled) {
if ((this.optionActive !== navigated.activeOption) &&
(!navigated.activeOption || !navigated.activeOption.disabled)) {
this.optionActive = navigated.activeOption;
this.navigated.emit(navigated);
}
Expand Down

0 comments on commit f219709

Please sign in to comment.