2
2
* @author zhixin wen <[email protected] >
3
3
*/
4
4
import Constants from './constants' ;
5
- import { compareObjects , deepCopy , findByParam , removeDiacritics , removeUndefined , setDataKeys } from './utils' ;
5
+ import { compareObjects , deepCopy , findByParam , removeDiacritics , removeUndefined , setDataKeys , stripScripts } from './utils' ;
6
6
import {
7
7
calculateAvailableSpace ,
8
8
createDomElement ,
@@ -53,7 +53,7 @@ export class MultipleSelectInstance {
53
53
protected elm : HTMLSelectElement ,
54
54
options ?: Partial < Omit < MultipleSelectOption , 'onHardDestroy' | 'onAfterHardDestroy' > >
55
55
) {
56
- this . options = Object . assign ( { } , Constants . DEFAULTS , this . elm . dataset , options ) ;
56
+ this . options = Object . assign ( { } , Constants . DEFAULTS , this . elm . dataset , options ) as MultipleSelectOption ;
57
57
this . _bindEventService = new BindingEventService ( { distinctEvent : true } ) ;
58
58
}
59
59
@@ -534,7 +534,7 @@ export class MultipleSelectInstance {
534
534
<li class="${ multiple } ${ classes } " ${ title } ${ style } >
535
535
<label class="${ row . disabled ? 'disabled' : '' } ">
536
536
<input type="${ type } "
537
- value="${ row . value } "
537
+ value="${ encodeURI ( row . value ) } "
538
538
data-key="${ row . _key } "
539
539
${ this . selectItemName }
540
540
${ row . selected ? ' checked="checked"' : '' }
@@ -879,32 +879,51 @@ export class MultipleSelectInstance {
879
879
textSelects = valueSelects ;
880
880
}
881
881
882
- const spanElm = this . choiceElm ?. querySelector ( 'span' ) as HTMLSpanElement ;
882
+ const spanElm = this . choiceElm ?. querySelector < HTMLSpanElement > ( 'span' ) ;
883
883
const sl = valueSelects . length ;
884
884
let html = '' ;
885
885
886
- if ( sl === 0 ) {
887
- spanElm . classList . add ( 'ms-placeholder' ) ;
888
- spanElm . innerHTML = this . options . placeholder || '' ;
889
- } else if ( sl < this . options . minimumCountSelected ) {
890
- html = textSelects . join ( this . options . displayDelimiter ) ;
891
- } else if ( this . options . formatAllSelected ( ) && sl === this . dataTotal ) {
892
- html = this . options . formatAllSelected ( ) ;
893
- } else if ( this . options . ellipsis && sl > this . options . minimumCountSelected ) {
894
- html = `${ textSelects . slice ( 0 , this . options . minimumCountSelected ) . join ( this . options . displayDelimiter ) } ...` ;
895
- } else if ( this . options . formatCountSelected ( sl , this . dataTotal ) && sl > this . options . minimumCountSelected ) {
896
- html = this . options . formatCountSelected ( sl , this . dataTotal ) ;
897
- } else {
898
- html = textSelects . join ( this . options . displayDelimiter ) ;
899
- }
886
+ const getSelectOptionHtml = ( ) => {
887
+ if ( this . options . useSelectOptionLabel || this . options . useSelectOptionLabelToHtml ) {
888
+ const labels = valueSelects . join ( this . options . delimiter ) ;
889
+ return this . options . useSelectOptionLabelToHtml ? stripScripts ( labels ) : labels ;
890
+ } else {
891
+ return textSelects . join ( this . options . displayDelimiter ) ;
892
+ }
893
+ } ;
900
894
901
- if ( html ) {
902
- spanElm ?. classList . remove ( 'ms-placeholder' ) ;
903
- spanElm . innerHTML = html ;
904
- }
895
+ if ( spanElm ) {
896
+ if ( sl === 0 ) {
897
+ spanElm . classList . add ( 'ms-placeholder' ) ;
898
+ spanElm . innerHTML = this . options . placeholder || '' ;
899
+ } else if ( sl < this . options . minimumCountSelected ) {
900
+ html = getSelectOptionHtml ( ) ;
901
+ } else if ( this . options . formatAllSelected ( ) && sl === this . dataTotal ) {
902
+ html = this . options . formatAllSelected ( ) ;
903
+ } else if ( this . options . ellipsis && sl > this . options . minimumCountSelected ) {
904
+ html = `${ textSelects . slice ( 0 , this . options . minimumCountSelected ) . join ( this . options . displayDelimiter ) } ...` ;
905
+ } else if ( this . options . formatCountSelected ( sl , this . dataTotal ) && sl > this . options . minimumCountSelected ) {
906
+ html = this . options . formatCountSelected ( sl , this . dataTotal ) ;
907
+ } else {
908
+ html = getSelectOptionHtml ( ) ;
909
+ }
905
910
906
- if ( this . options . displayTitle ) {
907
- spanElm . title = this . getSelects ( 'text' ) . join ( '' ) ;
911
+ if ( html ) {
912
+ spanElm ?. classList . remove ( 'ms-placeholder' ) ;
913
+ if ( this . options . useSelectOptionLabelToHtml ) {
914
+ spanElm . innerHTML = html ;
915
+ } else {
916
+ spanElm . textContent = html ;
917
+ }
918
+ }
919
+
920
+ if ( this . options . displayTitle || this . options . addTitle ) {
921
+ if ( this . options . addTitle ) {
922
+ console . warn ( '[Multiple-Select-Vanilla] Please note that the `addTitle` option was replaced with `displayTitle`.' ) ;
923
+ }
924
+ const selectType = this . options . useSelectOptionLabel || this . options . useSelectOptionLabelToHtml ? 'value' : 'text' ;
925
+ spanElm . title = this . getSelects ( selectType ) . join ( '' ) ;
926
+ }
908
927
}
909
928
910
929
// set selects to select
@@ -1303,10 +1322,16 @@ export class MultipleSelectInstance {
1303
1322
}
1304
1323
1305
1324
// calculate the "Select All" element width, this text is configurable which is why we recalculate every time
1306
- const selectAllElm = this . dropElm . querySelector ( '.ms-select-all span' ) as HTMLSpanElement ;
1325
+ const selectAllSpanElm = this . dropElm . querySelector ( '.ms-select-all span' ) as HTMLSpanElement ;
1307
1326
const dropUlElm = this . dropElm . querySelector ( 'ul' ) as HTMLUListElement ;
1308
1327
1309
- const selectAllElmWidth = selectAllElm . clientWidth + this . options . selectSidePadding ;
1328
+ let liPadding = 0 ;
1329
+ const firstLiElm = this . dropElm . querySelector ( 'li' ) ; // get padding of 1st <li> element
1330
+ if ( firstLiElm ) {
1331
+ const { paddingLeft, paddingRight } = window . getComputedStyle ( firstLiElm ) ;
1332
+ liPadding = parseFloat ( paddingLeft ) + parseFloat ( paddingRight ) ;
1333
+ }
1334
+ const selectAllElmWidth = selectAllSpanElm . clientWidth + liPadding ;
1310
1335
const hasScrollbar = dropUlElm . scrollHeight > dropUlElm . clientHeight ;
1311
1336
const scrollbarWidth = hasScrollbar ? this . getScrollbarWidth ( ) : 0 ;
1312
1337
let contentWidth = 0 ;
@@ -1318,7 +1343,7 @@ export class MultipleSelectInstance {
1318
1343
} ) ;
1319
1344
1320
1345
// add a padding & include the browser scrollbar width
1321
- contentWidth += this . options . selectSidePadding + scrollbarWidth ;
1346
+ contentWidth += liPadding + scrollbarWidth ;
1322
1347
1323
1348
// make sure the new calculated width is wide enough to include the "Select All" text (this text is configurable)
1324
1349
if ( contentWidth < selectAllElmWidth ) {
0 commit comments