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

Commit fa1cdfc

Browse files
gororkwesleycho
authored andcommitted
feat(typeahead): add customClass support for dropdown
- Adds support for custom classes on the typeahead dropdown Closes #4332 Closes #4410
1 parent 7ba2527 commit fa1cdfc

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/typeahead/docs/demo.html

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
<style>
2+
.dropdown-menu.demo-class {
3+
border-radius: 0;
4+
font-style: italic;
5+
box-shadow: 5px 5px 0 rgba(0, 0, 0, .12);
6+
}
7+
.dropdown-menu.demo-class > .active > a,
8+
.dropdown-menu.demo-class > .active > a:hover,
9+
.dropdown-menu.demo-class > .active > a:focus {
10+
background-color: rgba(247, 150, 4, .69);
11+
}
12+
</style>
113
<script type="text/ng-template" id="customTemplate.html">
214
<a>
315
<img ng-src="http://upload.wikimedia.org/wikipedia/commons/thumb/{{match.model.flag}}" width="16">
@@ -21,4 +33,8 @@ <h4>Asynchronous results</h4>
2133
<h4>Custom templates for results</h4>
2234
<pre>Model: {{customSelected | json}}</pre>
2335
<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">
36+
37+
<h4>Custom class for append-to-body popup</h4>
38+
<pre>Model: {{customClassSelected | json}}</pre>
39+
<input type="text" ng-model="customClassSelected" 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">
2440
</div>

src/typeahead/docs/readme.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,9 @@ The typeahead directives provide several attributes:
7373
On blur, select the currently highlighted match
7474

7575
* `typeahead-focus-on-select`
76-
_(Defaults: true) :
76+
_(Defaults: true)_ :
7777
On selection, focus the input element the typeahead directive is associated with
78+
79+
* `typeahead-dropdown-custom-class`
80+
_(Defaults: undefined)_ :
81+
Add custom class to popup

src/typeahead/test/typeahead.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,19 @@ describe('typeahead tests', function() {
595595
findMatches(element).eq(1).trigger('mouseenter');
596596
expect(element).toBeOpenWithActive(2, 1);
597597
});
598+
599+
it('should add a custom class to popup', function() {
600+
var element = prepareInputEl('<div><input ng-model="result" typeahead="item for item in source | filter:$viewValue" typeahead-dropdown-custom-class="demo-class"></div>');
601+
changeInputValueTo(element, 'b');
602+
expect(findDropDown(element).hasClass('demo-class')).toBeTruthy();
603+
});
604+
605+
it('should add a custom class to popup with custom template', function() {
606+
$templateCache.put('custom.html', '<div>foo</div>');
607+
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>');
608+
changeInputValueTo(element, 'b');
609+
expect(findDropDown(element).hasClass('demo-class dropdown-menu')).toBeTruthy();
610+
});
598611
});
599612

600613
describe('promises', function() {
@@ -895,6 +908,12 @@ describe('typeahead tests', function() {
895908
$timeout.flush();
896909
expect(dropdown.css('top')).toEqual('500px');
897910
});
911+
912+
it('should add a custom class to append-to-body popup', function() {
913+
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>');
914+
changeInputValueTo(element, 'b');
915+
expect(findDropDown($document.find('body')).hasClass('demo-class')).toBeTruthy();
916+
});
898917
});
899918

900919
describe('focus first', function() {

src/typeahead/typeahead.js

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
133133
popUpEl.attr('popup-template-url', attrs.typeaheadPopupTemplateUrl);
134134
}
135135

136+
// Add a custom class to Dropdown Menu element
137+
if (angular.isDefined(attrs.typeaheadDropdownCustomClass)) {
138+
popUpEl.addClass(attrs.typeaheadDropdownCustomClass);
139+
}
140+
136141
var resetMatches = function() {
137142
scope.matches = [];
138143
scope.activeIdx = -1;

0 commit comments

Comments
 (0)