Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Commit

Permalink
feat(textfield): Add empty getter (#451)
Browse files Browse the repository at this point in the history
Add empty() property to check if value is empty and isBadInput is false
  • Loading branch information
trimox authored Dec 12, 2017
1 parent eaf2c17 commit 4c6124d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/lib/textfield/text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ export class MdcTextField implements AfterViewInit, OnDestroy, ControlValueAcces
get valid(): boolean {
return this.inputText.nativeElement.validity.valid;
}

/** Whether the control is empty. */
get empty(): boolean {
return !this.elementRef.nativeElement.value && !this.isBadInput();
}

@HostBinding('class.mdc-text-field--dense') get classDense(): string {
return this.dense ? 'mdc-text-field--dense' : '';
}
Expand Down Expand Up @@ -294,7 +300,8 @@ export class MdcTextField implements AfterViewInit, OnDestroy, ControlValueAcces
}

isBadInput(): boolean {
return this.inputText.nativeElement.validity.badInput;
const validity = (this.elementRef.nativeElement as HTMLInputElement).validity;
return validity && validity.badInput;
}

focus(): void {
Expand Down
5 changes: 5 additions & 0 deletions test/unit/textfield/textfield.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ describe('MdcTextField', () => {
expect(textFieldInstance.isDisabled()).toBe(false);
});

it('#should not be empty', () => {
fixture.detectChanges();
expect(textFieldInstance.empty).toBe(true);
});

it('#should remove invalid styling', () => {
fixture.detectChanges();
textFieldInstance.setValid(false);
Expand Down

0 comments on commit 4c6124d

Please sign in to comment.