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

Commit ab0156a

Browse files
committed
feat(input): add asterisk to input directive
Fixes #6511
1 parent 8ef798f commit ab0156a

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/components/input/input-theme.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ 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) {
24+
label.md-required:after {
25+
color: '{{foreground-2}}';
26+
}
27+
}
28+
1929
ng-messages, [ng-messages],
2030
ng-message, data-ng-message, x-ng-message,
2131
[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
@@ -135,6 +135,7 @@ function labelDirective() {
135135
* @param {string=} placeholder An alternative approach to using aria-label when the label is not
136136
* PRESENT. The placeholder text is copied to the aria-label attribute.
137137
* @param md-no-autogrow {boolean=} When present, textareas will not grow automatically.
138+
* @param md-no-asterisk {boolean=} Stops appending of an asterisk to the label of required inputs
138139
* @param md-detect-hidden {boolean=} When present, textareas will be sized properly when they are
139140
* revealed after being hidden. This is off by default for performance reasons because it
140141
* guarantees a reflow every digest cycle.
@@ -235,6 +236,9 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
235236
var hasNgModel = !!ctrls[1];
236237
var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
237238
var isReadonly = angular.isDefined(attr.readonly);
239+
var isRequired = angular.isDefined(attr.required);
240+
var mdNoAsterisk = angular.isDefined(attr.mdNoAsterisk);
241+
238242

239243
if (!containerCtrl) return;
240244
if (containerCtrl.input) {
@@ -248,6 +252,8 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
248252

249253
if (!containerCtrl.label) {
250254
$mdAria.expect(element, 'aria-label', element.attr('placeholder'));
255+
} else if (isRequired && !mdNoAsterisk) {
256+
containerCtrl.label.addClass('md-required');
251257
}
252258

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

src/components/input/input.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ 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+
}
9498
}
9599

96100
// icon offset should have higher priority as normal label

src/components/input/input.spec.js

Lines changed: 20 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) {
@@ -135,6 +135,25 @@ describe('md-input-container directive', function() {
135135
expect(el).not.toHaveClass('md-input-has-value');
136136
});
137137

138+
it('should append an asterisk to the required label', function() {
139+
var el = setup('required');
140+
var label = el.find('label');
141+
label.text("Required Label");
142+
143+
var pseudoStyle = getComputedStyle(label[0], ':after');
144+
145+
expect(label).toHaveClass('md-required');
146+
147+
expect(label.text() + pseudoStyle.content).toBe('Required Label" *"');
148+
});
149+
150+
it('should not show asterisk on required label if disabled', function() {
151+
var el = setup('md-no-asterisk');
152+
var ctrl = el.controller('mdInputContainer');
153+
154+
expect(ctrl.label).not.toHaveClass('md-required');
155+
});
156+
138157
it('should match label to given input id', function() {
139158
var el = setup('id="foo"');
140159
expect(el.find('label').attr('for')).toBe('foo');

0 commit comments

Comments
 (0)