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

Commit 1f99795

Browse files
devversionThomasBurleson
authored andcommitted
feat(input): add asterisk to input directive
Fixes #6511. Closes #6518
1 parent f6c93b8 commit 1f99795

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

src/components/input/demoErrors/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<md-input-container class="md-block">
77
<label>Description</label>
8-
<input md-maxlength="30" required name="description" ng-model="project.description">
8+
<input md-maxlength="30" required md-no-asterisk name="description" ng-model="project.description">
99
<div ng-messages="projectForm.description.$error">
1010
<div ng-message="required">This is required.</div>
1111
<div ng-message="md-maxlength">The name has to be less than 30 characters long.</div>

src/components/input/input-theme.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ md-input-container.md-THEME_NAME-theme {
1616
color: '{{foreground-3}}';
1717
}
1818

19+
label.md-required:after {
20+
color: '{{warn-A700}}'
21+
}
22+
23+
&:not(.md-input-focused) label.md-required:after {
24+
color: '{{foreground-2}}';
25+
}
26+
1927
ng-messages, [ng-messages],
2028
ng-message, data-ng-message, x-ng-message,
2129
[ng-message], [data-ng-message], [x-ng-message],

src/components/input/input.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ function labelDirective() {
150150
* @param {string=} placeholder An alternative approach to using aria-label when the label is not
151151
* PRESENT. The placeholder text is copied to the aria-label attribute.
152152
* @param md-no-autogrow {boolean=} When present, textareas will not grow automatically.
153+
* @param md-no-asterisk {boolean=} When present, asterisk will not be appended to required inputs label
153154
* @param md-detect-hidden {boolean=} When present, textareas will be sized properly when they are
154155
* revealed after being hidden. This is off by default for performance reasons because it
155156
* guarantees a reflow every digest cycle.
@@ -250,6 +251,9 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
250251
var hasNgModel = !!ctrls[1];
251252
var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
252253
var isReadonly = angular.isDefined(attr.readonly);
254+
var isRequired = angular.isDefined(attr.required);
255+
var mdNoAsterisk = angular.isDefined(attr.mdNoAsterisk);
256+
253257

254258
if (!containerCtrl) return;
255259
if (containerCtrl.input) {
@@ -264,6 +268,8 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
264268

265269
if (!containerCtrl.label) {
266270
$mdAria.expect(element, 'aria-label', element.attr('placeholder'));
271+
} else if (isRequired && !mdNoAsterisk) {
272+
containerCtrl.label.addClass('md-required');
267273
}
268274

269275
element.addClass('md-input');

src/components/input/input.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ md-input-container {
9191
bottom: 100%;
9292
@include rtl(left, 0, auto);
9393
@include rtl(right, auto, 0);
94+
95+
&.md-required:after {
96+
content: ' *';
97+
font-size: 13px;
98+
vertical-align: top;
99+
}
94100
}
95101

96102
label:not(.md-no-float):not(.md-container-ignore),

src/components/input/input.spec.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('md-input-container directive', function() {
2020

2121
var template =
2222
'<md-input-container>' +
23-
'<input ' + (attrs || '') + '>' +
2423
'<label></label>' +
24+
'<input ' + (attrs || '') + '>' +
2525
'</md-input-container>';
2626

2727
if (isForm) {
@@ -174,6 +174,20 @@ describe('md-input-container directive', function() {
174174
expect(el).not.toHaveClass('md-input-has-value');
175175
});
176176

177+
it('should append an asterisk to the required label', function() {
178+
var el = setup('required');
179+
var label = el.find('label');
180+
181+
expect(label).toHaveClass('md-required');
182+
});
183+
184+
it('should not show asterisk on required label if disabled', function() {
185+
var el = setup('md-no-asterisk');
186+
var ctrl = el.controller('mdInputContainer');
187+
188+
expect(ctrl.label).not.toHaveClass('md-required');
189+
});
190+
177191
it('should match label to given input id', function() {
178192
var el = setup('id="foo"');
179193
expect(el.find('label').attr('for')).toBe('foo');

0 commit comments

Comments
 (0)