Skip to content

Commit

Permalink
Merge f31b5ae into c552663
Browse files Browse the repository at this point in the history
  • Loading branch information
varnastadeus committed Jun 25, 2019
2 parents c552663 + f31b5ae commit abe3392
Show file tree
Hide file tree
Showing 15 changed files with 499 additions and 408 deletions.
3 changes: 1 addition & 2 deletions demo/app/examples/groups.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { Component, ChangeDetectionStrategy } from '@angular/core';
bindLabel="name"
bindValue="name"
groupBy="country"
[multiple]="true"
[(ngModel)]="selectedAccount">
<ng-template ng-optgroup-tmp let-item="item">
{{item.country || 'Unnamed group'}}
Expand Down Expand Up @@ -149,7 +148,7 @@ import { Component, ChangeDetectionStrategy } from '@angular/core';
`
})
export class SelectGroupsComponent {
selectedAccount = ['Samantha'];
selectedAccount = 'Michael';
accounts = [
{ name: 'Jill', email: 'jill@email.com', age: 15, country: undefined, child: { state: 'Active' } },
{ name: 'Henry', email: 'henry@email.com', age: 10, country: undefined, child: { state: 'Active' } },
Expand Down
1 change: 1 addition & 0 deletions demo/app/examples/virtual-scroll.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { HttpClient } from '@angular/common/http';
bindLabel="title"
bindValue="thumbnailUrl"
placeholder="Select photo"
appendTo="body"
(scroll)="onScroll($event)"
(scrollToEnd)="onScrollToEnd()">
<ng-template ng-header-tmp>
Expand Down
33 changes: 31 additions & 2 deletions src/ng-select/items-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NgSelectComponent } from './ng-select.component';
import { NgSelectConfig } from './config.service';
import { ItemsList } from './items-list';
import { NgSelectComponent } from './ng-select.component';
import { DefaultSelectionModel } from './selection-model';
import { NgSelectConfig } from './config.service';

describe('ItemsList', () => {
describe('select', () => {
Expand Down Expand Up @@ -427,6 +427,35 @@ describe('ItemsList', () => {
});
});

describe('markSelectedOrDefault', () => {
let list: ItemsList;
let cmp: NgSelectComponent;

beforeEach(() => {
cmp = ngSelectFactory();
list = itemsListFactory(cmp);
const items = Array.from(Array(30)).map((_, index) => (`item-${index}`));
list.setItems(items);
});

it('should mark first item', () => {
list.markSelectedOrDefault(true);
expect(list.markedIndex).toBe(0);
});

it('should keep marked item if it is above last selected item', () => {
list.select(list.items[10]);
list.markSelectedOrDefault();
expect(list.markedIndex).toBe(10);

list.markNextItem();
list.markNextItem();
list.markNextItem();
list.markSelectedOrDefault();
expect(list.markedIndex).toBe(13);
});
});

function itemsListFactory(cmp: NgSelectComponent): ItemsList {
return new ItemsList(cmp, new DefaultSelectionModel());
}
Expand Down
20 changes: 17 additions & 3 deletions src/ng-select/items-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,10 @@ export class ItemsList {
if (this._filteredItems.length === 0) {
return;
}
const indexOfLastSelected = this._ngSelect.hideSelected ? -1 : this._filteredItems.indexOf(this.lastSelectedItem);
if (this.lastSelectedItem && indexOfLastSelected > -1) {
this._markedIndex = indexOfLastSelected;

const lastMarkedIndex = this._getLastMarkedIndex();
if (lastMarkedIndex > -1) {
this._markedIndex = lastMarkedIndex;
} else {
if (this._ngSelect.excludeGroupsFromDefaultSelection) {
this._markedIndex = markDefault ? this.filteredItems.findIndex(x => !x.disabled && !x.children) : -1;
Expand Down Expand Up @@ -310,6 +311,19 @@ export class ItemsList {
}
}

private _getLastMarkedIndex() {
if (this._ngSelect.hideSelected) {
return -1;
}

const selectedIndex = this._filteredItems.indexOf(this.lastSelectedItem);
if (this.lastSelectedItem && selectedIndex < 0) {
return -1;
}

return Math.max(this.markedIndex, selectedIndex);
}

private _groupBy(items: NgOption[], prop: string | Function): OptionGroups {
const groups = new Map<string | NgOption, NgOption[]>();
if (items.length === 0) {
Expand Down

0 comments on commit abe3392

Please sign in to comment.