Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit e7af2c8

Browse files
Derek Louiejelbourn
authored andcommitted
feat(select): Adding md-selected-text attribute to md-select.
Closes #7721
1 parent cd987f0 commit e7af2c8

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div ng-controller="SelectedTextController" class="md-padding" ng-cloak>
2+
<div>
3+
<h1 class="md-title">Pick an item below</h1>
4+
<div layout="row">
5+
<md-input-container>
6+
<label>Items</label>
7+
<md-select ng-model="selectedItem" md-selected-text="getSelectedText()">
8+
<md-optgroup label="items">
9+
<md-option ng-value="item" ng-repeat="item in items">Item {{item}}</md-option>
10+
</md-optgroup>
11+
</md-select>
12+
</md-input-container>
13+
</div>
14+
</div>
15+
</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
angular
2+
.module('selectDemoSelectedText', ['ngMaterial'])
3+
.controller('SelectedTextController', function($scope) {
4+
$scope.items = [1, 2, 3, 4, 5, 6, 7];
5+
$scope.selectedItem;
6+
$scope.getSelectedText = function() {
7+
if ($scope.selectedItem !== undefined) {
8+
return "You have selected: Item " + $scope.selectedItem;
9+
} else {
10+
return "Please select an item";
11+
}
12+
};
13+
});

src/components/select/select.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ angular.module('material.components.select', [
3838
* @param {expression=} md-on-close Expression to be evaluated when the select is closed.
3939
* @param {expression=} md-on-open Expression to be evaluated when opening the select.
4040
* Will hide the select options and show a spinner until the evaluated promise resolves.
41+
* @param {expression=} md-selected-text Expression to be evaluated that will return a string
42+
* to be displayed as a placeholder in the select input box when it is closed.
4143
* @param {string=} placeholder Placeholder hint text.
4244
* @param {string=} aria-label Optional label for accessibility. Only necessary if no placeholder or
4345
* explicit label is present.
@@ -257,9 +259,16 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
257259

258260
mdSelectCtrl.setLabelText = function(text) {
259261
mdSelectCtrl.setIsPlaceholder(!text);
260-
// Use placeholder attribute, otherwise fallback to the md-input-container label
261-
var tmpPlaceholder = attr.placeholder || (containerCtrl && containerCtrl.label ? containerCtrl.label.text() : '');
262-
text = text || tmpPlaceholder || '';
262+
263+
if (attr.mdSelectedText) {
264+
text = $parse(attr.mdSelectedText)(scope);
265+
} else {
266+
// Use placeholder attribute, otherwise fallback to the md-input-container label
267+
var tmpPlaceholder = attr.placeholder ||
268+
(containerCtrl && containerCtrl.label ? containerCtrl.label.text() : '');
269+
text = text || tmpPlaceholder || '';
270+
}
271+
263272
var target = valueEl.children().eq(0);
264273
target.html(text);
265274
};
@@ -1480,4 +1489,3 @@ function SelectProvider($$interimElementProvider) {
14801489
return isScrollable;
14811490
}
14821491
}
1483-

src/components/select/select.spec.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('<md-select>', function() {
8282

8383
it('sets aria-owns between the select and the container', function() {
8484
var select = setupSelect('ng-model="val"').find('md-select');
85-
var ownsId = select.attr('aria-owns');
85+
var ownsId = select.attr('aria-owns');
8686
expect(ownsId).toBeTruthy();
8787
var containerId = select[0].querySelector('.md-select-menu-container').getAttribute('id');
8888
expect(ownsId).toBe(containerId);
@@ -137,7 +137,7 @@ describe('<md-select>', function() {
137137
var opts = [ { id: 1, name: 'Bob' }, { id: 2, name: 'Alice' } ];
138138
var select = setupSelect('ng-model="$root.val" ng-change="onChange()" ng-model-options="{trackBy: \'$value.id\'}"', opts);
139139
expect(changed).toBe(false);
140-
140+
141141
openSelect(select);
142142
clickOption(select, 1);
143143
waitForSelectClose();
@@ -284,6 +284,24 @@ describe('<md-select>', function() {
284284
}
285285
}));
286286

287+
it('displays md-selected-text when specified', inject(function($rootScope) {
288+
$rootScope.selectedText = 'Hello World';
289+
290+
var select = setupSelect('ng-model="someVal", md-selected-text="selectedText"', null, true).find('md-select');
291+
var label = select.find('md-select-value');
292+
293+
expect(label.text()).toBe($rootScope.selectedText);
294+
295+
$rootScope.selectedText = 'Goodbye world';
296+
297+
// The label update function is not called until some user action occurs.
298+
openSelect(select);
299+
closeSelect(select);
300+
waitForSelectClose();
301+
302+
expect(label.text()).toBe($rootScope.selectedText);
303+
}));
304+
287305
it('supports rendering multiple', inject(function($rootScope, $compile) {
288306
$rootScope.val = [1, 3];
289307
var select = $compile('<md-input-container>' +

0 commit comments

Comments
 (0)