Skip to content

Commit 0c72567

Browse files
committed
sample(timepicker): update timepicker sample
1 parent b05b487 commit 0c72567

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ <h4 class="sample-title">Angular Time Picker with Reactive Form</h4>
88
formControlName="timePicker"
99
[mode]="properties.mode"
1010
[type]="properties.type"
11-
[value]="properties.value"
1211
[inputFormat]="properties.inputFormat"
1312
[displayFormat]="properties.displayFormat"
1413
[required]="properties.required"

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

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { Component, DestroyRef, inject, TemplateRef, ViewChild, OnInit } from '@
22
import { FormsModule, ReactiveFormsModule, UntypedFormBuilder, Validators } from '@angular/forms';
33
import {
44
IgxTimePickerComponent,
5-
IgxInputDirective,
6-
AutoPositionStrategy,
7-
OverlaySettings,
85
DatePart,
96
IgxHintDirective,
107
IgxButtonDirective,
@@ -50,9 +47,6 @@ export class TimePickerSampleComponent implements OnInit {
5047
@ViewChild('tp', { read: IgxTimePickerComponent, static: true })
5148
public tp: IgxTimePickerComponent;
5249

53-
@ViewChild('target')
54-
public target: IgxInputDirective;
55-
5650
@ViewChild('customControls', { static: true })
5751
public customControlsTemplate!: TemplateRef<any>;
5852

@@ -62,7 +56,6 @@ export class TimePickerSampleComponent implements OnInit {
6256
public hasHint = false;
6357

6458
public itemsDelta = { hours: 1, minutes: 15, seconds: 20 };
65-
public format = 'hh:mm:ss:SS a';
6659
public spinLoop = true;
6760
public datePart = DatePart.Hours;
6861

@@ -73,14 +66,6 @@ export class TimePickerSampleComponent implements OnInit {
7366
public val = '08:30:00';
7467
public today = new Date(Date.now());
7568

76-
public isRequired = true;
77-
78-
public myOverlaySettings: OverlaySettings = {
79-
modal: false,
80-
closeOnOutsideClick: true,
81-
positionStrategy: new AutoPositionStrategy()
82-
};
83-
8469
public panelConfig: PropertyPanelConfig = {
8570
size: {
8671
control: {
@@ -160,8 +145,9 @@ export class TimePickerSampleComponent implements OnInit {
160145
})
161146
) as Properties;
162147

148+
// FormControl owns the time picker value
163149
public reactiveForm = this.fb.group({
164-
timePicker: [this.properties?.value || ''],
150+
timePicker: [null],
165151
});
166152

167153
constructor() {
@@ -171,10 +157,7 @@ export class TimePickerSampleComponent implements OnInit {
171157
this.propertyChangeService.propertyChanges.subscribe(
172158
(properties) => {
173159
this.properties = properties;
174-
this.reactiveForm.patchValue({
175-
timePicker: properties?.value || ''
176-
});
177-
this.updateRequiredValidator();
160+
this.syncFormControlFromProperties();
178161
}
179162
);
180163

@@ -187,19 +170,55 @@ export class TimePickerSampleComponent implements OnInit {
187170
);
188171
}
189172

190-
private updateRequiredValidator(): void {
173+
/**
174+
* Syncs the reactive form control with the properties panel:
175+
* - programmatic value updates
176+
* - required validator
177+
* - disabled state
178+
*
179+
* All done in a way that does NOT mark the control dirty/touched.
180+
*/
181+
private syncFormControlFromProperties(): void {
191182
const control = this.reactiveForm.get('timePicker');
192-
if (control) {
193-
control.setValidators(this.properties?.required ? Validators.required : null);
194-
control.updateValueAndValidity();
183+
if (!control) {
184+
return;
195185
}
196-
}
197186

198-
public change() {
199-
this.isRequired = !this.isRequired;
200-
}
187+
// 1) Programmatic value update (from properties.value)
188+
// This does NOT mark the control dirty/touched.
189+
if ('value' in this.properties) {
190+
const newValue = this.properties.value ?? null;
191+
const currentValue = control.value;
192+
193+
// Shallow equality check to avoid unnecessary writes
194+
const sameValue =
195+
(newValue === currentValue) ||
196+
(newValue instanceof Date &&
197+
currentValue instanceof Date &&
198+
newValue.getTime() === currentValue.getTime());
199+
200+
if (!sameValue) {
201+
control.setValue(newValue, { emitEvent: false });
202+
}
203+
}
204+
205+
// 2) Required validator - set without triggering validation
206+
const currentValidators = control.validator;
207+
const newValidators = this.properties?.required ? Validators.required : null;
201208

202-
public valueChanged(event) {
209+
// Only update validators if they actually changed
210+
if (currentValidators !== newValidators) {
211+
control.setValidators(newValidators);
212+
// Don't call updateValueAndValidity - let natural form lifecycle handle validation
213+
}
214+
215+
// 3) Disabled state
216+
if (this.properties?.disabled) {
217+
control.disable({ emitEvent: false });
218+
} else {
219+
control.enable({ emitEvent: false });
220+
}
221+
} public valueChanged(event) {
203222
console.log(event);
204223
}
205224

0 commit comments

Comments
 (0)