Skip to content

Commit 3adf8ad

Browse files
committed
refactor: switch back to showing hints when valid
1 parent 29d2a5f commit 3adf8ad

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

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

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