Skip to content

Commit a974a11

Browse files
committed
refactor: Date editors with formValue
1 parent 0ca9c7d commit a974a11

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

src/components/common/utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class FormAssociatedTestBed<T extends IgcFormControl> {
103103
* If the `waitForUpdate` parameter is `true`, the function waits for the element to be updated before returning.
104104
*/
105105
public async setProperties(
106-
props: { [K in keyof T]?: T[K] },
106+
props: { [K in keyof T]?: T[K] | string },
107107
waitForUpdate = true
108108
): Promise<void> {
109109
Object.assign(this.element, props);

src/components/date-picker/date-picker.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import { IgcBaseComboBoxLikeComponent } from '../common/mixins/combo-box.js';
2929
import type { AbstractConstructor } from '../common/mixins/constructor.js';
3030
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
3131
import { FormAssociatedRequiredMixin } from '../common/mixins/forms/associated-required.js';
32+
import {
33+
type FormValue,
34+
createFormValueState,
35+
} from '../common/mixins/forms/form-value.js';
3236
import { createCounter, findElementFromEventPath } from '../common/util.js';
3337
import IgcDateTimeInputComponent from '../date-time-input/date-time-input.js';
3438
import type { DatePart } from '../date-time-input/date-util.js';
@@ -169,7 +173,6 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
169173
);
170174
}
171175

172-
private _value: Date | null = null;
173176
private _activeDate: Date | null = null;
174177
private _min: Date | null = null;
175178
private _max: Date | null = null;
@@ -204,6 +207,8 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
204207
return getThemeController(this)?.theme === 'material';
205208
}
206209

210+
protected override _formValue: FormValue<Date | null>;
211+
207212
/**
208213
* Sets the state of the datepicker dropdown.
209214
* @attr
@@ -246,13 +251,12 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
246251
*/
247252
@property({ converter: convertToDate })
248253
public set value(value: Date | string | null | undefined) {
249-
this._value = convertToDate(value);
250-
this._setFormValue(getDateFormValue(this._value));
254+
this._formValue.setValueAndFormState(value as Date | null);
251255
this._validate();
252256
}
253257

254258
public get value(): Date | null {
255-
return this._value;
259+
return this._formValue.value;
256260
}
257261

258262
/**
@@ -428,6 +432,15 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
428432
constructor() {
429433
super();
430434

435+
this._formValue = createFormValueState<Date | null>(this, {
436+
initialValue: null,
437+
transformers: {
438+
setValue: convertToDate,
439+
setDefaultValue: convertToDate,
440+
setFormValue: getDateFormValue,
441+
},
442+
});
443+
431444
this.addEventListener('focusin', this.handleFocusIn);
432445
this.addEventListener('focusout', this.handleFocusOut);
433446

@@ -547,7 +560,7 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
547560
if (this.readOnly) {
548561
// Wait till the calendar finishes updating and then restore the current value from the date-picker.
549562
await this._calendar.updateComplete;
550-
this._calendar.value = this._value;
563+
this._calendar.value = this.value;
551564
return;
552565
}
553566

@@ -582,10 +595,6 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
582595
event.stopPropagation();
583596
}
584597

585-
protected override _setDefaultValue(current: string | null) {
586-
this.defaultValue = convertToDate(current);
587-
}
588-
589598
private setDateConstraints() {
590599
const dates: DateRangeDescriptor[] = [];
591600
if (this._min) {

src/components/date-time-input/date-time-input.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { html } from 'lit';
2-
import { property } from 'lit/decorators.js';
2+
import { eventOptions, property } from 'lit/decorators.js';
33
import { ifDefined } from 'lit/directives/if-defined.js';
44
import { live } from 'lit/directives/live.js';
55

@@ -17,6 +17,10 @@ import { watch } from '../common/decorators/watch.js';
1717
import { registerComponent } from '../common/definitions/register.js';
1818
import type { AbstractConstructor } from '../common/mixins/constructor.js';
1919
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
20+
import {
21+
type FormValue,
22+
createFormValueState,
23+
} from '../common/mixins/forms/form-value.js';
2024
import { noop, partNameMap } from '../common/util.js';
2125
import type { IgcInputComponentEventMap } from '../input/input-base.js';
2226
import {
@@ -81,8 +85,9 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
8185
return dateTimeInputValidators;
8286
}
8387

88+
protected override _formValue: FormValue<Date | null>;
89+
8490
protected _defaultMask!: string;
85-
protected _value: Date | null = null;
8691
private _oldValue: Date | null = null;
8792
private _min: Date | null = null;
8893
private _max: Date | null = null;
@@ -118,7 +123,7 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
118123
}
119124

120125
public get value(): Date | null {
121-
return this._value;
126+
return this._formValue.value;
122127
}
123128

124129
/* @tsTwoWayProperty(true, "igcChange", "detail", false) */
@@ -128,8 +133,7 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
128133
*/
129134
@property({ converter: convertToDate })
130135
public set value(value: Date | string | null | undefined) {
131-
this._value = convertToDate(value);
132-
this._setFormValue(getDateFormValue(this._value));
136+
this._formValue.setValueAndFormState(value as Date | null);
133137
this.updateMask();
134138
this._validate();
135139
}
@@ -276,6 +280,15 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
276280
constructor() {
277281
super();
278282

283+
this._formValue = createFormValueState(this, {
284+
initialValue: null,
285+
transformers: {
286+
setValue: convertToDate,
287+
setDefaultValue: convertToDate,
288+
setFormValue: getDateFormValue,
289+
},
290+
});
291+
279292
addKeybindings(this, {
280293
skip: () => this.readOnly,
281294
bindingDefaults: { preventDefault: true, triggers: ['keydownRepeat'] },
@@ -464,6 +477,7 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
464477
return newDate;
465478
}
466479

480+
@eventOptions({ passive: false })
467481
private async onWheel(event: WheelEvent) {
468482
if (!this.focused || this.readOnly) {
469483
return;
@@ -587,7 +601,7 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
587601
this._oldValue = this.value;
588602
const areFormatsDifferent = this.displayFormat !== this.inputFormat;
589603

590-
if (!this._value) {
604+
if (!this.value) {
591605
this.maskedValue = this.emptyMask;
592606
await this.updateComplete;
593607
this.select();

0 commit comments

Comments
 (0)