@@ -21,7 +21,7 @@ import {
2121 PipeTransform
2222} from '@angular/core' ;
2323import { 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
2626const MULTISELECT_VALUE_ACCESSOR : any = {
2727 provide : NG_VALUE_ACCESSOR ,
@@ -67,12 +67,12 @@ export interface IMultiSelectTexts {
6767export 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