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

feat(typeahead): Custom CSS class for typeahead's dropdown menu #4410

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
16 changes: 16 additions & 0 deletions src/typeahead/docs/demo.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<style>
.dropdown-menu.demo-class {
border-radius: 0;
font-style: italic;
box-shadow: 5px 5px 0 rgba(0, 0, 0, .12);
}
.dropdown-menu.demo-class > .active > a,
.dropdown-menu.demo-class > .active > a:hover,
.dropdown-menu.demo-class > .active > a:focus {
background-color: rgba(247, 150, 4, .69);
}
</style>
<script type="text/ng-template" id="customTemplate.html">
<a>
<img ng-src="http://upload.wikimedia.org/wikipedia/commons/thumb/{{match.model.flag}}" width="16">
Expand All @@ -21,4 +33,8 @@ <h4>Asynchronous results</h4>
<h4>Custom templates for results</h4>
<pre>Model: {{customSelected | json}}</pre>
<input type="text" ng-model="customSelected" placeholder="Custom template" typeahead="state as state.name for state in statesWithFlags | filter:{name:$viewValue}" typeahead-template-url="customTemplate.html" class="form-control">

<h4>Custom class for append-to-body popup</h4>
<pre>Model: {{customSelected | json}}</pre>
<input type="text" ng-model="customSelected" typeahead-append-to-body="true" typeahead-dropdown-custom-class="demo-class" placeholder="Custom class" typeahead="state as state.name for state in statesWithFlags | filter:{name:$viewValue}" class="form-control">
</div>
6 changes: 5 additions & 1 deletion src/typeahead/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,9 @@ The typeahead directives provide several attributes:
On blur, select the currently highlighted match

* `typeahead-focus-on-select`
_(Defaults: true) :
_(Defaults: true)_ :
On selection, focus the input element the typeahead directive is associated with

* `typeahead-dropdown-custom-class`
_(Defaults: undefined)_ :
Add custom class to popup
19 changes: 19 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,19 @@ describe('typeahead tests', function() {
findMatches(element).eq(1).trigger('mouseenter');
expect(element).toBeOpenWithActive(2, 1);
});

it('should add a custom class to popup', function() {
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-dropdown-custom-class="demo-class"></div>');
changeInputValueTo(element, 'b');
expect(findDropDown(element).hasClass('demo-class')).toBeTruthy();
});

it('should add a custom class to popup with custom template', function() {
$templateCache.put('custom.html', '<div>foo</div>');
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-dropdown-custom-class="demo-class" typeahead-template-url="custom.html"></div>');
changeInputValueTo(element, 'b');
expect(findDropDown(element).hasClass('demo-class dropdown-menu')).toBeTruthy();
});
});

describe('promises', function() {
Expand Down Expand Up @@ -895,6 +908,12 @@ describe('typeahead tests', function() {
$timeout.flush();
expect(dropdown.css('top')).toEqual('500px');
});

it('should add a custom class to append-to-body popup', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test begs the question, if the user uses typeahead-append-to-body, should the component remove this class from the body tag when the element is destroyed? I think it should here.

var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-append-to-body="true" typeahead-dropdown-custom-class="demo-class"></div>');
changeInputValueTo(element, 'b');
expect(findDropDown($document.find('body')).hasClass('demo-class')).toBeTruthy();
});
});

describe('focus first', function() {
Expand Down
5 changes: 5 additions & 0 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
popUpEl.attr('popup-template-url', attrs.typeaheadPopupTemplateUrl);
}

// Add a custom class to Dropdown Menu element
if (angular.isDefined(attrs.typeaheadDropdownCustomClass)) {
popUpEl.addClass(attrs.typeaheadDropdownCustomClass);
}

var resetMatches = function() {
scope.matches = [];
scope.activeIdx = -1;
Expand Down