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

fix(uiSelect) support data-multiple attribute #1519

Merged
merged 1 commit into from
Mar 28, 2016
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
19 changes: 16 additions & 3 deletions src/uiSelectMatchDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ uis.directive('uiSelectMatch', ['uiSelectConfig', function(uiSelectConfig) {
// Needed so the uiSelect can detect the transcluded content
tElement.addClass('ui-select-match');

var parent = tElement.parent();
// Gets theme attribute from parent (ui-select)
var theme = tElement.parent().attr('theme') || uiSelectConfig.theme;
var multi = tElement.parent().attr('multiple');
return theme + (multi ? '/match-multiple.tpl.html' : '/match.tpl.html');
var theme = getAttribute(parent, 'theme') || uiSelectConfig.theme;
var multi = angular.isDefined(getAttribute(parent, 'multiple'));

return theme + (multi ? '/match-multiple.tpl.html' : '/match.tpl.html');
},
link: function(scope, element, attrs, $select) {
$select.lockChoiceExpression = attrs.uiLockChoice;
Expand All @@ -32,4 +34,15 @@ uis.directive('uiSelectMatch', ['uiSelectConfig', function(uiSelectConfig) {

}
};

function getAttribute(elem, attribute) {
if (elem[0].hasAttribute(attribute))
return elem.attr(attribute);

if (elem[0].hasAttribute('data-' + attribute))
return elem.attr('data-' + attribute);

if (elem[0].hasAttribute('x-' + attribute))
return elem.attr('x-' + attribute);
}
}]);
38 changes: 38 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,44 @@ describe('ui-select tests', function() {
expect(el.find('.ui-select-match-item').length).toBe(0);
});

it('should render intial state with data-multiple attribute', function () {
// ensure match template has been loaded by having more than one selection
scope.selection.selectedMultiple = [scope.people[0], scope.people[1]];

var el = compileTemplate(
'<ui-select data-multiple ng-model="selection.selectedMultiple" theme="bootstrap" style="width: 800px;"> \
<ui-select-match placeholder="Pick one...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
<ui-select-choices repeat="person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);

expect(el).toHaveClass('ui-select-multiple');
expect(el.scope().$select.selected.length).toBe(2);
expect(el.find('.ui-select-match-item').length).toBe(2);
});

it('should render intial state with x-multiple attribute', function () {
// ensure match template has been loaded by having more than one selection
scope.selection.selectedMultiple = [scope.people[0], scope.people[1]];

var el = compileTemplate(
'<ui-select x-multiple ng-model="selection.selectedMultiple" theme="bootstrap" style="width: 800px;"> \
<ui-select-match placeholder="Pick one...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
<ui-select-choices repeat="person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);

expect(el).toHaveClass('ui-select-multiple');
expect(el.scope().$select.selected.length).toBe(2);
expect(el.find('.ui-select-match-item').length).toBe(2);
});

it('should set model as an empty array if ngModel isnt defined after an item is selected', function () {

// scope.selection.selectedMultiple = [];
Expand Down