Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
NileshPatel17 committed Sep 21, 2019
1 parent 9ed3a82 commit 3ecef09
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 42 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-multiselect-dropdown",
"version": "0.2.4",
"version": "0.2.5",
"private": true,
"description": "Angular Multi-Select Dropdown",
"author": "Nilesh Patel",
Expand Down Expand Up @@ -79,7 +79,7 @@
"karma-coverage-istanbul-reporter": "^2.1.0",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"ng-multiselect-dropdown": "^0.2.3",
"ng-multiselect-dropdown": "^0.2.5",
"ng-packagr": "^5.5.0",
"ngx-bootstrap": "^5.1.1",
"protractor": "~5.4.2",
Expand All @@ -91,4 +91,4 @@
"typescript": "3.5.3",
"zone.js": "^0.10.2"
}
}
}
2 changes: 1 addition & 1 deletion src/app/components/select/multiple-demo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FormBuilder, FormGroup } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { IDropdownSettings } from 'ng-multiselect-dropdown';
import { IDropdownSettings } from '../../../ng-multiselect-dropdown/src';

@Component({
selector: 'multiple-demo',
Expand Down
63 changes: 25 additions & 38 deletions src/ng-multiselect-dropdown/src/multiselect.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
Component,
HostListener,
forwardRef,
Input,
Output,
EventEmitter,
ChangeDetectionStrategy,
ChangeDetectorRef
} from '@angular/core';
import { Component, HostListener, forwardRef, Input, Output, EventEmitter, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { ListItem, IDropdownSettings } from './multiselect.model';

Expand Down Expand Up @@ -82,15 +73,14 @@ export class MultiSelectComponent implements ControlValueAccessor {
// return item;
// }
// });
this._data = value.map(
(item: any) =>
typeof item === 'string'
? new ListItem(item)
: new ListItem({
id: item[this._settings.idField],
text: item[this._settings.textField],
isDisabled: item[this._settings.disabledField]
})
this._data = value.map((item: any) =>
typeof item === 'string'
? new ListItem(item)
: new ListItem({
id: item[this._settings.idField],
text: item[this._settings.textField],
isDisabled: item[this._settings.disabledField]
})
);
}
}
Expand Down Expand Up @@ -127,21 +117,15 @@ export class MultiSelectComponent implements ControlValueAccessor {
}

const found = this.isSelected(item);
const allowAdd =
this._settings.limitSelection === -1 ||
(this._settings.limitSelection > 0 &&
this.selectedItems.length < this._settings.limitSelection);
const allowAdd = this._settings.limitSelection === -1 || (this._settings.limitSelection > 0 && this.selectedItems.length < this._settings.limitSelection);
if (!found) {
if (allowAdd) {
this.addSelected(item);
}
} else {
this.removeSelected(item);
}
if (
this._settings.singleSelection &&
this._settings.closeDropDownOnSelection
) {
if (this._settings.singleSelection && this._settings.closeDropDownOnSelection) {
this.closeDropdown();
}
}
Expand All @@ -166,15 +150,14 @@ export class MultiSelectComponent implements ControlValueAccessor {
// console.error(e.body.msg);
}
} else {
const _data = value.map(
(item: any) =>
typeof item === 'string'
? new ListItem(item)
: new ListItem({
id: item[this._settings.idField],
text: item[this._settings.textField],
isDisabled: item[this._settings.disabledField]
})
const _data = value.map((item: any) =>
typeof item === 'string'
? new ListItem(item)
: new ListItem({
id: item[this._settings.idField],
text: item[this._settings.textField],
isDisabled: item[this._settings.disabledField]
})
);
if (this._settings.limitSelection > 0) {
this.selectedItems = _data.splice(0, this._settings.limitSelection);
Expand Down Expand Up @@ -224,7 +207,10 @@ export class MultiSelectComponent implements ControlValueAccessor {
}

isAllItemsSelected(): boolean {
return this._data.length === this.selectedItems.length;
// get disabld item count
const itemDisabledCount = this._data.filter(item => item.isDisabled).length;
// take disabled items into consideration when checking
return this._data.length === this.selectedItems.length + itemDisabledCount;
}

showButton(): boolean {
Expand Down Expand Up @@ -320,7 +306,8 @@ export class MultiSelectComponent implements ControlValueAccessor {
return false;
}
if (!this.isAllItemsSelected()) {
this.selectedItems = this._data.slice();
// filter out disabled item first before slicing
this.selectedItems = this._data.filter(item => !item.isDisabled).slice();
this.onSelectAll.emit(this.emittedValue(this.selectedItems));
} else {
this.selectedItems = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Component, Type, ViewChild, DebugElement } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ComponentFixture, fakeAsync } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { MultiSelectComponent } from './../src/multiselect.component';
import { createTestingModule, tickAndDetectChanges } from './helper';

@Component({
template: ``
})
class Ng2MultiSelectDropdownMultipleSelectWithDisableItemComponent {
@ViewChild(MultiSelectComponent, { static: false })
select: MultiSelectComponent;
cities = [
{ item_id: 1, item_text: 'Mumbai' },
{ item_id: 2, item_text: 'Bangalore' },
{ item_id: 3, item_text: 'Pune', isDisabled: true },
{ item_id: 4, item_text: 'Navsari' },
{ item_id: 5, item_text: 'New Delhi' }
];
selectedItem = [{ item_id: 1, item_text: 'Mumbai' }, { item_id: 4, item_text: 'Navsari' }];
dropdownSettings = {
singleSelection: false,
idField: 'item_id',
textField: 'item_text',
selectAllText: 'Select All',
unSelectAllText: 'UnSelect All',
badgeShowLimit: 3,
disabled: false,
allowSearchFilter: true,
closeDropDownOnSelection: true
};
}
describe('Multiple Selection:disable item', () => {
let fixture: ComponentFixture<Ng2MultiSelectDropdownMultipleSelectWithDisableItemComponent>;
beforeEach(fakeAsync(() => {
fixture = createTestingModule(
Ng2MultiSelectDropdownMultipleSelectWithDisableItemComponent,
`<div class='container'>
<ng-multiselect-dropdown name="city" [data]="cities"
[(ngModel)]="selectedItem" [settings]="dropdownSettings"
(onSelect)="onItemSelect($event)"
[disabled]="disabled">
</ng-multiselect-dropdown>
</div>`
);
}));
it('should have 4 items enabled and 1 item disabled', fakeAsync(() => {
let selCheckBoxes: HTMLLIElement[];
const sel = fixture.nativeElement.querySelectorAll('.multiselect-item-checkbox');
selCheckBoxes = Array.from(sel);
// tickAndDetectChanges(fixture);
// Mumbai
expect(selCheckBoxes[1].querySelector('div').textContent).toContain('Mumbai');
expect(selCheckBoxes[1].querySelector('input').disabled).toBe(false);
// Bangalore
expect(selCheckBoxes[2].querySelector('div').textContent).toContain('Bangalore');
expect(selCheckBoxes[2].querySelector('input').disabled).toBe(false);
expect(selCheckBoxes[3].querySelector('div').textContent).toContain('Pune');
expect(selCheckBoxes[3].querySelector('input').disabled).toBe(true); // Pune should have disable attribute

// Navsari
expect(selCheckBoxes[4].querySelector('div').textContent).toContain('Navsari');
expect(selCheckBoxes[4].querySelector('input').disabled).toBe(false);
// New Delhi
expect(selCheckBoxes[5].querySelector('div').textContent).toContain('New Delhi');
expect(selCheckBoxes[5].querySelector('input').disabled).toBe(false);

expect(fixture.componentInstance.selectedItem.length).toEqual(2);
}));
it('should not select disabled item-Pune', fakeAsync(() => {
const index = 3; // 0 is select all checkbox
let selCheckBoxes: HTMLLIElement[];
const sel = fixture.nativeElement.querySelectorAll('.multiselect-item-checkbox');
selCheckBoxes = Array.from(sel);
selCheckBoxes[index].click();
selCheckBoxes[index - 1].click(); // select Bangalore
tickAndDetectChanges(fixture);
expect(selCheckBoxes[index - 1].querySelector('input').disabled).toBe(false); // Bangalore should not have disable attribute
expect(selCheckBoxes[index].querySelector('input').disabled).toBe(true); // Pune should have disable attribute
expect(selCheckBoxes[index].querySelector('div').textContent).toContain('Pune');
expect(fixture.componentInstance.selectedItem.length).toEqual(3);
}));
it('should not select disabled item when selecting all items', fakeAsync(() => {
const index = 0; // 0 is select all checkbox
let selCheckBoxes: HTMLLIElement[];
const sel = fixture.nativeElement.querySelectorAll('.multiselect-item-checkbox');
selCheckBoxes = Array.from(sel);
selCheckBoxes[index].click(); // click on "select all"
tickAndDetectChanges(fixture);
expect(selCheckBoxes[3].querySelector('div').textContent).toContain('Pune');
expect(selCheckBoxes[3].querySelector('input').disabled).toBe(true); // Pune should have disable attribute
expect(fixture.componentInstance.selectedItem.length).toEqual(4);
}));
});

0 comments on commit 3ecef09

Please sign in to comment.