@@ -7,8 +7,8 @@ import { debounceTime, distinctUntilChanged, switchMap, tap } from 'rxjs/operato
77import {
88 convertToBoolean ,
99 isTypeof ,
10- removeDuplicatedOptions ,
11- removeUndefinedAndNullOptions ,
10+ removeDuplicatedOptionsWithFieldValue ,
11+ removeUndefinedAndNullOptionsWithFieldValue ,
1212 sortOptionsByProperty
1313} from '../../../utils/util' ;
1414import { requiredFailed } from './../validators' ;
@@ -136,8 +136,8 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
136136 */
137137 @Output ( 'p-change' ) change : EventEmitter < any > = new EventEmitter < any > ( ) ;
138138
139- selectedOptions : Array < PoMultiselectOption > = [ ] ;
140- visibleOptionsDropdown : Array < PoMultiselectOption > = [ ] ;
139+ selectedOptions : Array < PoMultiselectOption | any > = [ ] ;
140+ visibleOptionsDropdown : Array < PoMultiselectOption | any > = [ ] ;
141141 visibleDisclaimers = [ ] ;
142142 isServerSearching = false ;
143143 isFirstFilter : boolean = true ;
@@ -158,7 +158,7 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
158158 private _filterMode ?: PoMultiselectFilterMode = PoMultiselectFilterMode . startsWith ;
159159 private _hideSearch ?: boolean = false ;
160160 private _literals : PoMultiselectLiterals ;
161- private _options : Array < PoMultiselectOption > ;
161+ private _options : Array < PoMultiselectOption | any > ;
162162 private _required ?: boolean = false ;
163163 private _sort ?: boolean = false ;
164164 private _autoHeight : boolean = false ;
@@ -359,7 +359,7 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
359359 /**
360360 * @description
361361 *
362- * Nesta propriedade deve ser definida uma lista de objetos que implementam a interface PoMultiselectOption .
362+ * Nesta propriedade deve ser definida uma lista de objetos que será exibida no multiselect .
363363 * Esta lista deve conter os valores e os labels que serão apresentados na tela.
364364 *
365365 * > Essa propriedade é imutável, ou seja, sempre que quiser atualizar a lista de opções disponíveis
@@ -372,14 +372,23 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
372372 * // evite, pois não atualiza a referência do objeto podendo gerar atrasos na atualização do template
373373 * this.options.push({ value: 'x', label: 'Nova opção' });
374374 * ```
375+ * > A lista pode ser definida utilizando um array com o valor representando `value` e `label` das seguintes formas:
376+ *
377+ * ```
378+ * <po-multiselect name="multiselect" p-label="PO Multiselect" [p-options]="[{value: 1, label: 'One'}, {value: 2, label: 'two'}]"> </po-multiselect>
379+ * ```
380+ *
381+ * ```
382+ * <po-multiselect name="multiselect" p-label="PO Multiselect" [p-options]="[{name: 'Roger', age: 28}, {name: 'Anne', age: 35}]" p-field-label="name" p-field-value="age"> </po-multiselect>
383+ * ```
384+ *
385+ * - Aconselha-se utilizar valores distintos no `label` e `value` dos itens.
375386 */
376- @Input ( 'p-options' ) set options ( options : Array < PoMultiselectOption > ) {
387+ @Input ( 'p-options' ) set options ( options : Array < PoMultiselectOption | any > ) {
377388 this . _options = options ;
378-
379- this . validAndSortOptions ( ) ;
380389 }
381390
382- get options ( ) {
391+ get options ( ) : Array < PoMultiselectOption | any > {
383392 return this . _options ;
384393 }
385394
@@ -445,7 +454,7 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
445454 * @default `label`
446455 */
447456 @Input ( 'p-field-label' ) set fieldLabel ( value : string ) {
448- this . _fieldLabel = value || PO_MULTISELECT_FIELD_LABEL_DEFAULT ;
457+ this . _fieldLabel = value ? value : PO_MULTISELECT_FIELD_LABEL_DEFAULT ;
449458
450459 if ( isTypeof ( this . filterService , 'string' ) && this . service ) {
451460 this . service . fieldLabel = this . _fieldLabel ;
@@ -469,7 +478,7 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
469478 * @default `value`
470479 */
471480 @Input ( 'p-field-value' ) set fieldValue ( value : string ) {
472- this . _fieldValue = value || PO_MULTISELECT_FIELD_VALUE_DEFAULT ;
481+ this . _fieldValue = value ? value : PO_MULTISELECT_FIELD_VALUE_DEFAULT ;
473482
474483 if ( isTypeof ( this . filterService , 'string' ) && this . service ) {
475484 this . service . fieldValue = this . _fieldValue ;
@@ -499,6 +508,7 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
499508 )
500509 . subscribe ( ) ;
501510
511+ this . validAndSortOptions ( ) ;
502512 this . updateList ( this . options ) ;
503513 }
504514
@@ -515,31 +525,31 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
515525
516526 validAndSortOptions ( ) {
517527 if ( this . options && this . options . length ) {
518- removeUndefinedAndNullOptions ( this . options ) ;
519- removeDuplicatedOptions ( this . options ) ;
528+ removeUndefinedAndNullOptionsWithFieldValue ( this . options , this . fieldValue ) ;
529+ removeDuplicatedOptionsWithFieldValue ( this . options , this . fieldValue ) ;
520530 this . setUndefinedLabels ( this . options ) ;
521531
522532 if ( this . sort ) {
523- sortOptionsByProperty ( this . options , 'label' ) ;
533+ sortOptionsByProperty ( this . options , this . fieldLabel ) ;
524534 }
525535 }
526536 }
527537
528538 setUndefinedLabels ( options ) {
529539 options . forEach ( option => {
530- if ( ! option [ 'label' ] ) {
531- option . label = option . value ;
540+ if ( ! option [ this . fieldLabel ] ) {
541+ option [ this . fieldLabel ] = option [ this . fieldValue ] ;
532542 }
533543 } ) ;
534544 }
535545
536- updateList ( options : Array < PoMultiselectOption > ) {
546+ updateList ( options : Array < PoMultiselectOption | any > ) {
537547 if ( options ) {
538548 this . visibleOptionsDropdown = options ;
539549 }
540550 }
541551
542- callOnChange ( selectedOptions : Array < PoMultiselectOption > ) {
552+ callOnChange ( selectedOptions : Array < PoMultiselectOption | any > ) {
543553 if ( this . onModelChange ) {
544554 this . onModelChange ( this . getValuesFromOptions ( selectedOptions ) ) ;
545555 this . eventChange ( selectedOptions ) ;
@@ -553,20 +563,20 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
553563 this . lastLengthModel = selectedOptions ? selectedOptions . length : null ;
554564 }
555565
556- getValuesFromOptions ( selectedOptions : Array < PoMultiselectOption > ) {
557- return selectedOptions && selectedOptions . length ? selectedOptions . map ( option => option . value ) : [ ] ;
566+ getValuesFromOptions ( selectedOptions : Array < PoMultiselectOption | any > ) {
567+ return selectedOptions && selectedOptions . length ? selectedOptions . map ( option => option [ this . fieldValue ] ) : [ ] ;
558568 }
559569
560570 getLabelByValue ( value ) {
561- const index = this . options . findIndex ( option => option . value === value ) ;
571+ const index = this . options . findIndex ( option => option [ this . fieldValue ] === value ) ;
562572 return this . options [ index ] . label ;
563573 }
564574
565- searchByLabel ( search : string , options : Array < PoMultiselectOption > , filterMode : PoMultiselectFilterMode ) {
575+ searchByLabel ( search : string , options : Array < PoMultiselectOption | any > , filterMode : PoMultiselectFilterMode ) {
566576 if ( search && options && options . length ) {
567- const newOptions : Array < PoMultiselectOption > = [ ] ;
577+ const newOptions : Array < PoMultiselectOption | any > = [ ] ;
568578 options . forEach ( option => {
569- if ( option . label && this . compareMethod ( search , option , filterMode ) ) {
579+ if ( option [ this . fieldLabel ] && this . compareMethod ( search , option , filterMode ) ) {
570580 newOptions . push ( option ) ;
571581 }
572582 } ) ;
@@ -588,15 +598,15 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
588598 }
589599
590600 startsWith ( search : string , option : PoMultiselectOption ) {
591- return option . label . toLowerCase ( ) . startsWith ( search . toLowerCase ( ) ) ;
601+ return option [ this . fieldLabel ] . toLowerCase ( ) . startsWith ( search . toLowerCase ( ) ) ;
592602 }
593603
594604 contains ( search : string , option : PoMultiselectOption ) {
595- return option . label . toLowerCase ( ) . indexOf ( search . toLowerCase ( ) ) > - 1 ;
605+ return option [ this . fieldLabel ] . toLowerCase ( ) . indexOf ( search . toLowerCase ( ) ) > - 1 ;
596606 }
597607
598608 endsWith ( search : string , option : PoMultiselectOption ) {
599- return option . label . toLowerCase ( ) . endsWith ( search . toLowerCase ( ) ) ;
609+ return option [ this . fieldLabel ] . toLowerCase ( ) . endsWith ( search . toLowerCase ( ) ) ;
600610 }
601611
602612 validate ( c : AbstractControl ) : { [ key : string ] : any } {
@@ -623,7 +633,7 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
623633 } else {
624634 newOptions . forEach ( newOption => {
625635 options . forEach ( option => {
626- if ( option . value === newOption . value ) {
636+ if ( option [ this . fieldValue ] === newOption [ this . fieldValue ] ) {
627637 this . selectedOptions . push ( option ) ;
628638 }
629639 } ) ;
@@ -643,7 +653,7 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
643653 } ) ;
644654 } else {
645655 // Validar se todos os items existem entre os options, senão atualizar o model
646- this . updateSelectedOptions ( values . map ( value => ( { value } ) ) ) ;
656+ this . updateSelectedOptions ( values . map ( value => ( { [ this . fieldValue ] : value } ) ) ) ;
647657
648658 if ( this . selectedOptions && this . selectedOptions . length < values . length ) {
649659 this . callOnChange ( this . selectedOptions ) ;
@@ -675,6 +685,6 @@ export abstract class PoMultiselectBaseComponent implements ControlValueAccessor
675685 }
676686 }
677687
678- abstract applyFilter ( value ?: string ) : Observable < Array < PoMultiselectOption > > ;
688+ abstract applyFilter ( value ?: string ) : Observable < Array < PoMultiselectOption | any > > ;
679689 abstract updateVisibleItems ( ) : void ;
680690}
0 commit comments