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

Commit 2eee071

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

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

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
@@ -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: 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
// icon offset should have higher priority as normal label

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) {
@@ -135,6 +135,20 @@ 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+
142+
expect(label).toHaveClass('md-required');
143+
});
144+
145+
it('should not show asterisk on required label if disabled', function() {
146+
var el = setup('md-no-asterisk');
147+
var ctrl = el.controller('mdInputContainer');
148+
149+
expect(ctrl.label).not.toHaveClass('md-required');
150+
});
151+
138152
it('should match label to given input id', function() {
139153
var el = setup('id="foo"');
140154
expect(el.find('label').attr('for')).toBe('foo');

0 commit comments

Comments
 (0)