@@ -15,6 +15,8 @@ describe('ui-select tests', function () {
15
15
Escape : 27
16
16
} ;
17
17
18
+ var defaultPlaceholder = 'Pick one...' ;
19
+
18
20
function isNil ( value ) {
19
21
return angular . isUndefined ( value ) || value === null ;
20
22
}
@@ -191,6 +193,10 @@ describe('ui-select tests', function () {
191
193
return $ ( el ) . find ( '.ui-select-match > span:first > span[ng-transclude]:not(.ng-hide)' ) . text ( ) ;
192
194
}
193
195
196
+ function getMatchPlaceholder ( el ) {
197
+ return el . find ( '.ui-select-search' ) . attr ( 'placeholder' )
198
+ }
199
+
194
200
function clickItem ( el , text ) {
195
201
196
202
if ( ! isDropdownOpened ( el ) ) {
@@ -740,6 +746,28 @@ describe('ui-select tests', function () {
740
746
el2 . remove ( ) ;
741
747
} ) ;
742
748
749
+ it ( 'should close an opened select clicking outside with stopPropagation()' , function ( ) {
750
+ var el1 = createUiSelect ( ) ;
751
+ var el2 = $ ( '<div></div>' ) ;
752
+ el1 . appendTo ( document . body ) ;
753
+ el2 . appendTo ( document . body ) ;
754
+
755
+ el2 . on ( 'click' , function ( e ) {
756
+ e . stopPropagation ( )
757
+ } ) ;
758
+
759
+ expect ( isDropdownOpened ( el1 ) ) . toEqual ( false ) ;
760
+ clickMatch ( el1 ) ;
761
+ expect ( isDropdownOpened ( el1 ) ) . toEqual ( true ) ;
762
+
763
+ // Using a native dom click() to make sure the test fails when it should.
764
+ el2 [ 0 ] . click ( ) ;
765
+
766
+ expect ( isDropdownOpened ( el1 ) ) . toEqual ( false ) ;
767
+ el1 . remove ( ) ;
768
+ el2 . remove ( ) ;
769
+ } ) ;
770
+
743
771
it ( 'should bind model correctly (with object as source)' , function ( ) {
744
772
var el = compileTemplate (
745
773
'<ui-select ng-model="selection.selected"> \
@@ -846,6 +874,20 @@ describe('ui-select tests', function () {
846
874
expect ( getMatchLabel ( el ) ) . toEqual ( '-- None Selected --' ) ;
847
875
} ) ;
848
876
877
+ it ( 'should not have active option when model cleared' , function ( ) {
878
+ var el = createUiSelect ( ) ;
879
+
880
+ clickItem ( el , 'Samantha' ) ;
881
+ expect ( el . scope ( ) . $select . activeIndex ) . toBe ( 5 ) ;
882
+
883
+ scope . selection . selected = null ;
884
+ scope . $digest ( ) ;
885
+
886
+ el . find ( ".ui-select-toggle" ) . click ( ) ;
887
+
888
+ expect ( el . scope ( ) . $select . activeIndex ) . toBe ( 0 ) ;
889
+ } ) ;
890
+
849
891
describe ( 'backspace reset option' , function ( ) {
850
892
it ( 'should undefined model when pressing BACKSPACE key if backspaceReset=true' , function ( ) {
851
893
var el = createUiSelect ( ) ;
@@ -1860,7 +1902,9 @@ describe('ui-select tests', function () {
1860
1902
function createUiSelectMultiple ( attrs ) {
1861
1903
var attrsHtml = '' ,
1862
1904
choicesAttrsHtml = '' ,
1863
- matchesAttrsHtml = '' ;
1905
+ matchesAttrsHtml = '' ,
1906
+ matchesPlaceholder = defaultPlaceholder ;
1907
+
1864
1908
if ( attrs !== undefined ) {
1865
1909
if ( attrs . disabled !== undefined ) { attrsHtml += ' ng-disabled="' + attrs . disabled + '"' ; }
1866
1910
if ( attrs . required !== undefined ) { attrsHtml += ' ng-required="' + attrs . required + '"' ; }
@@ -1870,24 +1914,26 @@ describe('ui-select tests', function () {
1870
1914
if ( attrs . taggingTokens !== undefined ) { attrsHtml += ' tagging-tokens="' + attrs . taggingTokens + '"' ; }
1871
1915
if ( attrs . taggingLabel !== undefined ) { attrsHtml += ' tagging-label="' + attrs . taggingLabel + '"' ; }
1872
1916
if ( attrs . inputId !== undefined ) { attrsHtml += ' input-id="' + attrs . inputId + '"' ; }
1873
- if ( attrs . groupBy !== undefined ) { choicesAttrsHtml += ' group-by="' + attrs . groupBy + '"' ; }
1874
- if ( attrs . uiDisableChoice !== undefined ) { choicesAttrsHtml += ' ui-disable-choice="' + attrs . uiDisableChoice + '"' ; }
1875
- if ( attrs . lockChoice !== undefined ) { matchesAttrsHtml += ' ui-lock-choice="' + attrs . lockChoice + '"' ; }
1876
1917
if ( attrs . removeSelected !== undefined ) { attrsHtml += ' remove-selected="' + attrs . removeSelected + '"' ; }
1877
1918
if ( attrs . resetSearchInput !== undefined ) { attrsHtml += ' reset-search-input="' + attrs . resetSearchInput + '"' ; }
1878
1919
if ( attrs . limit !== undefined ) { attrsHtml += ' limit="' + attrs . limit + '"' ; }
1879
1920
if ( attrs . onSelect !== undefined ) { attrsHtml += ' on-select="' + attrs . onSelect + '"' ; }
1880
1921
if ( attrs . removeSelected !== undefined ) { attrsHtml += ' remove-selected="' + attrs . removeSelected + '"' ; }
1881
- if ( attrs . refresh !== undefined ) { choicesAttrsHtml += ' refresh="' + attrs . refresh + '"' ; }
1882
- if ( attrs . refreshDelay !== undefined ) { choicesAttrsHtml += ' refresh-delay="' + attrs . refreshDelay + '"' ; }
1883
1922
if ( attrs . spinnerEnabled !== undefined ) { attrsHtml += ' spinner-enabled="' + attrs . spinnerEnabled + '"' ; }
1884
1923
if ( attrs . spinnerClass !== undefined ) { attrsHtml += ' spinner-class="' + attrs . spinnerClass + '"' ; }
1924
+ if ( attrs . groupBy !== undefined ) { choicesAttrsHtml += ' group-by="' + attrs . groupBy + '"' ; }
1925
+ if ( attrs . uiDisableChoice !== undefined ) { choicesAttrsHtml += ' ui-disable-choice="' + attrs . uiDisableChoice + '"' ; }
1926
+ if ( attrs . refresh !== undefined ) { choicesAttrsHtml += ' refresh="' + attrs . refresh + '"' ; }
1927
+ if ( attrs . refreshDelay !== undefined ) { choicesAttrsHtml += ' refresh-delay="' + attrs . refreshDelay + '"' ; }
1928
+ if ( attrs . lockChoice !== undefined ) { matchesAttrsHtml += ' ui-lock-choice="' + attrs . lockChoice + '"' ; }
1885
1929
if ( attrs . uiSelectHeaderGroupSelectable !== undefined ) { choicesAttrsHtml += ' ui-select-header-group-selectable' ; }
1886
1930
}
1887
1931
1932
+ matchesAttrsHtml += ' placeholder="' + matchesPlaceholder + '"' ;
1933
+
1888
1934
return compileTemplate (
1889
1935
'<ui-select multiple ng-model="selection.selectedMultiple"' + attrsHtml + ' theme="bootstrap" style="width: 800px;"> \
1890
- <ui-select-match " ' + matchesAttrsHtml + ' placeholder="Pick one..." >{{$item.name}} <{{$item.email}}></ui-select-match> \
1936
+ <ui-select-match ' + matchesAttrsHtml + '>{{$item.name}} <{{$item.email}}></ui-select-match> \
1891
1937
<ui-select-choices repeat="person in people | filter: $select.search"' + choicesAttrsHtml + '> \
1892
1938
<div ng-bind-html="person.name | highlight: $select.search"></div> \
1893
1939
<div ng-bind-html="person.email | highlight: $select.search"></div> \
@@ -2958,6 +3004,65 @@ describe('ui-select tests', function () {
2958
3004
triggerKeydown ( searchInput , Key . Enter ) ;
2959
3005
expect ( el . scope ( ) . $select . activeIndex ) . toBe ( 2 ) ;
2960
3006
} ) ;
3007
+
3008
+ it ( 'should not display the placeholder when tag is selected (by default)' , function ( ) {
3009
+ var placeholderText = defaultPlaceholder ;
3010
+
3011
+ var el = createUiSelectMultiple ( {
3012
+ tagging : '' ,
3013
+ taggingLabel : ''
3014
+ } ) ;
3015
+
3016
+ var $select = el . scope ( ) . $select ; // uiSelectCtrl
3017
+
3018
+ expect ( $select . selected ) . toEqual ( [ ] ) ;
3019
+ expect ( $select . getPlaceholder ( ) ) . toEqual ( placeholderText ) ;
3020
+ expect ( getMatchPlaceholder ( el ) ) . toEqual ( placeholderText ) ; // get placeholder
3021
+
3022
+ clickItem ( el , scope . people [ 0 ] . name ) ;
3023
+ expect ( $select . selected ) . toEqual ( [ scope . people [ 0 ] ] ) ;
3024
+ expect ( getMatchLabel ( el ) ) . toEqual ( '' ) ; // empty text
3025
+ expect ( getMatchPlaceholder ( el ) ) . toEqual ( '' ) ; // empty placeholder
3026
+
3027
+ clickItem ( el , scope . people [ 1 ] . name ) ;
3028
+ expect ( $select . selected ) . toEqual ( [ scope . people [ 0 ] , scope . people [ 1 ] ] ) ;
3029
+ expect ( getMatchLabel ( el ) ) . toEqual ( '' ) ;
3030
+ expect ( getMatchPlaceholder ( el ) ) . toEqual ( '' ) ;
3031
+ } ) ;
3032
+
3033
+ // Could be needed when e.g. tag is shown below the input
3034
+ it ( 'should display the placeholder when tag is selected (if user overrides .getPlaceholder())' , function ( ) {
3035
+ var placeholderText = defaultPlaceholder ;
3036
+
3037
+ var el = createUiSelectMultiple ( {
3038
+ tagging : '' ,
3039
+ taggingLabel : ''
3040
+ } ) ;
3041
+ var $select = el . scope ( ) . $select ;
3042
+
3043
+ /**
3044
+ * In case user wants to show placeholder when the text is empty - they can override $select.getPlaceholder.
3045
+ * Cannot do this with $selectMultiple, bc <ui-select-multiple is appended inside the library
3046
+ * This override closes #1796
3047
+ */
3048
+ $select . getPlaceholder = function ( ) {
3049
+ return $select . placeholder ;
3050
+ } ;
3051
+
3052
+ expect ( $select . selected ) . toEqual ( [ ] ) ;
3053
+ expect ( getMatchLabel ( el ) ) . toEqual ( '' ) ;
3054
+ expect ( getMatchPlaceholder ( el ) ) . toEqual ( placeholderText ) ;
3055
+
3056
+ clickItem ( el , scope . people [ 0 ] . name ) ;
3057
+ expect ( $select . selected ) . toEqual ( [ scope . people [ 0 ] ] ) ;
3058
+ expect ( getMatchLabel ( el ) ) . toEqual ( '' ) ; // empty text
3059
+ expect ( getMatchPlaceholder ( el ) ) . toEqual ( placeholderText ) ; // show placeholder
3060
+
3061
+ clickItem ( el , scope . people [ 1 ] . name ) ;
3062
+ expect ( $select . selected ) . toEqual ( [ scope . people [ 0 ] , scope . people [ 1 ] ] ) ;
3063
+ expect ( getMatchLabel ( el ) ) . toEqual ( '' ) ;
3064
+ expect ( getMatchPlaceholder ( el ) ) . toEqual ( placeholderText ) ;
3065
+ } ) ;
2961
3066
} ) ;
2962
3067
2963
3068
describe ( 'resetSearchInput option multiple' , function ( ) {
0 commit comments