Skip to content

Commit 3a82990

Browse files
committed
refactor: switch back to showing hints when valid
1 parent dcd029a commit 3a82990

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
@@ -222,7 +222,7 @@ $mat-input-underline-disabled-background-image:
222222

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

234+
// Clears the floats on the hints. This is necessary for the hint animation to work.
235+
.mat-input-hint-wrapper {
236+
&::before,
237+
&::after {
238+
content: ' ';
239+
display: table;
240+
}
241+
242+
&::after {
243+
clear: both;
244+
}
245+
}
246+
234247
// The hint is shown below the underline. There can be
235248
// more than one; one at the start and one at the end.
236249
.mat-hint {

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

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ describe('MdInputContainer', function () {
641641
});
642642
}));
643643

644-
it('should hide the error messages once the input becomes valid', async(() => {
644+
it('should hide the errors and show the hints once the input becomes valid', async(() => {
645645
testComponent.formControl.markAsTouched();
646646
fixture.detectChanges();
647647

@@ -650,6 +650,8 @@ describe('MdInputContainer', function () {
650650
.toContain('mat-input-invalid', 'Expected container to have the invalid CSS class.');
651651
expect(containerEl.querySelectorAll('md-error').length)
652652
.toBe(1, 'Expected one error message to have been rendered.');
653+
expect(containerEl.querySelectorAll('md-hint').length)
654+
.toBe(0, 'Expected no hints to be shown.');
653655

654656
testComponent.formControl.setValue('something');
655657
fixture.detectChanges();
@@ -659,39 +661,12 @@ describe('MdInputContainer', function () {
659661
'Expected container not to have the invalid class when valid.');
660662
expect(containerEl.querySelectorAll('md-error').length)
661663
.toBe(0, 'Expected no error messages when the input is valid.');
664+
expect(containerEl.querySelectorAll('md-hint').length)
665+
.toBe(1, 'Expected one hint to be shown once the input is valid.');
662666
});
663667
});
664668
}));
665669

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

src/lib/input/input-container.ts

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

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

386377
/**

0 commit comments

Comments
 (0)