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

feat(input): add asterisk to input directive #6518

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/input/demoErrors/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<md-input-container class="md-block">
<label>Description</label>
<input md-maxlength="30" required name="description" ng-model="project.description">
<input md-maxlength="30" required md-no-asterisk name="description" ng-model="project.description">
<div ng-messages="projectForm.description.$error">
<div ng-message="required">This is required.</div>
<div ng-message="md-maxlength">The name has to be less than 30 characters long.</div>
Expand Down
8 changes: 8 additions & 0 deletions src/components/input/input-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ md-input-container.md-THEME_NAME-theme {
color: '{{foreground-3}}';
}

label.md-required:after {
color: '{{warn-A700}}'
}

&:not(.md-input-focused) label.md-required:after {
color: '{{foreground-2}}';
}

ng-messages, [ng-messages],
ng-message, data-ng-message, x-ng-message,
[ng-message], [data-ng-message], [x-ng-message],
Expand Down
6 changes: 6 additions & 0 deletions src/components/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function labelDirective() {
* @param {string=} placeholder An alternative approach to using aria-label when the label is not
* PRESENT. The placeholder text is copied to the aria-label attribute.
* @param md-no-autogrow {boolean=} When present, textareas will not grow automatically.
* @param md-no-asterisk {boolean=} When present, asterisk will not be appended to required inputs label
* @param md-detect-hidden {boolean=} When present, textareas will be sized properly when they are
* revealed after being hidden. This is off by default for performance reasons because it
* guarantees a reflow every digest cycle.
Expand Down Expand Up @@ -235,6 +236,9 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
var hasNgModel = !!ctrls[1];
var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
var isReadonly = angular.isDefined(attr.readonly);
var isRequired = angular.isDefined(attr.required);
var mdNoAsterisk = angular.isDefined(attr.mdNoAsterisk);


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

if (!containerCtrl.label) {
$mdAria.expect(element, 'aria-label', element.attr('placeholder'));
} else if (isRequired && !mdNoAsterisk) {
containerCtrl.label.addClass('md-required');
}

element.addClass('md-input');
Expand Down
6 changes: 6 additions & 0 deletions src/components/input/input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ md-input-container {
bottom: 100%;
@include rtl(left, 0, auto);
@include rtl(right, auto, 0);

&.md-required:after {
content: ' *';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the content reverse for rtl text?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asterisk will be aligned in the same direction as the other text, so it's compatible with RTL, LTR.
But it seems that the placeholders are aligned wrong? (See here.

But in general, Yes asterisk will be reversed for RTL text

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devversion I know this is old, but I thought I would update this since I just looked at it. The labels are properly "aligned" to the right, it's just that the width is not 100% of the parent. I guess we need to use right: 0 in addition to text-align: right here to fix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just looked a bit more; it's actually an issue with the scale(0.75) transform.

Looks like we can fix it with transform-origin: right;.

font-size: 13px;
vertical-align: top;
}
}

// icon offset should have higher priority as normal label
Expand Down
16 changes: 15 additions & 1 deletion src/components/input/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('md-input-container directive', function() {

var template =
'<md-input-container>' +
'<input ' + (attrs || '') + '>' +
'<label></label>' +
'<input ' + (attrs || '') + '>' +
'</md-input-container>';

if (isForm) {
Expand Down Expand Up @@ -135,6 +135,20 @@ describe('md-input-container directive', function() {
expect(el).not.toHaveClass('md-input-has-value');
});

it('should append an asterisk to the required label', function() {
var el = setup('required');
var label = el.find('label');

expect(label).toHaveClass('md-required');
});

it('should not show asterisk on required label if disabled', function() {
var el = setup('md-no-asterisk');
var ctrl = el.controller('mdInputContainer');

expect(ctrl.label).not.toHaveClass('md-required');
});

it('should match label to given input id', function() {
var el = setup('id="foo"');
expect(el.find('label').attr('for')).toBe('foo');
Expand Down