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

Commit d978458

Browse files
devversionThomasBurleson
authored andcommitted
feat(select): support asterisk on floating labels.
* This adds support for asterisks on the select's floating labels. * Also adds a description to the input and select documentation. Closes #7928. Closes #8348
1 parent 2173a2f commit d978458

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/components/input/input.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ function labelDirective() {
157157
* You can use any `<input>` or `<textarea>` element as a child of an `<md-input-container>`. This
158158
* allows you to build complex forms for data entry.
159159
*
160+
* When the input is required and uses a floating label, then the label will automatically contain
161+
* an asterisk (`*`).<br/>
162+
* This behavior can be disabled by using the `md-no-asterisk` attribute.
163+
*
160164
* @param {number=} md-maxlength The maximum number of characters allowed in this input. If this is
161165
* specified, a character counter will be shown underneath the input.<br/><br/>
162166
* The purpose of **`md-maxlength`** is exactly to show the max length counter text. If you don't
@@ -169,7 +173,7 @@ function labelDirective() {
169173
* @param {string=} placeholder An alternative approach to using aria-label when the label is not
170174
* PRESENT. The placeholder text is copied to the aria-label attribute.
171175
* @param md-no-autogrow {boolean=} When present, textareas will not grow automatically.
172-
* @param md-no-asterisk {boolean=} When present, asterisk will not be appended to required inputs label
176+
* @param md-no-asterisk {boolean=} When present, an asterisk will not be appended to the inputs floating label
173177
* @param md-detect-hidden {boolean=} When present, textareas will be sized properly when they are
174178
* revealed after being hidden. This is off by default for performance reasons because it
175179
* guarantees a reflow every digest cycle.

src/components/select/select.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ angular.module('material.components.select', [
3434
*
3535
* @description Displays a select box, bound to an ng-model.
3636
*
37+
* When the select is required and uses a floating label, then the label will automatically contain
38+
* an asterisk (`*`).<br/>
39+
* This behavior can be disabled by using the `md-no-asterisk` attribute.
40+
*
3741
* @param {expression} ng-model The model!
3842
* @param {boolean=} multiple Whether it's multiple.
3943
* @param {expression=} md-on-close Expression to be evaluated when the select is closed.
@@ -42,6 +46,7 @@ angular.module('material.components.select', [
4246
* @param {expression=} md-selected-text Expression to be evaluated that will return a string
4347
* to be displayed as a placeholder in the select input box when it is closed.
4448
* @param {string=} placeholder Placeholder hint text.
49+
* @param md-no-asterisk {boolean=} When set to true, an asterisk will not be appended to the floating label.
4550
* @param {string=} aria-label Optional label for accessibility. Only necessary if no placeholder or
4651
* explicit label is present.
4752
* @param {string=} md-container-class Class list to get applied to the `._md-select-menu-container`
@@ -227,6 +232,7 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
227232
// grab a reference to the select menu value label
228233
var valueEl = element.find('md-select-value');
229234
var isReadonly = angular.isDefined(attr.readonly);
235+
var disableAsterisk = $mdUtil.parseAttributeBoolean(attr.mdNoAsterisk);
230236

231237
if (containerCtrl) {
232238
var isErrorGetter = containerCtrl.isErrorGetter || function() {
@@ -284,6 +290,13 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
284290

285291
attr.$observe('placeholder', ngModelCtrl.$render);
286292

293+
if (containerCtrl && containerCtrl.label) {
294+
attr.$observe('required', function (value) {
295+
// Toggle the md-required class on the input containers label, because the input container is automatically
296+
// applying the asterisk indicator on the label.
297+
containerCtrl.label.toggleClass('md-required', value && !disableAsterisk);
298+
});
299+
}
287300

288301
mdSelectCtrl.setLabelText = function(text) {
289302
mdSelectCtrl.setIsPlaceholder(!text);

src/components/select/select.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,29 @@ describe('<md-select>', function() {
838838

839839
expect($rootScope.testForm.$pristine).toBe(true);
840840
}));
841+
842+
it('should correctly update the input containers label', inject(function($rootScope) {
843+
var el = setupSelect('ng-required="isRequired" ng-model="someModel"');
844+
var label = el.find('label');
845+
846+
expect(label).not.toHaveClass('md-required');
847+
848+
$rootScope.$apply('isRequired = true');
849+
850+
expect(label).toHaveClass('md-required');
851+
}));
852+
853+
it('should correctly update the input containers label when asterisk is disabled', inject(function($rootScope) {
854+
var el = setupSelect('ng-required="isRequired" md-no-asterisk ng-model="someModel"');
855+
var label = el.find('label');
856+
857+
expect(label).not.toHaveClass('md-required');
858+
859+
$rootScope.$apply('isRequired = true');
860+
861+
expect(label).not.toHaveClass('md-required');
862+
}));
863+
841864
});
842865

843866
describe('view->model', function() {

0 commit comments

Comments
 (0)