Skip to content

Commit 94e7b35

Browse files
authoredAug 29, 2022
fix(timepicker): add exportAs property for ngbTimepicker (#3980)
1 parent 014e8c1 commit 94e7b35

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
 

‎src/timepicker/timepicker.spec.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ describe('ngb-timepicker', () => {
183183
}));
184184
});
185185

186-
187186
describe('model updates in response to increment / decrement button clicks', () => {
188187

189188
it('should increment / decrement hours', fakeAsync(() => {
@@ -1623,6 +1622,32 @@ describe('ngb-timepicker', () => {
16231622
expectToDisplayTime(fixture.nativeElement, '13:30');
16241623
}));
16251624
});
1625+
1626+
describe('on export', () => {
1627+
1628+
it('should change active time by calling change on an exported directive instance', fakeAsync(() => {
1629+
const html = `
1630+
<ngb-timepicker #myTimepicker="ngbTimepicker" [ngModel]="model"></ngb-timepicker>
1631+
<button type="button" id="hours" (click)="myTimepicker.changeHour(1)"></button>
1632+
<button type="button" id="minutes" (click)="myTimepicker.changeMinute(1)"></button>`;
1633+
1634+
const fixture = createTestComponent(html);
1635+
fixture.componentInstance.model = {hour: 1, minute: 23, second: 45};
1636+
fixture.detectChanges();
1637+
tick();
1638+
fixture.detectChanges();
1639+
1640+
const buttonChangeHours = fixture.nativeElement.querySelector('#hours') as HTMLButtonElement;
1641+
buttonChangeHours.click();
1642+
fixture.detectChanges();
1643+
expectToDisplayTime(fixture.nativeElement, '02:23');
1644+
1645+
const buttonChangeMinutes = fixture.nativeElement.querySelector('#minutes') as HTMLButtonElement;
1646+
buttonChangeMinutes.click();
1647+
fixture.detectChanges();
1648+
expectToDisplayTime(fixture.nativeElement, '02:24');
1649+
}));
1650+
});
16261651
});
16271652

16281653

‎src/timepicker/timepicker.ts

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const FILTER_REGEX = /[^0-9]/g;
2121
* A directive that helps with wth picking hours, minutes and seconds.
2222
*/
2323
@Component({
24+
exportAs: 'ngbTimepicker',
2425
selector: 'ngb-timepicker',
2526
encapsulation: ViewEncapsulation.None,
2627
styleUrls: ['./timepicker.scss'],
@@ -209,21 +210,33 @@ export class NgbTimepicker implements ControlValueAccessor,
209210

210211
setDisabledState(isDisabled: boolean) { this.disabled = isDisabled; }
211212

213+
/**
214+
* Increments the hours by the given step.
215+
*/
212216
changeHour(step: number) {
213217
this.model ?.changeHour(step);
214218
this.propagateModelChange();
215219
}
216220

221+
/**
222+
* Increments the minutes by the given step.
223+
*/
217224
changeMinute(step: number) {
218225
this.model ?.changeMinute(step);
219226
this.propagateModelChange();
220227
}
221228

229+
/**
230+
* Increments the seconds by the given step.
231+
*/
222232
changeSecond(step: number) {
223233
this.model ?.changeSecond(step);
224234
this.propagateModelChange();
225235
}
226236

237+
/**
238+
* Update hours with the new value.
239+
*/
227240
updateHour(newVal: string) {
228241
const isPM = this.model ? this.model.hour >= 12 : false;
229242
const enteredHour = toInteger(newVal);
@@ -235,11 +248,17 @@ export class NgbTimepicker implements ControlValueAccessor,
235248
this.propagateModelChange();
236249
}
237250

251+
/**
252+
* Update minutes with the new value.
253+
*/
238254
updateMinute(newVal: string) {
239255
this.model ?.updateMinute(toInteger(newVal));
240256
this.propagateModelChange();
241257
}
242258

259+
/**
260+
* Update seconds with the new value.
261+
*/
243262
updateSecond(newVal: string) {
244263
this.model ?.updateSecond(toInteger(newVal));
245264
this.propagateModelChange();

0 commit comments

Comments
 (0)
Please sign in to comment.