Skip to content

Commit

Permalink
Merge pull request #203 from paullryan/issue-201
Browse files Browse the repository at this point in the history
Add support step in textfields
  • Loading branch information
mseemann authored Nov 14, 2016
2 parents 26bd53e + bff2482 commit 4c03663
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/demo-app/app/textfield/textfield.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,36 @@ <h5>Text/Numeric with floating label</h5>
]]>
</pre>

<h5>Number with min and step restrictions</h5>

<mdl-textfield
name="number1"
type="number"
label="Number..."
min="0"
step="0.01"
error-msg="Input is not a number!"
[(ngModel)]="number1"
floating-label ></mdl-textfield>

<br/>
{{number1}}

<pre prism>
<![CDATA[
<mdl-textfield
name="number1"
type="number"
label="Number..."
min="0"
step="0.01"
error-msg="Input is not a number!"
[(ngModel)]="number1"
floating-label ></mdl-textfield>
]]>
</pre>


<h5>Multiline Textfield</h5>
aka Textarea. Add the attribute row to turn the input field to a textarea. Specify maxrows to restrict the
possible lines.<br/>
Expand Down Expand Up @@ -173,6 +203,12 @@ <h5>Attributes of the mdl-textfield component</h5>
<td>Restrict the value for type number to a min value.</td>
</tr>

<tr>
<td>step</td>
<td>Restrict the value for type number to a be based on the given step (e.g. 0.01
for numbers with two decimal places).</td>
</tr>

<tr>
<td>ngModel</td>
<td>The text field is ngModel compatible.</td>
Expand Down
21 changes: 21 additions & 0 deletions src/lib/components/textfield/mdl-textfield.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ describe('Component: MdlTextField', () => {
});
}));


it('should mark the component as invalid ngModel (step)', async(() => {

TestBed.overrideComponent(MdlTestComponent, { set: {
template: '<mdl-textfield type="number" label="Text..." [(ngModel)]="text1" min="0" step="1"></mdl-textfield>' }
});
let fixture = TestBed.createComponent(MdlTestComponent);
fixture.detectChanges();

let hostEl: HTMLElement = fixture.debugElement.query(By.directive(MdlTextFieldComponent)).nativeElement;
let el: HTMLInputElement = fixture.debugElement.query(By.css('input')).nativeElement;

el.value = '0.1';
fixture.detectChanges();

fixture.whenStable().then( () => {
expect(hostEl.classList.contains('is-invalid')).toBe(true);
});
}));


it('should create a textarea if row is specified', () => {

TestBed.overrideComponent(MdlTestComponent, { set: {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/components/textfield/mdl-textfield.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const IS_DIRTY = 'is-dirty';
[pattern]="pattern ? pattern : '.*'"
[attr.min]="min"
[attr.max]="max"
[attr.step]="step"
[placeholder]="placeholder ? placeholder : ''"
[autocomplete]="autocomplete ? autocomplete : ''"
(focus)="onFocus($event)"
Expand All @@ -102,6 +103,7 @@ const IS_DIRTY = 'is-dirty';
[pattern]="pattern ? pattern : '.*'"
[attr.min]="min"
[attr.max]="max"
[attr.step]="step"
[placeholder]="placeholder ? placeholder : ''"
[autocomplete]="autocomplete ? autocomplete : ''"
(focus)="onFocus($event)"
Expand Down Expand Up @@ -146,6 +148,7 @@ export class MdlTextFieldComponent implements ControlValueAccessor, OnChanges, D
@Input() public pattern;
@Input() public min;
@Input() public max;
@Input() public step;
@Input() public name;
@Input() public id = `mdl-textfield-${nextId++}`;
@Input('error-msg') public errorMessage;
Expand Down

0 comments on commit 4c03663

Please sign in to comment.