Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take selected disabled values into account when using select all/unse… #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ng-multiselect-dropdown/src/multiselect.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class MultiSelectComponent implements ControlValueAccessor {
if ((!this.data || this.data.length === 0) && this._settings.allowRemoteDataSearch) {
return false;
}
return filteredItems.length === this.selectedItems.length + itemDisabledCount;
return filteredItems.length === this.selectedItems.filter(item => !item.isDisabled).length + itemDisabledCount;
}

showButton(): boolean {
Expand Down Expand Up @@ -312,12 +312,14 @@ export class MultiSelectComponent implements ControlValueAccessor {
if (this.disabled) {
return false;
}
const selectedDisabledValues = this.selectedItems.filter(item => item.isDisabled);
if (!this.isAllItemsSelected()) {
// filter out disabled item first before slicing
this.selectedItems = this.listFilterPipe.transform(this._data,this.filter).filter(item => !item.isDisabled).slice();
selectedDisabledValues.forEach(disabledSelectedItem => this.selectedItems.push(disabledSelectedItem));
this.onSelectAll.emit(this.emittedValue(this.selectedItems));
} else {
this.selectedItems = [];
this.selectedItems = selectedDisabledValues;
this.onDeSelectAll.emit(this.emittedValue(this.selectedItems));
}
this.onChangeCallback(this.emittedValue(this.selectedItems));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ class Ng2MultiSelectDropdownMultipleSelectWithDisableItemComponent {
closeDropDownOnSelection: true
};
}
@Component({
template: ``
})
class Ng2MultiSelectDropdownMultipleSelectWithDisabledItemsComponent {
@ViewChild(MultiSelectComponent, { static: false })
select: MultiSelectComponent;
cities = [
{ item_id: 1, item_text: 'Mumbai' },
{ item_id: 2, item_text: 'Bangalore', isDisabled: true },
{ item_id: 3, item_text: 'Pune', isDisabled: true },
{ item_id: 4, item_text: 'Navsari' }
];
selectedItem = [{ item_id: 2, item_text: 'Bangalore', isDisabled: true }, { item_id: 3, item_text: 'Pune', isDisabled: true }];
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(() => {
Expand Down Expand Up @@ -65,7 +90,7 @@ describe('Multiple Selection:disable item', () => {
// 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(() => {
Expand Down Expand Up @@ -93,3 +118,131 @@ describe('Multiple Selection:disable item', () => {
expect(fixture.componentInstance.selectedItem.length).toEqual(4);
}));
});
describe('Multiple Selection:disabled items', () => {
let fixture: ComponentFixture<Ng2MultiSelectDropdownMultipleSelectWithDisabledItemsComponent>;
beforeEach(fakeAsync(() => {
fixture = createTestingModule(
Ng2MultiSelectDropdownMultipleSelectWithDisabledItemsComponent,
`<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 2 items enabled and 2 items disabled', fakeAsync(() => {
let selCheckBoxes: HTMLLIElement[];
const sel = fixture.nativeElement.querySelectorAll('.multiselect-item-checkbox');
selCheckBoxes = Array.from(sel);
// Text should be Select All
expect(selCheckBoxes[0].querySelector('div').textContent).toContain('Select All');
// 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(true); // Bangalore should have disable attribute
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);

expect(fixture.componentInstance.selectedItem.length).toEqual(2);
expect(fixture.componentInstance.selectedItem[0].item_id).toEqual(2);
expect(fixture.componentInstance.selectedItem[0].item_text).toEqual('Bangalore');
expect(fixture.componentInstance.selectedItem[1].item_id).toEqual(3);
expect(fixture.componentInstance.selectedItem[1].item_text).toEqual('Pune');
}));
it('should not de-select disabled item-Bangalore', fakeAsync(() => {
const index = 2; // 2 is item-Bangalore
let selCheckBoxes: HTMLLIElement[];
const sel = fixture.nativeElement.querySelectorAll('.multiselect-item-checkbox');
selCheckBoxes = Array.from(sel);
selCheckBoxes[index].click();
tickAndDetectChanges(fixture);
expect(selCheckBoxes[index].querySelector('input').disabled).toBe(true); // Bangalore should have disable attribute
expect(selCheckBoxes[index].querySelector('div').textContent).toContain('Bangalore');
expect(fixture.componentInstance.selectedItem.length).toEqual(2);
expect(fixture.componentInstance.selectedItem[0].item_id).toEqual(2);
expect(fixture.componentInstance.selectedItem[0].item_text).toEqual('Bangalore');
expect(fixture.componentInstance.selectedItem[1].item_id).toEqual(3);
expect(fixture.componentInstance.selectedItem[1].item_text).toEqual('Pune');
}));
it('should not de-select disabled item-Pune', fakeAsync(() => {
const index = 3; // 3 is item-Pune
let selCheckBoxes: HTMLLIElement[];
const sel = fixture.nativeElement.querySelectorAll('.multiselect-item-checkbox');
selCheckBoxes = Array.from(sel);
selCheckBoxes[index].click();
tickAndDetectChanges(fixture);
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(2);
expect(fixture.componentInstance.selectedItem[0].item_id).toEqual(2);
expect(fixture.componentInstance.selectedItem[0].item_text).toEqual('Bangalore');
expect(fixture.componentInstance.selectedItem[1].item_id).toEqual(3);
expect(fixture.componentInstance.selectedItem[1].item_text).toEqual('Pune');
}));
it('should not de-select disabled items 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);
// Text should be Select All
expect(selCheckBoxes[0].querySelector('div').textContent).toContain('Select All');
selCheckBoxes[index].click(); // click on "select all"
tickAndDetectChanges(fixture);
expect(selCheckBoxes[1].querySelector('div').textContent).toContain('Mumbai');
expect(selCheckBoxes[1].querySelector('input').disabled).toBe(false);
expect(selCheckBoxes[2].querySelector('div').textContent).toContain('Bangalore');
expect(selCheckBoxes[2].querySelector('input').disabled).toBe(true);
expect(selCheckBoxes[3].querySelector('div').textContent).toContain('Pune');
expect(selCheckBoxes[3].querySelector('input').disabled).toBe(true);
expect(selCheckBoxes[4].querySelector('div').textContent).toContain('Navsari');
expect(selCheckBoxes[4].querySelector('input').disabled).toBe(false);
expect(fixture.componentInstance.selectedItem.length).toEqual(4);
// Text should be UnSelect All
expect(selCheckBoxes[0].querySelector('div').textContent).toContain('UnSelect All');
}));
it('should not de-select disabled items when selecting all items, after deselecting all values disabled values should stay as they were from the beginning', fakeAsync(() => {
const index = 0; // 0 is select all checkbox
let selCheckBoxes: HTMLLIElement[];
const sel = fixture.nativeElement.querySelectorAll('.multiselect-item-checkbox');
selCheckBoxes = Array.from(sel);
// Text should be Select All
expect(selCheckBoxes[0].querySelector('div').textContent).toContain('Select All');
selCheckBoxes[index].click(); // click on "select all"
tickAndDetectChanges(fixture);
// Text should be UnSelect All
expect(selCheckBoxes[0].querySelector('div').textContent).toContain('UnSelect All');
expect(selCheckBoxes[1].querySelector('div').textContent).toContain('Mumbai');
expect(selCheckBoxes[1].querySelector('input').disabled).toBe(false);
expect(selCheckBoxes[2].querySelector('div').textContent).toContain('Bangalore');
expect(selCheckBoxes[2].querySelector('input').disabled).toBe(true);
expect(selCheckBoxes[3].querySelector('div').textContent).toContain('Pune');
expect(selCheckBoxes[3].querySelector('input').disabled).toBe(true);
expect(selCheckBoxes[4].querySelector('div').textContent).toContain('Navsari');
expect(selCheckBoxes[4].querySelector('input').disabled).toBe(false);
expect(fixture.componentInstance.selectedItem.length).toEqual(4);
selCheckBoxes[index].click(); // click on "UnSelect All"
tickAndDetectChanges(fixture);
// Text should be Select All
expect(selCheckBoxes[0].querySelector('div').textContent).toContain('Select All');
expect(selCheckBoxes[1].querySelector('div').textContent).toContain('Mumbai');
expect(selCheckBoxes[1].querySelector('input').disabled).toBe(false);
expect(selCheckBoxes[2].querySelector('div').textContent).toContain('Bangalore');
expect(selCheckBoxes[2].querySelector('input').disabled).toBe(true);
expect(selCheckBoxes[3].querySelector('div').textContent).toContain('Pune');
expect(selCheckBoxes[3].querySelector('input').disabled).toBe(true);
expect(selCheckBoxes[4].querySelector('div').textContent).toContain('Navsari');
expect(selCheckBoxes[4].querySelector('input').disabled).toBe(false);
expect(fixture.componentInstance.selectedItem.length).toEqual(2); // Originally selected disabled values should still be selected after unselecting all
expect(fixture.componentInstance.selectedItem[0].item_id).toEqual(2);
expect(fixture.componentInstance.selectedItem[0].item_text).toEqual('Bangalore');
expect(fixture.componentInstance.selectedItem[1].item_id).toEqual(3);
expect(fixture.componentInstance.selectedItem[1].item_text).toEqual('Pune');
}));
});