Skip to content

Commit fd78ed3

Browse files
committed
Adding validation support.
fixes #43 #83
1 parent 24cd5fe commit fd78ed3

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-2-dropdown-multiselect",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Customizable dropdown multiselect in Angular 2 with bootstrap css.",
55
"main": "src/multiselect-dropdown.ts",
66
"repository": {

src/multiselect-dropdown.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
PipeTransform
2222
} from '@angular/core';
2323
import { CommonModule } from '@angular/common';
24-
import { FormsModule, NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
24+
import { FormsModule, NG_VALUE_ACCESSOR, ControlValueAccessor, Validator, AbstractControl } from '@angular/forms';
2525

2626
const MULTISELECT_VALUE_ACCESSOR: any = {
2727
provide: NG_VALUE_ACCESSOR,
@@ -67,12 +67,12 @@ export interface IMultiSelectTexts {
6767
export class MultiSelectSearchFilter implements PipeTransform {
6868
transform(options: Array<IMultiSelectOption>, args: string): Array<IMultiSelectOption> {
6969
const matchPredicate = (option: IMultiSelectOption) => option.name.toLowerCase().indexOf((args || '').toLowerCase()) > -1,
70-
getChildren = (option: IMultiSelectOption) => options.filter(child => child.parentId === option.id),
71-
getParent = (option: IMultiSelectOption) => options.find(parent => option.parentId === parent.id);
70+
getChildren = (option: IMultiSelectOption) => options.filter(child => child.parentId === option.id),
71+
getParent = (option: IMultiSelectOption) => options.find(parent => option.parentId === parent.id);
7272
return options.filter((option: IMultiSelectOption) => {
73-
return matchPredicate(option) ||
74-
(typeof(option.parentId) === 'undefined' && getChildren(option).some(matchPredicate)) ||
75-
(typeof(option.parentId) !== 'undefined' && matchPredicate(getParent(option)));
73+
return matchPredicate(option) ||
74+
(typeof (option.parentId) === 'undefined' && getChildren(option).some(matchPredicate)) ||
75+
(typeof (option.parentId) !== 'undefined' && matchPredicate(getParent(option)));
7676
});
7777
}
7878
}
@@ -132,8 +132,7 @@ export class MultiSelectSearchFilter implements PipeTransform {
132132
</div>
133133
`
134134
})
135-
export class MultiselectDropdown implements OnInit, DoCheck, ControlValueAccessor {
136-
135+
export class MultiselectDropdown implements OnInit, DoCheck, ControlValueAccessor, Validator {
137136
@Input() options: Array<IMultiSelectOption>;
138137
@Input() settings: IMultiSelectSettings;
139138
@Input() texts: IMultiSelectTexts;
@@ -222,6 +221,18 @@ export class MultiselectDropdown implements OnInit, DoCheck, ControlValueAccesso
222221
}
223222
}
224223

224+
validate(c: AbstractControl): { [key: string]: any; } {
225+
return (this.model && this.model.length) ? null : {
226+
required: {
227+
valid: false,
228+
},
229+
};
230+
}
231+
232+
registerOnValidatorChange(fn: () => void): void {
233+
throw new Error('Method not implemented.');
234+
}
235+
225236
clearSearch(event: Event) {
226237
event.stopPropagation();
227238
this.searchFilterText = '';
@@ -266,6 +277,7 @@ export class MultiselectDropdown implements OnInit, DoCheck, ControlValueAccesso
266277
this.toggleDropdown();
267278
}
268279
this.onModelChange(this.model);
280+
this.onModelTouched();
269281
}
270282

271283
updateNumSelected() {
@@ -300,12 +312,14 @@ export class MultiselectDropdown implements OnInit, DoCheck, ControlValueAccesso
300312
return option.id;
301313
});
302314
this.onModelChange(this.model);
315+
this.onModelTouched();
303316
}
304317

305318
uncheckAll() {
306319
this.model.forEach((id: number) => this.onRemoved.emit(id));
307320
this.model = [];
308321
this.onModelChange(this.model);
322+
this.onModelTouched();
309323
}
310324

311325
preventCheckboxCheck(event: Event, option: IMultiSelectOption) {
@@ -323,4 +337,4 @@ export class MultiselectDropdown implements OnInit, DoCheck, ControlValueAccesso
323337
exports: [MultiselectDropdown],
324338
declarations: [MultiselectDropdown, MultiSelectSearchFilter],
325339
})
326-
export class MultiselectDropdownModule {}
340+
export class MultiselectDropdownModule { }

0 commit comments

Comments
 (0)