Skip to content

Commit

Permalink
fix(input-container): New attribute hideRequiredMarker (#4237)
Browse files Browse the repository at this point in the history
* fix(input-container): New attribute hideRequiredMarker to md-input-container

This attribute will enable the user to hide the required marker (star) fron an mdInput even when it's required

Fixes #3681

* fix(input-container): Checking if the input container placeholder has the '*' before applying the 'hideRequiredMarker' attribute on test. Removing extra leading space on hideRequiredMarker demo.
  • Loading branch information
jefersonestevo authored and mmalerba committed Apr 25, 2017
1 parent 5e39091 commit 6c31adb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/demo-app/input/input-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ <h4>Textarea</h4>
[placeholder]="requiredField ? 'Required field' : 'Not required field'">
</md-input-container>
</p>
<p>
<md-checkbox [(ngModel)]="hideRequiredMarker">Check to hide the required marker:</md-checkbox>
<md-input-container [hideRequiredMarker]="hideRequiredMarker" style="width: 300px">
<input mdInput
required
[placeholder]="hideRequiredMarker ? 'Required Without Marker' : 'Required With Marker'">
</md-input-container>
</p>
<p>
<md-button-toggle-group [(ngModel)]="floatingLabel">
<md-button-toggle value="auto">Auto Float</md-button-toggle>
Expand Down
1 change: 1 addition & 0 deletions src/demo-app/input/input-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class InputDemo {
floatingLabel: string = 'auto';
color: boolean;
requiredField: boolean;
hideRequiredMarker: boolean;
ctrlDisabled = false;

name: string;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/input/input-container.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*ngIf="_hasPlaceholder()">
<ng-content select="md-placeholder, mat-placeholder"></ng-content>
{{_mdInputChild.placeholder}}
<span class="mat-placeholder-required" *ngIf="_mdInputChild.required">*</span>
<span class="mat-placeholder-required" *ngIf="!hideRequiredMarker && _mdInputChild.required">*</span>
</label>
</span>
</div>
Expand Down
22 changes: 20 additions & 2 deletions src/lib/input/input-container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,20 @@ describe('MdInputContainer', function () {
expect(el.nativeElement.textContent).toMatch(/hello\s+\*/g);
});

it('hide placeholder required star when set to hide the required marker', () => {
let fixture = TestBed.createComponent(MdInputContainerPlaceholderRequiredTestComponent);
fixture.detectChanges();

let el = fixture.debugElement.query(By.css('label'));
expect(el).not.toBeNull();
expect(el.nativeElement.textContent).toMatch(/hello\s+\*/g);

fixture.componentInstance.hideRequiredMarker = true;
fixture.detectChanges();

expect(el.nativeElement.textContent).toMatch(/hello/g);
});

it('supports the disabled attribute as binding', async(() => {
const fixture = TestBed.createComponent(MdInputContainerWithDisabled);
fixture.detectChanges();
Expand Down Expand Up @@ -741,9 +755,13 @@ class MdInputContainerWithType {
}

@Component({
template: `<md-input-container><input mdInput required placeholder="hello"></md-input-container>`
template: `<md-input-container [hideRequiredMarker]="hideRequiredMarker">
<input mdInput required placeholder="hello">
</md-input-container>`
})
class MdInputContainerPlaceholderRequiredTestComponent {}
class MdInputContainerPlaceholderRequiredTestComponent {
hideRequiredMarker: boolean;
}

@Component({
template: `
Expand Down
8 changes: 8 additions & 0 deletions src/lib/input/input-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ export class MdInputContainer implements AfterViewInit, AfterContentInit {
get dividerColor() { return this.color; }
set dividerColor(value) { this.color = value; }

/** Whether we should hide the required marker. */
@Input()
get hideRequiredMarker() { return this._hideRequiredMarker; }
set hideRequiredMarker(value: any) {
this._hideRequiredMarker = coerceBooleanProperty(value);
}
private _hideRequiredMarker: boolean;

/** Whether the floating label should always float or not. */
get _shouldAlwaysFloat() { return this._floatPlaceholder === 'always'; }

Expand Down

0 comments on commit 6c31adb

Please sign in to comment.