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

Commit c43cc91

Browse files
committed
Add resetSearchInput attribute
1 parent e7e0596 commit c43cc91

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/select.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,27 @@ angular.module('ui.select', [])
110110
ctrl.items = [];
111111
ctrl.selected = undefined;
112112
ctrl.open = false;
113-
ctrl.disabled = false;
113+
ctrl.disabled = undefined; // Initialized inside uiSelect directive link function
114+
ctrl.resetSearchInput = undefined; // Initialized inside uiSelect directive link function
114115

115116
ctrl.searchInput = $element.querySelectorAll('input.ui-select-search');
116117
if (ctrl.searchInput.length !== 1) {
117118
throw uiSelectMinErr('searchInput', "Expected 1 input.ui-select-search but got '{0}'.", ctrl.searchInput.length);
118119
}
119120

121+
// Most of the time the user does not want to empty the search input when in typeahead mode
122+
function _resetSearchInput() {
123+
if (ctrl.resetSearchInput) {
124+
ctrl.search = EMPTY_SEARCH;
125+
}
126+
}
127+
120128
// When the user clicks on ui-select, displays the dropdown list
121129
ctrl.activate = function() {
122-
if (ctrl.disabled === false) {
130+
if (!ctrl.disabled) {
131+
_resetSearchInput();
123132
ctrl.open = true;
133+
124134
// Give it time to appear before focus
125135
$timeout(function() {
126136
ctrl.searchInput[0].focus();
@@ -143,6 +153,11 @@ angular.module('ui.select', [])
143153
});
144154
};
145155

156+
/**
157+
* Typeahead mode: lets the user refresh the collection using his own function.
158+
*
159+
* See Expose $select.search for external / remote filtering https://github.com/angular-ui/ui-select/pull/31
160+
*/
146161
ctrl.refresh = function(refreshAttr) {
147162
if (refreshAttr !== undefined) {
148163
$timeout(function() {
@@ -160,8 +175,8 @@ angular.module('ui.select', [])
160175

161176
ctrl.close = function() {
162177
if (ctrl.open) {
178+
_resetSearchInput();
163179
ctrl.open = false;
164-
ctrl.search = EMPTY_SEARCH;
165180
}
166181
};
167182

@@ -219,7 +234,14 @@ angular.module('ui.select', [])
219234
var ngModel = ctrls[1];
220235

221236
attrs.$observe('disabled', function() {
222-
$select.disabled = attrs.disabled ? true : false;
237+
// No need to use $eval() (thanks to ng-disabled) since we already get a boolean instead of a string
238+
$select.disabled = attrs.disabled !== undefined ? attrs.disabled : false;
239+
});
240+
241+
attrs.$observe('resetSearchInput', function() {
242+
// $eval() is needed otherwise we get a string instead of a boolean
243+
var resetSearchInput = scope.$eval(attrs.resetSearchInput);
244+
$select.resetSearchInput = resetSearchInput !== undefined ? resetSearchInput : true;
223245
});
224246

225247
scope.$watch('$select.selected', function(newValue, oldValue) {

0 commit comments

Comments
 (0)