Skip to content

Commit

Permalink
fix unselect item
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao committed Jun 12, 2018
1 parent 77e1ab1 commit cc11fa3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 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 { ItemsList } from './items-list';

fdescribe('ItemsList', () => {
describe('ItemsList', () => {
describe('select', () => {
describe('single', () => {
let list: ItemsList;
Expand Down Expand Up @@ -210,7 +210,7 @@ fdescribe('ItemsList', () => {
list.unselect(list.items[1]); // K1

expect(list.selectedItems.length).toBe(1);
expect(list.selectedItems[0]).toBe(list.items[2]);
expect(list.selectedItems[0].label).toBe(list.items[2].label); // should select only K2
});

it('should unselect selected item and insert it back to filtered items list when hideSelected=true', () => {
Expand Down
13 changes: 9 additions & 4 deletions src/ng-select/selection-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class SelectionModel {
this._selected.push(item);
if (multiple) {
if (isDefined(item.parent)) {
this._selected = this._selected.filter(x => x !== parent);
this._removeParent(item.parent);
const childrenCount = item.parent.children ? item.parent.children.length : 0;
const selectedCount = this._selected.filter(x => x.parent === item.parent).length;
item.parent.selected = childrenCount === selectedCount;
Expand All @@ -30,9 +30,10 @@ export class SelectionModel {
item.selected = false;
if (multiple) {
if (isDefined(item.parent) && item.parent.selected) {
// const children = item.parent.children;
// this._removeSelectedChildren(children);
// this._selected.push(...children.filter(x => x === item));
const children = item.parent.children;
this._removeParent(item.parent);
this._removeSelectedChildren(children);
this._selected.push(...children.filter(x => x !== item));
item.parent.selected = false;
} else if (item.children) {
const children = item.children
Expand All @@ -53,4 +54,8 @@ export class SelectionModel {
private _removeSelectedChildren(children: NgOption[]) {
this._selected = this._selected.filter(x => children.indexOf(x) === -1);
}

private _removeParent(parent: NgOption) {
this._selected = this._selected.filter(x => x !== parent)
}
}

0 comments on commit cc11fa3

Please sign in to comment.