Skip to content

Commit 6c31adb

Browse files
jefersonestevommalerba
authored andcommitted
fix(input-container): New attribute hideRequiredMarker (#4237)
* 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.
1 parent 5e39091 commit 6c31adb

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

src/demo-app/input/input-demo.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ <h4>Textarea</h4>
244244
[placeholder]="requiredField ? 'Required field' : 'Not required field'">
245245
</md-input-container>
246246
</p>
247+
<p>
248+
<md-checkbox [(ngModel)]="hideRequiredMarker">Check to hide the required marker:</md-checkbox>
249+
<md-input-container [hideRequiredMarker]="hideRequiredMarker" style="width: 300px">
250+
<input mdInput
251+
required
252+
[placeholder]="hideRequiredMarker ? 'Required Without Marker' : 'Required With Marker'">
253+
</md-input-container>
254+
</p>
247255
<p>
248256
<md-button-toggle-group [(ngModel)]="floatingLabel">
249257
<md-button-toggle value="auto">Auto Float</md-button-toggle>

src/demo-app/input/input-demo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class InputDemo {
1616
floatingLabel: string = 'auto';
1717
color: boolean;
1818
requiredField: boolean;
19+
hideRequiredMarker: boolean;
1920
ctrlDisabled = false;
2021

2122
name: string;

src/lib/input/input-container.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*ngIf="_hasPlaceholder()">
1919
<ng-content select="md-placeholder, mat-placeholder"></ng-content>
2020
{{_mdInputChild.placeholder}}
21-
<span class="mat-placeholder-required" *ngIf="_mdInputChild.required">*</span>
21+
<span class="mat-placeholder-required" *ngIf="!hideRequiredMarker && _mdInputChild.required">*</span>
2222
</label>
2323
</span>
2424
</div>

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,20 @@ describe('MdInputContainer', function () {
371371
expect(el.nativeElement.textContent).toMatch(/hello\s+\*/g);
372372
});
373373

374+
it('hide placeholder required star when set to hide the required marker', () => {
375+
let fixture = TestBed.createComponent(MdInputContainerPlaceholderRequiredTestComponent);
376+
fixture.detectChanges();
377+
378+
let el = fixture.debugElement.query(By.css('label'));
379+
expect(el).not.toBeNull();
380+
expect(el.nativeElement.textContent).toMatch(/hello\s+\*/g);
381+
382+
fixture.componentInstance.hideRequiredMarker = true;
383+
fixture.detectChanges();
384+
385+
expect(el.nativeElement.textContent).toMatch(/hello/g);
386+
});
387+
374388
it('supports the disabled attribute as binding', async(() => {
375389
const fixture = TestBed.createComponent(MdInputContainerWithDisabled);
376390
fixture.detectChanges();
@@ -741,9 +755,13 @@ class MdInputContainerWithType {
741755
}
742756

743757
@Component({
744-
template: `<md-input-container><input mdInput required placeholder="hello"></md-input-container>`
758+
template: `<md-input-container [hideRequiredMarker]="hideRequiredMarker">
759+
<input mdInput required placeholder="hello">
760+
</md-input-container>`
745761
})
746-
class MdInputContainerPlaceholderRequiredTestComponent {}
762+
class MdInputContainerPlaceholderRequiredTestComponent {
763+
hideRequiredMarker: boolean;
764+
}
747765

748766
@Component({
749767
template: `

src/lib/input/input-container.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ export class MdInputContainer implements AfterViewInit, AfterContentInit {
297297
get dividerColor() { return this.color; }
298298
set dividerColor(value) { this.color = value; }
299299

300+
/** Whether we should hide the required marker. */
301+
@Input()
302+
get hideRequiredMarker() { return this._hideRequiredMarker; }
303+
set hideRequiredMarker(value: any) {
304+
this._hideRequiredMarker = coerceBooleanProperty(value);
305+
}
306+
private _hideRequiredMarker: boolean;
307+
300308
/** Whether the floating label should always float or not. */
301309
get _shouldAlwaysFloat() { return this._floatPlaceholder === 'always'; }
302310

0 commit comments

Comments
 (0)