Skip to content

Commit 756258c

Browse files
authored
Merge pull request #8061 from IgniteUI/valadzhov/time-picker-label-6905
Implement label for the time picker
2 parents 2475539 + 56de9f1 commit 756258c

File tree

4 files changed

+88
-33
lines changed

4 files changed

+88
-33
lines changed

projects/igniteui-angular/src/lib/time-picker/time-picker.component.html

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
<ng-template #labelTemplate>
2+
<ng-content select="[igxLabel]"></ng-content>
3+
</ng-template>
4+
15
<ng-template #dropdownInputTemplate>
26
<igx-input-group #group (mousedown)="mouseDown($event)">
3-
<label igxLabel>Time</label>
7+
<label igxLabel *ngIf="!labelDirective">Time</label>
8+
<ng-container ngProjectAs="[igxLabel]" *ngTemplateOutlet="labelTemplate"></ng-container>
49
<igx-prefix (click)="openDialog(group.element.nativeElement)">
510
<igx-icon>access_time</igx-icon>
611
</igx-prefix>
@@ -30,7 +35,8 @@
3035
<igx-prefix>
3136
<igx-icon>access_time</igx-icon>
3237
</igx-prefix>
33-
<label igxLabel>Time</label>
38+
<label igxLabel *ngIf="!labelDirective">Time</label>
39+
<ng-container ngProjectAs="[igxLabel]" *ngTemplateOutlet="labelTemplate"></ng-container>
3440
<input
3541
igxInput
3642
[value]="displayTime || ''"

projects/igniteui-angular/src/lib/time-picker/time-picker.component.spec.ts

+72-31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IgxLabelDirective } from './../directives/label/label.directive';
12
import { Component, ViewChild, NgModule, ElementRef, EventEmitter, DebugElement } from '@angular/core';
23
import { async, TestBed, fakeAsync, tick, ComponentFixture } from '@angular/core/testing';
34
import { FormsModule, FormGroup, FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@@ -46,6 +47,32 @@ describe('IgxTimePicker', () => {
4647
expect(domTimePicker.id).toBe('customTimePicker');
4748
}));
4849

50+
it('Should display default and custom label', (() => {
51+
const fixture = TestBed.createComponent(IgxTimePickerCustomLabelComponent);
52+
const testComponent = fixture.componentInstance;
53+
testComponent.mode = InteractionMode.DropDown;
54+
fixture.detectChanges();
55+
56+
const dom = fixture.debugElement;
57+
let label = dom.query(By.directive(IgxLabelDirective)).nativeElement.innerText;
58+
expect(label).toEqual('Custom label');
59+
60+
testComponent.customLabel = false;
61+
fixture.detectChanges();
62+
label = dom.query(By.directive(IgxLabelDirective)).nativeElement.innerText;
63+
expect(label).toEqual('Time');
64+
65+
testComponent.mode = InteractionMode.Dialog;
66+
fixture.detectChanges();
67+
label = dom.query(By.directive(IgxLabelDirective)).nativeElement.innerText;
68+
expect(label).toEqual('Time');
69+
70+
testComponent.customLabel = true;
71+
fixture.detectChanges();
72+
label = dom.query(By.directive(IgxLabelDirective)).nativeElement.innerText;
73+
expect(label).toEqual('Custom label');
74+
}));
75+
4976
it('@Input properties', (() => {
5077
const fixture = TestBed.createComponent(IgxTimePickerWithPassedTimeComponent);
5178
fixture.detectChanges();
@@ -1640,34 +1667,34 @@ describe('IgxTimePicker', () => {
16401667
}));
16411668

16421669
it('When timepicker is closed via outside click, the focus should NOT remain on the input',
1643-
fakeAsync(() => {
1644-
fixture.detectChanges();
1645-
input = fixture.debugElement.query(By.directive(IgxInputDirective)).nativeElement;
1646-
let overlayToggle = document.getElementsByClassName('igx-overlay__wrapper');
1670+
fakeAsync(() => {
1671+
fixture.detectChanges();
1672+
input = fixture.debugElement.query(By.directive(IgxInputDirective)).nativeElement;
1673+
let overlayToggle = document.getElementsByClassName('igx-overlay__wrapper');
16471674

1648-
expect(overlayToggle.length).toEqual(0);
1675+
expect(overlayToggle.length).toEqual(0);
16491676

1650-
const iconTime = dom.queryAll(By.css('.igx-icon'))[0];
1651-
UIInteractions.simulateClickAndSelectEvent(iconTime);
1652-
tick();
1653-
fixture.detectChanges();
1677+
const iconTime = dom.queryAll(By.css('.igx-icon'))[0];
1678+
UIInteractions.simulateClickAndSelectEvent(iconTime);
1679+
tick();
1680+
fixture.detectChanges();
16541681

1655-
overlayToggle = document.getElementsByClassName('igx-overlay__wrapper');
1656-
expect(overlayToggle[0]).not.toBeNull();
1657-
expect(overlayToggle[0]).not.toBeUndefined();
1682+
overlayToggle = document.getElementsByClassName('igx-overlay__wrapper');
1683+
expect(overlayToggle[0]).not.toBeNull();
1684+
expect(overlayToggle[0]).not.toBeUndefined();
16581685

1659-
const dummyInput = fixture.componentInstance.dummyInput.nativeElement;
1660-
dummyInput.focus();
1661-
dummyInput.click();
1662-
tick();
1663-
fixture.detectChanges();
1686+
const dummyInput = fixture.componentInstance.dummyInput.nativeElement;
1687+
dummyInput.focus();
1688+
dummyInput.click();
1689+
tick();
1690+
fixture.detectChanges();
16641691

1665-
overlayToggle = document.getElementsByClassName('igx-overlay__wrapper');
1666-
input = fixture.debugElement.query(By.directive(IgxInputDirective)).nativeElement;
1667-
expect(overlayToggle[0]).toEqual(undefined);
1668-
expect(input).not.toEqual(document.activeElement);
1669-
expect(dummyInput).toEqual(document.activeElement);
1670-
}));
1692+
overlayToggle = document.getElementsByClassName('igx-overlay__wrapper');
1693+
input = fixture.debugElement.query(By.directive(IgxInputDirective)).nativeElement;
1694+
expect(overlayToggle[0]).toEqual(undefined);
1695+
expect(input).not.toEqual(document.activeElement);
1696+
expect(dummyInput).toEqual(document.activeElement);
1697+
}));
16711698

16721699
it('should apply disabled style for time outside the min and max values', fakeAsync(() => {
16731700
timePicker = new IgxTimePickerComponent(null, null);
@@ -2080,7 +2107,7 @@ describe('IgxTimePicker', () => {
20802107

20812108
expect(inputDirective.valid).toEqual(IgxInputState.INITIAL);
20822109

2083-
inputDirectiveElement.triggerEventHandler('focus', { target: { value: null }});
2110+
inputDirectiveElement.triggerEventHandler('focus', { target: { value: null } });
20842111
tick(16);
20852112
fixture.detectChanges();
20862113

@@ -2094,7 +2121,7 @@ describe('IgxTimePicker', () => {
20942121
fixture.detectChanges();
20952122
expect(inputDirective.valid).toEqual(IgxInputState.INITIAL);
20962123

2097-
inputDirectiveElement.triggerEventHandler('blur', { target: { value: null}});
2124+
inputDirectiveElement.triggerEventHandler('blur', { target: { value: null } });
20982125
fixture.detectChanges();
20992126

21002127
expect(inputDirective.valid).toEqual(IgxInputState.INVALID);
@@ -2132,7 +2159,7 @@ describe('IgxTimePicker', () => {
21322159
nativeElement: { getBoundingClientRect: () => 0 },
21332160
style: { width: null }
21342161
};
2135-
cdr = { detectChanges: () => {}};
2162+
cdr = { detectChanges: () => { } };
21362163
toggleRef = {
21372164
onOpened: new EventEmitter<any>(),
21382165
onClosed: new EventEmitter<any>(),
@@ -2147,7 +2174,7 @@ describe('IgxTimePicker', () => {
21472174
const timePicker = new IgxTimePickerComponent(injector, cdr);
21482175
timePicker['_inputGroup'] = inputGroup;
21492176
timePicker['toggleRef'] = toggleRef;
2150-
ngModel.control.validator = () => ({ required: true});
2177+
ngModel.control.validator = () => ({ required: true });
21512178
timePicker.ngOnInit();
21522179
timePicker.ngAfterViewInit();
21532180
timePicker.ngAfterViewChecked();
@@ -2167,11 +2194,11 @@ describe('IgxTimePicker', () => {
21672194
expect(timePicker).toBeDefined();
21682195
expect(inputGroup.isRequired).toBeFalsy();
21692196

2170-
ngModel.control.validator = () => ({ required: true});
2197+
ngModel.control.validator = () => ({ required: true });
21712198
ngModel.statusChanges.emit();
21722199
expect(inputGroup.isRequired).toBeTruthy();
21732200

2174-
ngModel.control.validator = () => ({ required: false});
2201+
ngModel.control.validator = () => ({ required: false });
21752202
ngModel.statusChanges.emit();
21762203
expect(inputGroup.isRequired).toBeFalsy();
21772204
});
@@ -2286,6 +2313,19 @@ export class IgxTimePickerWithItemsDeltaValueComponent {
22862313
})
22872314
export class IgxTimePickerRetemplatedComponent { }
22882315

2316+
@Component({
2317+
template: `
2318+
<igx-time-picker [mode]="mode">
2319+
<label igxLabel *ngIf="customLabel">Custom label</label>
2320+
</igx-time-picker>
2321+
`
2322+
})
2323+
export class IgxTimePickerCustomLabelComponent {
2324+
public customLabel = true;
2325+
public mode = InteractionMode.DropDown;
2326+
}
2327+
2328+
22892329
@Component({
22902330
template: `
22912331
<input class="dummyInput" #dummyInput/>
@@ -2395,7 +2435,7 @@ export class IgxTimePickerReactiveFormComponent {
23952435
constructor(fb: FormBuilder) {
23962436
this.reactiveForm = fb.group({
23972437
timePickerOnChange: [null, Validators.required],
2398-
timePickerOnBlur: [null, { updateOn: 'blur', validators: Validators.required}]
2438+
timePickerOnBlur: [null, { updateOn: 'blur', validators: Validators.required }]
23992439
});
24002440
}
24012441
onSubmitReactive() { }
@@ -2417,7 +2457,8 @@ export class IgxTimePickerReactiveFormComponent {
24172457
IgxTimePickerDropDownNoValueComponent,
24182458
IgxTimePickerRetemplatedDropDownComponent,
24192459
IgxTimePickerWithOutletComponent,
2420-
IgxTimePickerReactiveFormComponent
2460+
IgxTimePickerReactiveFormComponent,
2461+
IgxTimePickerCustomLabelComponent
24212462
],
24222463
imports: [
24232464
IgxTimePickerModule,

projects/igniteui-angular/src/lib/time-picker/time-picker.component.ts

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { CurrentResourceStrings } from '../core/i18n/resources';
5151
import { KEYS, CancelableBrowserEventArgs, IBaseEventArgs } from '../core/utils';
5252
import { InteractionMode } from '../core/enums';
5353
import { IgxTextSelectionModule} from '../directives/text-selection/text-selection.directive';
54+
import { IgxLabelDirective } from '../directives/label/label.directive';
5455

5556

5657
let NEXT_ID = 0;
@@ -505,6 +506,12 @@ export class IgxTimePickerComponent implements
505506
@ContentChild(IgxTimePickerActionsDirective, { read: IgxTimePickerActionsDirective })
506507
public timePickerActionsDirective: IgxTimePickerActionsDirective;
507508

509+
/**
510+
* @hidden @internal
511+
*/
512+
@ContentChild(IgxLabelDirective)
513+
public labelDirective: IgxLabelDirective;
514+
508515
/**
509516
* @hidden
510517
*/

src/app/time-picker/time-picker.sample.html

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ <h4 class="sample-title">Time Picker with Dropdown</h4>
66
<igx-time-picker #tp (onValueChanged)="valueChanged($event)" [minValue]="'9:15:20 AM'" [maxValue]="'11:15:20 AM'"
77
(onValidationFailed)="validationFailed($event)" [mode]="mode" [isSpinLoop]="isSpinLoop"
88
[(ngModel)]="date" [itemsDelta]="itemsDelta" [format]="format">
9+
<label igxLabel>Custom label</label>
910
</igx-time-picker>
1011
</div>
1112
</article>

0 commit comments

Comments
 (0)