Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

fix($selectMultiple): allow user to override getPlaceholder() Behavior #2015

Merged
merged 3 commits into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bootstrap/select-multiple.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
autocapitalize="off"
spellcheck="false"
class="ui-select-search input-xs"
placeholder="{{$selectMultiple.getPlaceholder()}}"
placeholder="{{$select.getPlaceholder()}}"
ng-disabled="$select.disabled"
ng-click="$select.activate()"
ng-model="$select.search"
Expand Down
2 changes: 1 addition & 1 deletion src/select2/select-multiple.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
aria-label="{{ $select.baseTitle }}"
aria-activedescendant="ui-select-choices-row-{{ $select.generatedId }}-{{ $select.activeIndex }}"
class="select2-input ui-select-search"
placeholder="{{$selectMultiple.getPlaceholder()}}"
placeholder="{{$select.getPlaceholder()}}"
ng-disabled="$select.disabled"
ng-hide="$select.disabled"
ng-model="$select.search"
Expand Down
4 changes: 2 additions & 2 deletions src/selectize/select-multiple.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<input type="search" autocomplete="off" tabindex="-1"
class="ui-select-search"
ng-class="{'ui-select-search-hidden':!$select.searchEnabled}"
placeholder="{{$selectMultiple.getPlaceholder()}}"
placeholder="{{$select.getPlaceholder()}}"
ng-model="$select.search"
ng-disabled="$select.disabled"
aria-expanded="{{$select.open}}"
Expand All @@ -15,4 +15,4 @@
</div>
<div class="ui-select-choices"></div>
<div class="ui-select-no-choice"></div>
</div>
</div>
5 changes: 5 additions & 0 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ uis.controller('uiSelectCtrl',
return isNil(ctrl.selected) || ctrl.selected === '' || (ctrl.multiple && ctrl.selected.length === 0);
};

ctrl.getPlaceholder = function(){
if(ctrl.selected && ctrl.selected.length) return;
return ctrl.placeholder;
};

function _findIndex(collection, predicate, thisArg){
if (collection.findIndex){
return collection.findIndex(predicate, thisArg);
Expand Down
6 changes: 0 additions & 6 deletions src/uiSelectMultipleDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
return true;
};

ctrl.getPlaceholder = function(){
//Refactor single?
if($select.selected && $select.selected.length) return;
return $select.placeholder;
};


}],
controllerAs: '$selectMultiple',
Expand Down
85 changes: 78 additions & 7 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('ui-select tests', function () {
Escape: 27
};

var defaultPlaceholder = 'Pick one...';

function isNil(value) {
return angular.isUndefined(value) || value === null;
}
Expand Down Expand Up @@ -191,6 +193,10 @@ describe('ui-select tests', function () {
return $(el).find('.ui-select-match > span:first > span[ng-transclude]:not(.ng-hide)').text();
}

function getMatchPlaceholder(el) {
return el.find('.ui-select-search').attr('placeholder')
}

function clickItem(el, text) {

if (!isDropdownOpened(el)) {
Expand Down Expand Up @@ -1860,7 +1866,9 @@ describe('ui-select tests', function () {
function createUiSelectMultiple(attrs) {
var attrsHtml = '',
choicesAttrsHtml = '',
matchesAttrsHtml = '';
matchesAttrsHtml = '',
matchesPlaceholder = defaultPlaceholder;

if (attrs !== undefined) {
if (attrs.disabled !== undefined) { attrsHtml += ' ng-disabled="' + attrs.disabled + '"'; }
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
Expand All @@ -1870,23 +1878,27 @@ describe('ui-select tests', function () {
if (attrs.taggingTokens !== undefined) { attrsHtml += ' tagging-tokens="' + attrs.taggingTokens + '"'; }
if (attrs.taggingLabel !== undefined) { attrsHtml += ' tagging-label="' + attrs.taggingLabel + '"'; }
if (attrs.inputId !== undefined) { attrsHtml += ' input-id="' + attrs.inputId + '"'; }
if (attrs.groupBy !== undefined) { choicesAttrsHtml += ' group-by="' + attrs.groupBy + '"'; }
if (attrs.uiDisableChoice !== undefined) { choicesAttrsHtml += ' ui-disable-choice="' + attrs.uiDisableChoice + '"'; }
if (attrs.lockChoice !== undefined) { matchesAttrsHtml += ' ui-lock-choice="' + attrs.lockChoice + '"'; }
if (attrs.removeSelected !== undefined) { attrsHtml += ' remove-selected="' + attrs.removeSelected + '"'; }
if (attrs.resetSearchInput !== undefined) { attrsHtml += ' reset-search-input="' + attrs.resetSearchInput + '"'; }
if (attrs.limit !== undefined) { attrsHtml += ' limit="' + attrs.limit + '"'; }
if (attrs.onSelect !== undefined) { attrsHtml += ' on-select="' + attrs.onSelect + '"'; }
if (attrs.removeSelected !== undefined) { attrsHtml += ' remove-selected="' + attrs.removeSelected + '"'; }
if (attrs.refresh !== undefined) { choicesAttrsHtml += ' refresh="' + attrs.refresh + '"'; }
if (attrs.refreshDelay !== undefined) { choicesAttrsHtml += ' refresh-delay="' + attrs.refreshDelay + '"'; }
if (attrs.spinnerEnabled !== undefined) { attrsHtml += ' spinner-enabled="' + attrs.spinnerEnabled + '"'; }
if (attrs.spinnerClass !== undefined) { attrsHtml += ' spinner-class="' + attrs.spinnerClass + '"'; }

if (attrs.groupBy !== undefined) { choicesAttrsHtml += ' group-by="' + attrs.groupBy + '"'; }
if (attrs.uiDisableChoice !== undefined) { choicesAttrsHtml += ' ui-disable-choice="' + attrs.uiDisableChoice + '"'; }
if (attrs.refresh !== undefined) { choicesAttrsHtml += ' refresh="' + attrs.refresh + '"'; }
if (attrs.refreshDelay !== undefined) { choicesAttrsHtml += ' refresh-delay="' + attrs.refreshDelay + '"'; }

if (attrs.lockChoice !== undefined) { matchesAttrsHtml += ' ui-lock-choice="' + attrs.lockChoice + '"'; }
}

matchesAttrsHtml += ' placeholder="' + matchesPlaceholder + '"';

return compileTemplate(
'<ui-select multiple ng-model="selection.selectedMultiple"' + attrsHtml + ' theme="bootstrap" style="width: 800px;"> \
<ui-select-match "' + matchesAttrsHtml + ' placeholder="Pick one...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
<ui-select-match ' + matchesAttrsHtml + '>{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
<ui-select-choices repeat="person in people | filter: $select.search"' + choicesAttrsHtml + '> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
Expand Down Expand Up @@ -2957,6 +2969,65 @@ describe('ui-select tests', function () {
triggerKeydown(searchInput, Key.Enter);
expect(el.scope().$select.activeIndex).toBe(2);
});

it('should not display the placeholder when tag is selected (by default)', function () {
var placeholderText = defaultPlaceholder;

var el = createUiSelectMultiple({
tagging: '',
taggingLabel: ''
});

var $select = el.scope().$select; // uiSelectCtrl

expect($select.selected).toEqual([]);
expect($select.getPlaceholder()).toEqual(placeholderText);
expect(getMatchPlaceholder(el)).toEqual(placeholderText); // get placeholder

clickItem(el, scope.people[0].name);
expect($select.selected).toEqual([scope.people[0]]);
expect(getMatchLabel(el)).toEqual(''); // empty text
expect(getMatchPlaceholder(el)).toEqual(''); // empty placeholder

clickItem(el, scope.people[1].name);
expect($select.selected).toEqual([scope.people[0], scope.people[1]]);
expect(getMatchLabel(el)).toEqual('');
expect(getMatchPlaceholder(el)).toEqual('');
});

// Could be needed when e.g. tag is shown below the input
it('should display the placeholder when tag is selected (if user overrides .getPlaceholder())', function () {
var placeholderText = defaultPlaceholder;

var el = createUiSelectMultiple({
tagging: '',
taggingLabel: ''
});
var $select = el.scope().$select;

/**
* In case user wants to show placeholder when the text is empty - they can override $select.getPlaceholder.
* Cannot do this with $selectMultiple, bc <ui-select-multiple is appended inside the library
* This override closes #1796
*/
$select.getPlaceholder = function() {
return $select.placeholder;
};

expect($select.selected).toEqual([]);
expect(getMatchLabel(el)).toEqual('');
expect(getMatchPlaceholder(el)).toEqual(placeholderText);

clickItem(el, scope.people[0].name);
expect($select.selected).toEqual([scope.people[0]]);
expect(getMatchLabel(el)).toEqual(''); // empty text
expect(getMatchPlaceholder(el)).toEqual(placeholderText); // show placeholder

clickItem(el, scope.people[1].name);
expect($select.selected).toEqual([scope.people[0], scope.people[1]]);
expect(getMatchLabel(el)).toEqual('');
expect(getMatchPlaceholder(el)).toEqual(placeholderText);
});
});

describe('resetSearchInput option multiple', function () {
Expand Down