Skip to content

Commit af41cdd

Browse files
committed
refactor: switch back to showing hints when valid
1 parent 7725dc7 commit af41cdd

File tree

4 files changed

+25
-45
lines changed

4 files changed

+25
-45
lines changed

src/lib/input/input-container.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@
3636
[class.mat-warn]="dividerColor == 'warn'"></span>
3737
</div>
3838

39-
<div class="mat-input-hint-wrapper" [ngSwitch]="_getDisplayedMessages()">
39+
<div class="mat-input-subscript-wrapper" [ngSwitch]="_getDisplayedMessages()">
4040
<div *ngSwitchCase="'error'" [@transitionMessages]="_subscriptAnimationState">
4141
<ng-content select="md-error, mat-error"></ng-content>
4242
</div>
4343

44-
<div *ngSwitchCase="'hint'" [@transitionMessages]="_subscriptAnimationState">
44+
<div class="mat-input-hint-wrapper" *ngSwitchCase="'hint'"
45+
[@transitionMessages]="_subscriptAnimationState">
4546
<div *ngIf="hintLabel" [id]="_hintLabelId" class="mat-hint">{{hintLabel}}</div>
4647
<ng-content select="md-hint, mat-hint"></ng-content>
4748
</div>

src/lib/input/input-container.scss

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ $mat-input-underline-disabled-background-image:
214214

215215
// Wrapper for the hints and error messages. Provides positioning and text size.
216216
// Note that we're using `top` in order to allow for stacked children to flow downwards.
217-
.mat-input-hint-wrapper {
217+
.mat-input-subscript-wrapper {
218218
position: absolute;
219219
font-size: 75%;
220220
top: 100%;
@@ -223,6 +223,19 @@ $mat-input-underline-disabled-background-image:
223223
overflow: hidden; // prevents multi-line errors from overlapping the input
224224
}
225225

226+
// Clears the floats on the hints. This is necessary for the hint animation to work.
227+
.mat-input-hint-wrapper {
228+
&::before,
229+
&::after {
230+
content: ' ';
231+
display: table;
232+
}
233+
234+
&::after {
235+
clear: both;
236+
}
237+
}
238+
226239
// The hint is shown below the underline. There can be
227240
// more than one; one at the start and one at the end.
228241
.mat-hint {

src/lib/input/input-container.spec.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ describe('MdInputContainer', function () {
638638
});
639639
}));
640640

641-
it('should hide the error messages once the input becomes valid', async(() => {
641+
it('should hide the errors and show the hints once the input becomes valid', async(() => {
642642
testComponent.formControl.markAsTouched();
643643
fixture.detectChanges();
644644

@@ -647,6 +647,8 @@ describe('MdInputContainer', function () {
647647
.toContain('mat-input-invalid', 'Expected container to have the invalid CSS class.');
648648
expect(containerEl.querySelectorAll('md-error').length)
649649
.toBe(1, 'Expected one error message to have been rendered.');
650+
expect(containerEl.querySelectorAll('md-hint').length)
651+
.toBe(0, 'Expected no hints to be shown.');
650652

651653
testComponent.formControl.setValue('something');
652654
fixture.detectChanges();
@@ -656,39 +658,12 @@ describe('MdInputContainer', function () {
656658
'Expected container not to have the invalid class when valid.');
657659
expect(containerEl.querySelectorAll('md-error').length)
658660
.toBe(0, 'Expected no error messages when the input is valid.');
661+
expect(containerEl.querySelectorAll('md-hint').length)
662+
.toBe(1, 'Expected one hint to be shown once the input is valid.');
659663
});
660664
});
661665
}));
662666

663-
it('should hide the hints when there are errors and not show them again when' +
664-
' the input becomes valid', async(() => {
665-
666-
expect(containerEl.querySelectorAll('md-hint').length)
667-
.toBe(1, 'Expected one hint to be shown on load.');
668-
expect(containerEl.querySelectorAll('md-error').length)
669-
.toBe(0, 'Expected no errors to be shown on load.');
670-
671-
testComponent.formControl.markAsTouched();
672-
fixture.detectChanges();
673-
674-
fixture.whenStable().then(() => {
675-
expect(containerEl.querySelectorAll('md-hint').length)
676-
.toBe(0, 'Expected no hints to be shown after interaction.');
677-
expect(containerEl.querySelectorAll('md-error').length)
678-
.toBe(1, 'Expected one error to be shown after interaction.');
679-
680-
testComponent.formControl.setValue('something');
681-
fixture.detectChanges();
682-
683-
fixture.whenStable().then(() => {
684-
expect(containerEl.querySelectorAll('md-hint').length)
685-
.toBe(0, 'Expected no hints to be shown after the value is set.');
686-
expect(containerEl.querySelectorAll('md-error').length)
687-
.toBe(0, 'Expected no errors to be shown after the value is set.');
688-
});
689-
});
690-
}));
691-
692667
it('should not hide the hint if there are no error messages', async(() => {
693668
testComponent.renderError = false;
694669
fixture.detectChanges();

src/lib/input/input-container.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,9 @@ export class MdInputContainer implements AfterViewInit, AfterContentInit {
366366
return !!(isInvalid && (isTouched || isSubmitted));
367367
}
368368

369-
/** Determines whether to display hints, errors or no messages at all. */
370-
_getDisplayedMessages(): 'error' | 'hint' | 'none' {
371-
if (this._errorChildren.length > 0) {
372-
if (this._isErrorState()) {
373-
return 'error';
374-
} else if (this._mdInputChild._ngControl) {
375-
let control = this._mdInputChild._ngControl;
376-
return (control.valid && control.touched) ? 'none' : 'hint';
377-
}
378-
}
379-
380-
return 'hint';
369+
/** Determines whether to display hints or errors. */
370+
_getDisplayedMessages(): 'error' | 'hint' {
371+
return (this._errorChildren.length > 0 && this._isErrorState()) ? 'error' : 'hint';
381372
}
382373

383374
/**

0 commit comments

Comments
 (0)