Skip to content

Commit

Permalink
fix(timepicker): no placeholder for default timeformat (#164161) (#67)
Browse files Browse the repository at this point in the history
* fix: No placeholder for default timeFormat

* add: Tests for placeholder
  • Loading branch information
RistiCore authored and lskramarov committed Nov 7, 2018
1 parent 6701b24 commit 02803ba
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/lib/timepicker/timepicker.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export enum TimeFormats {
}

export const TIMEFORMAT_PLACEHOLDERS: { [timeFormat: string]: string } = {
'hh:mm:ss': ' : : ',
'hh:mm': ' : '
[TimeFormats.HHmmss]: ' : : ',
[TimeFormats.HHmm]: ' : '
};

export const DEFAULT_TIME_FORMAT: TimeFormats = TimeFormats.HHmm;
Expand Down
68 changes: 47 additions & 21 deletions src/lib/timepicker/timepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,53 @@ describe('McTimepicker', () => {
let testComponent: TestApp;
let inputElementDebug;

it('Timepicker disabled state switching on/off', () => {
fixture = TestBed.createComponent(TestApp);
testComponent = fixture.debugElement.componentInstance;
inputElementDebug = fixture.debugElement.query(By.directive(McTimepicker));

testComponent.isDisabled = true;
fixture.detectChanges();

return fixture.whenStable()
.then(() => {
fixture.detectChanges();
expect(inputElementDebug.nativeElement.disabled).toBe(true, 'input not disabled');
testComponent.isDisabled = false;
fixture.detectChanges();

return fixture.whenStable();
})
.then(() => {
fixture.detectChanges();
expect(inputElementDebug.nativeElement.disabled).toBe(false, 'input not disabled');
});
describe('Core attributes support', () => {
beforeEach(() => {
fixture = TestBed.createComponent(TestApp);
testComponent = fixture.debugElement.componentInstance;
inputElementDebug = fixture.debugElement.query(By.directive(McTimepicker));

testComponent.timeValue = new Date('1970-01-01 12:18:28');
fixture.detectChanges();
});

it('Timepicker disabled state switching on/off', () => {
testComponent.isDisabled = true;
fixture.detectChanges();

return fixture.whenStable()
.then(() => {
fixture.detectChanges();
expect(inputElementDebug.nativeElement.disabled).toBe(true, 'input not disabled');
testComponent.isDisabled = false;
fixture.detectChanges();

return fixture.whenStable();
})
.then(() => {
fixture.detectChanges();
expect(inputElementDebug.nativeElement.disabled).toBe(false, 'input not disabled');
});
});

it('Placeholder set on default timeFormat', () => {
return fixture.whenStable()
.then(() => {
fixture.detectChanges();
expect(inputElementDebug.nativeElement.placeholder).toBe(' : ');
});
});

it('Correct placeholder set for non-default time format', () => {
testComponent.timeFormat = 'HH:mm:ss';
fixture.detectChanges();

return fixture.whenStable()
.then(() => {
fixture.detectChanges();
expect(inputElementDebug.nativeElement.placeholder).toBe(' : : ');
});
});
});

describe('Timerange validation', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/timepicker/timepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class McTimepicker extends McTimepickerMixinBase
.indexOf(formatValue) > -1 ? formatValue : DEFAULT_TIME_FORMAT;

validatorOnChange();
this.placeholder = TIMEFORMAT_PLACEHOLDERS[this._timeFormat.toLowerCase()];
this.placeholder = TIMEFORMAT_PLACEHOLDERS[this._timeFormat];
}

@Input('min-time')
Expand Down Expand Up @@ -260,6 +260,8 @@ export class McTimepicker extends McTimepickerMixinBase
// Force setter to be called in case id was not specified.
this.id = this.id;

this.placeholder = TIMEFORMAT_PLACEHOLDERS[DEFAULT_TIME_FORMAT];

// Instead of NG_VALUE_ACCESSOR (https://github.com/angular/material2/issues/8158#issuecomment-344618103)
if (this.ngControl) { this.ngControl.valueAccessor = this; }

Expand Down

0 comments on commit 02803ba

Please sign in to comment.