Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(input): don't add empty prefix & suffix wrappers #3724

Merged
merged 2 commits into from
Mar 29, 2017
Merged
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
30 changes: 18 additions & 12 deletions src/lib/input/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {NgModule, ModuleWithProviders} from '@angular/core';
import {ModuleWithProviders, NgModule} from '@angular/core';
import {
MdPlaceholder,
MdInputContainer,
MdErrorDirective,
MdHint,
MdInputContainer,
MdInputDirective,
MdErrorDirective,
MdPlaceholder,
MdPrefix,
MdSuffix
} from './input-container';
import {MdTextareaAutosize} from './autosize';
import {CommonModule} from '@angular/common';
Expand All @@ -14,25 +16,29 @@ import {PlatformModule} from '../core/platform/index';

@NgModule({
declarations: [
MdPlaceholder,
MdInputContainer,
MdErrorDirective,
MdHint,
MdTextareaAutosize,
MdInputContainer,
MdInputDirective,
MdErrorDirective
MdPlaceholder,
MdPrefix,
MdSuffix,
MdTextareaAutosize,
],
imports: [
CommonModule,
FormsModule,
PlatformModule,
],
exports: [
MdPlaceholder,
MdInputContainer,
MdErrorDirective,
MdHint,
MdTextareaAutosize,
MdInputContainer,
MdInputDirective,
MdErrorDirective
MdPlaceholder,
MdPrefix,
MdSuffix,
MdTextareaAutosize,
],
})
export class MdInputModule {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/input/input-container.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="mat-input-wrapper">
<div class="mat-input-table">
<div class="mat-input-prefix">
<div class="mat-input-prefix" *ngIf="_prefixChildren.length">
<!-- TODO(andrewseguin): remove [md-prefix] -->
<ng-content select="[mdPrefix], [matPrefix], [md-prefix]"></ng-content>
</div>
Expand All @@ -23,7 +23,7 @@
</span>
</div>

<div class="mat-input-suffix">
<div class="mat-input-suffix" *ngIf="_suffixChildren.length">
Copy link
Member

Choose a reason for hiding this comment

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

Add a unit test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

<!-- TODO(andrewseguin): remove [md-suffix] -->
<ng-content select="[mdSuffix], [matSuffix], [md-suffix]"></ng-content>
</div>
Expand Down
98 changes: 66 additions & 32 deletions src/lib/input/input-container.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {async, TestBed, inject, ComponentFixture} from '@angular/core/testing';
import {async, ComponentFixture, inject, TestBed} from '@angular/core/testing';
import {Component, ViewChild} from '@angular/core';
import {
FormsModule,
ReactiveFormsModule,
FormControl,
NgForm,
Validators,
FormGroupDirective,
FormGroup,
FormGroupDirective,
FormsModule,
NgForm,
ReactiveFormsModule,
Validators
} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
Expand All @@ -18,52 +18,53 @@ import {PlatformModule} from '../core/platform/index';
import {wrappedErrorMessage} from '../core/testing/wrapped-error-message';
import {dispatchFakeEvent} from '../core/testing/dispatch-events';
import {
MdInputContainerDuplicatedHintError,
MdInputContainerMissingMdInputError,
MdInputContainerPlaceholderConflictError,
MdInputContainerDuplicatedHintError
MdInputContainerPlaceholderConflictError
} from './input-container-errors';


describe('MdInputContainer', function () {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MdInputModule.forRoot(),
PlatformModule.forRoot(),
FormsModule,
MdInputModule,
NoopAnimationsModule,
PlatformModule,
ReactiveFormsModule,
NoopAnimationsModule
],
declarations: [
MdInputContainerPlaceholderRequiredTestComponent,
MdInputContainerPlaceholderElementTestComponent,
MdInputContainerPlaceholderAttrTestComponent,
MdInputContainerBaseTestController,
MdInputContainerDateTestController,
MdInputContainerHintLabel2TestController,
MdInputContainerHintLabelTestController,
MdInputContainerInvalidTypeTestController,
MdInputContainerInvalidPlaceholderTestController,
MdInputContainerInvalidHint2TestController,
MdInputContainerInvalidHintTestController,
MdInputContainerBaseTestController,
MdInputContainerWithId,
MdInputContainerDateTestController,
MdInputContainerTextTestController,
MdInputContainerPasswordTestController,
MdInputContainerInvalidPlaceholderTestController,
MdInputContainerInvalidTypeTestController,
MdInputContainerMissingMdInputTestController,
MdInputContainerMultipleHintMixedTestController,
MdInputContainerMultipleHintTestController,
MdInputContainerNumberTestController,
MdInputContainerZeroTestController,
MdTextareaWithBindings,
MdInputContainerPasswordTestController,
MdInputContainerPlaceholderAttrTestComponent,
MdInputContainerPlaceholderElementTestComponent,
MdInputContainerPlaceholderRequiredTestComponent,
MdInputContainerTextTestController,
MdInputContainerWithDisabled,
MdInputContainerWithDynamicPlaceholder,
MdInputContainerWithFormControl,
MdInputContainerWithFormErrorMessages,
MdInputContainerWithFormGroupErrorMessages,
MdInputContainerWithId,
MdInputContainerWithPrefixAndSuffix,
MdInputContainerWithRequired,
MdInputContainerWithStaticPlaceholder,
MdInputContainerWithType,
MdInputContainerWithValueBinding,
MdInputContainerWithFormControl,
MdInputContainerWithStaticPlaceholder,
MdInputContainerMissingMdInputTestController,
MdInputContainerMultipleHintTestController,
MdInputContainerMultipleHintMixedTestController,
MdInputContainerWithDynamicPlaceholder,
MdInputContainerWithFormErrorMessages,
MdInputContainerWithFormGroupErrorMessages
MdInputContainerZeroTestController,
MdTextareaWithBindings,
],
});

Expand Down Expand Up @@ -685,6 +686,29 @@ describe('MdInputContainer', function () {

});

it('should not have prefix and suffix elements when none are specified', () => {
let fixture = TestBed.createComponent(MdInputContainerWithId);
fixture.detectChanges();

let prefixEl = fixture.debugElement.query(By.css('.mat-input-prefix'));
let suffixEl = fixture.debugElement.query(By.css('.mat-input-suffix'));

expect(prefixEl).toBeNull();
expect(suffixEl).toBeNull();
});

it('should add prefix and suffix elements when specified', () => {
let fixture = TestBed.createComponent(MdInputContainerWithPrefixAndSuffix);
fixture.detectChanges();

let prefixEl = fixture.debugElement.query(By.css('.mat-input-prefix'));
let suffixEl = fixture.debugElement.query(By.css('.mat-input-suffix'));

expect(prefixEl).not.toBeNull();
expect(suffixEl).not.toBeNull();
expect(prefixEl.nativeElement.innerText.trim()).toEqual('Prefix');
expect(suffixEl.nativeElement.innerText.trim()).toEqual('Suffix');
});
});

@Component({
Expand Down Expand Up @@ -927,7 +951,6 @@ class MdInputContainerWithFormErrorMessages {
renderError = true;
}


@Component({
template: `
<form [formGroup]="formGroup" novalidate>
Expand All @@ -945,3 +968,14 @@ class MdInputContainerWithFormGroupErrorMessages {
name: new FormControl('', Validators.required)
});
}

@Component({
template: `
<md-input-container>
<div mdPrefix>Prefix</div>
<input mdInput>
<div mdSuffix>Suffix</div>
</md-input-container>
`
})
class MdInputContainerWithPrefixAndSuffix {}
38 changes: 25 additions & 13 deletions src/lib/input/input-container.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import {
AfterViewInit,
AfterContentInit,
AfterViewInit,
ChangeDetectorRef,
Component,
ContentChild,
ContentChildren,
Directive,
ElementRef,
EventEmitter,
Input,
Optional,
Output,
EventEmitter,
QueryList,
Renderer,
ChangeDetectorRef,
ViewEncapsulation,
Self,
QueryList,
ViewEncapsulation
} from '@angular/core';
import {
animate,
state,
style,
transition,
trigger,
} from '@angular/animations';
import {animate, state, style, transition, trigger} from '@angular/animations';
import {coerceBooleanProperty} from '../core';
import {NgControl, NgForm, FormGroupDirective} from '@angular/forms';
import {FormGroupDirective, NgControl, NgForm} from '@angular/forms';
import {getSupportedInputTypes} from '../core/platform/features';
import {
MdInputContainerDuplicatedHintError,
Expand Down Expand Up @@ -90,6 +84,20 @@ export class MdHint {
})
export class MdErrorDirective { }

/** The input prefix. */
@Directive({
selector: '[mdPrefix], [matPrefix], [md-prefix]'
})
export class MdPrefix {}


/** The input suffix. */
@Directive({
selector: '[mdSuffix], [matSuffix], [md-suffix]'
})
export class MdSuffix {}


/** The input directive, used to mark the input that `MdInputContainer` is wrapping. */
@Directive({
selector: `input[mdInput], textarea[mdInput], input[matInput], textarea[matInput]`,
Expand Down Expand Up @@ -327,6 +335,10 @@ export class MdInputContainer implements AfterViewInit, AfterContentInit {

@ContentChildren(MdHint) _hintChildren: QueryList<MdHint>;

@ContentChildren(MdPrefix) _prefixChildren: QueryList<MdPrefix>;

@ContentChildren(MdSuffix) _suffixChildren: QueryList<MdSuffix>;

constructor(
private _changeDetectorRef: ChangeDetectorRef,
@Optional() private _parentForm: NgForm,
Expand Down