Skip to content

Commit 197d659

Browse files
r-farkhutdinovRuslan FarkhutdinovCopilot
authored
TimeView: Improve types (#32093) (#32116)
Signed-off-by: Ruslan Farkhutdinov <[email protected]> Co-authored-by: Ruslan Farkhutdinov <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 46485a4 commit 197d659

File tree

1 file changed

+74
-42
lines changed

1 file changed

+74
-42
lines changed

packages/devextreme/js/__internal/ui/date_box/m_time_view.ts

Lines changed: 74 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import registerComponent from '@js/core/component_registrator';
33
import type { dxElementWrapper } from '@js/core/renderer';
44
import $ from '@js/core/renderer';
55
import type { OptionChanged } from '@ts/core/widget/types';
6+
import type { KeyboardKeyDownEvent } from '@ts/events/core/m_keyboard_processor';
7+
import type { BoxItemData } from '@ts/ui/box';
68
import Box from '@ts/ui/box';
79
import type { EditorProperties } from '@ts/ui/editor/editor';
810
import Editor from '@ts/ui/editor/editor';
@@ -22,17 +24,25 @@ const TIMEVIEW_FORMAT12_AM = -1;
2224
const TIMEVIEW_FORMAT12_PM = 1;
2325
const TIMEVIEW_MINUTEARROW_CLASS = 'dx-timeview-minutearrow';
2426

25-
const rotateArrow = function ($arrow, angle, offset) {
26-
cssRotate($arrow, angle, offset);
27+
const cssRotate = ($arrow: dxElementWrapper, angle: number, offset = 0): void => {
28+
$arrow.css('transform', `rotate(${angle}deg) translate(0,${offset}px)`);
2729
};
2830

29-
const cssRotate = function ($arrow, angle, offset) {
30-
// eslint-disable-next-line no-useless-concat
31-
$arrow.css('transform', `rotate(${angle}deg)` + ` translate(0,${offset}px)`);
31+
const rotateArrow = (
32+
$arrow: dxElementWrapper | undefined,
33+
angle: number,
34+
offset?: number,
35+
): void => {
36+
if (!$arrow) {
37+
return;
38+
}
39+
40+
cssRotate($arrow, angle, offset);
3241
};
3342

34-
export interface TimeViewProperties extends EditorProperties {
43+
export interface TimeViewProperties extends Omit<EditorProperties, 'value'> {
3544
use24HourFormat?: boolean;
45+
value: Date;
3646
_showClock?: boolean;
3747
_arrowOffset?: number;
3848
}
@@ -58,7 +68,7 @@ class TimeView extends Editor<TimeViewProperties> {
5868
};
5969
}
6070

61-
_getValue() {
71+
_getValue(): Date {
6272
const { value } = this.option();
6373
return value || new Date();
6474
}
@@ -77,19 +87,21 @@ class TimeView extends Editor<TimeViewProperties> {
7787
}
7888

7989
_renderBox(): void {
90+
// eslint-disable-next-line @typescript-eslint/naming-convention
91+
const { _showClock } = this.option();
92+
8093
const $box = $('<div>').appendTo(this.$element());
81-
const items = [];
94+
const items: BoxItemData[] = [];
8295

83-
if (this.option('_showClock')) {
84-
// @ts-expect-error
96+
if (_showClock) {
8597
items.push({
8698
ratio: 1,
8799
shrink: 0,
88100
baseSize: 'auto',
89101
template: this._renderClock.bind(this),
90102
});
91103
}
92-
// @ts-expect-error
104+
93105
items.push({
94106
ratio: 0,
95107
shrink: 0,
@@ -105,7 +117,7 @@ class TimeView extends Editor<TimeViewProperties> {
105117
});
106118
}
107119

108-
_renderClock(_, __, container): void {
120+
_renderClock(_: BoxItemData, __: number, container: dxElementWrapper): void {
109121
this._$hourArrow = $('<div>').addClass(TIMEVIEW_HOURARROW_CLASS);
110122
this._$minuteArrow = $('<div>').addClass(TIMEVIEW_MINUTEARROW_CLASS);
111123

@@ -118,16 +130,19 @@ class TimeView extends Editor<TimeViewProperties> {
118130
}
119131

120132
_updateClock(): void {
133+
// eslint-disable-next-line @typescript-eslint/naming-convention
134+
const { _arrowOffset } = this.option();
135+
121136
const time = this._getValue();
122-
const hourArrowAngle = time.getHours() / 12 * 360 + time.getMinutes() / 60 * 30;
123-
const minuteArrowAngle = time.getMinutes() / 60 * 360;
137+
const hourArrowAngle = (time.getHours() / 12) * 360 + (time.getMinutes() / 60) * 30;
138+
const minuteArrowAngle = (time.getMinutes() / 60) * 360;
124139

125-
rotateArrow(this._$hourArrow, hourArrowAngle, this.option('_arrowOffset'));
126-
rotateArrow(this._$minuteArrow, minuteArrowAngle, this.option('_arrowOffset'));
140+
rotateArrow(this._$hourArrow, hourArrowAngle, _arrowOffset);
141+
rotateArrow(this._$minuteArrow, minuteArrowAngle, _arrowOffset);
127142
}
128143

129-
_getBoxItems(is12HourFormat: boolean) {
130-
const items = [{
144+
_getBoxItems(is12HourFormat: boolean): BoxItemData[] {
145+
const items: BoxItemData[] = [{
131146
ratio: 0,
132147
shrink: 0,
133148
baseSize: 'auto',
@@ -136,8 +151,8 @@ class TimeView extends Editor<TimeViewProperties> {
136151
ratio: 0,
137152
shrink: 0,
138153
baseSize: 'auto',
139-
// @ts-expect-error ts-error
140-
template: $('<div>').addClass(TIMEVIEW_TIME_SEPARATOR_CLASS).text(dateLocalization.getTimeSeparator()),
154+
// @ts-expect-error core/DateLocalization type should be fixed
155+
template: (): dxElementWrapper => $('<div>').addClass(TIMEVIEW_TIME_SEPARATOR_CLASS).text(dateLocalization.getTimeSeparator()),
141156
}, {
142157
ratio: 0,
143158
shrink: 0,
@@ -150,15 +165,16 @@ class TimeView extends Editor<TimeViewProperties> {
150165
ratio: 0,
151166
shrink: 0,
152167
baseSize: 'auto',
153-
template: () => this._format12.$element(),
168+
template: () => this._format12?.$element(),
154169
});
155170
}
156171

157172
return items;
158173
}
159174

160175
_renderField(): dxElementWrapper {
161-
const is12HourFormat = !this.option('use24HourFormat');
176+
const { use24HourFormat } = this.option();
177+
const is12HourFormat = !use24HourFormat;
162178

163179
this._createHourBox(is12HourFormat);
164180
this._createMinuteBox();
@@ -188,7 +204,7 @@ class TimeView extends Editor<TimeViewProperties> {
188204
max: is12HourFormat ? 13 : 24,
189205
value: this._getValue().getHours(),
190206
onValueChanged: this._onHourBoxValueChanged.bind(this),
191-
onKeyboardHandled: (opts) => this._keyboardHandler(opts),
207+
onKeyboardHandled: (opts: KeyboardKeyDownEvent) => this._keyboardHandler(opts),
192208
...this._getNumberBoxConfig(),
193209
},
194210
);
@@ -197,11 +213,14 @@ class TimeView extends Editor<TimeViewProperties> {
197213
}
198214

199215
_isPM(): boolean {
200-
// @ts-expect-error ts-error
201-
return !this.option('use24HourFormat') && this._format12.option('value') === 1;
216+
const { use24HourFormat } = this.option();
217+
218+
const format12Value = this._format12?.option().value;
219+
220+
return !use24HourFormat && format12Value === TIMEVIEW_FORMAT12_PM;
202221
}
203222

204-
_onHourBoxValueChanged({ value, component }) {
223+
_onHourBoxValueChanged({ value, component }: { value: number; component: NumberBox }): void {
205224
const currentValue = this._getValue();
206225
const newValue = new Date(currentValue);
207226
let newHours = this._convertMaxHourToMin(value);
@@ -217,8 +236,10 @@ class TimeView extends Editor<TimeViewProperties> {
217236
this.option('value', newValue);
218237
}
219238

220-
_convertMaxHourToMin(hours): number {
221-
const maxHoursValue = this.option('use24HourFormat') ? 24 : 12;
239+
_convertMaxHourToMin(hours: number): number {
240+
const { use24HourFormat } = this.option();
241+
242+
const maxHoursValue = use24HourFormat ? 24 : 12;
222243
return (maxHoursValue + hours) % maxHoursValue;
223244
}
224245

@@ -230,8 +251,8 @@ class TimeView extends Editor<TimeViewProperties> {
230251
min: -1,
231252
max: 60,
232253
value: this._getValue().getMinutes(),
233-
onKeyboardHandled: (opts) => this._keyboardHandler(opts),
234-
onValueChanged: ({ value, component }) => {
254+
onKeyboardHandled: (opts: KeyboardKeyDownEvent) => this._keyboardHandler(opts),
255+
onValueChanged: ({ value, component }: { value: number; component: NumberBox }) => {
235256
const newMinutes = (60 + value) % 60;
236257
component.option('value', newMinutes);
237258

@@ -248,8 +269,10 @@ class TimeView extends Editor<TimeViewProperties> {
248269
}
249270

250271
_createFormat12Box(): void {
251-
// @ts-expect-error ts-error
272+
// @ts-expect-error core/DateLocalization type should be fixed
252273
const periodNames = dateLocalization.getPeriodNames();
274+
const { stylingMode } = this.option();
275+
253276
this._format12 = this._createComponent(
254277
$('<div>').addClass(TIMEVIEW_FORMAT12_CLASS),
255278
SelectBox,
@@ -260,7 +283,7 @@ class TimeView extends Editor<TimeViewProperties> {
260283
],
261284
valueExpr: 'value',
262285
displayExpr: 'text',
263-
onKeyboardHandled: (opts) => this._keyboardHandler(opts),
286+
onKeyboardHandled: (opts: KeyboardKeyDownEvent) => this._keyboardHandler(opts),
264287
onValueChanged: ({ value }) => {
265288
const hours = this._getValue().getHours();
266289
const time = new Date(this._getValue());
@@ -273,15 +296,17 @@ class TimeView extends Editor<TimeViewProperties> {
273296
container: this.$element(),
274297
},
275298
value: this._getValue().getHours() >= 12 ? TIMEVIEW_FORMAT12_PM : TIMEVIEW_FORMAT12_AM,
276-
stylingMode: this.option('stylingMode'),
299+
stylingMode,
277300
},
278301
);
279302

280303
this._format12.setAria('label', 'type');
281304
}
282305

283-
_refreshFormat12() {
284-
if (this.option('use24HourFormat')) return;
306+
_refreshFormat12(): void {
307+
const { use24HourFormat } = this.option();
308+
309+
if (use24HourFormat) return;
285310

286311
const value = this._getValue();
287312
const hours = value.getHours();
@@ -291,12 +316,15 @@ class TimeView extends Editor<TimeViewProperties> {
291316
this._silentEditorValueUpdate(this._format12, newValue);
292317
}
293318

294-
_silentEditorValueUpdate(editor, value) {
295-
if (editor) {
296-
editor._suppressValueChangeAction();
297-
editor.option('value', value);
298-
editor._resumeValueChangeAction();
319+
// eslint-disable-next-line class-methods-use-this
320+
_silentEditorValueUpdate(editor: NumberBox | SelectBox | undefined, value: number): void {
321+
if (!editor) {
322+
return;
299323
}
324+
325+
editor._suppressValueChangeAction();
326+
editor.option('value', value);
327+
editor._resumeValueChangeAction();
300328
}
301329

302330
_getNumberBoxConfig(): NumberBoxMaskProperties {
@@ -312,7 +340,8 @@ class TimeView extends Editor<TimeViewProperties> {
312340
}
313341

314342
_normalizeHours(hours: number): number {
315-
return this.option('use24HourFormat') ? hours : hours % 12 || 12;
343+
const { use24HourFormat } = this.option();
344+
return use24HourFormat ? hours : hours % 12 || 12;
316345
}
317346

318347
_updateField(): void {
@@ -325,7 +354,10 @@ class TimeView extends Editor<TimeViewProperties> {
325354
}
326355

327356
_updateTime(): void {
328-
if (this.option('_showClock')) {
357+
// eslint-disable-next-line @typescript-eslint/naming-convention
358+
const { _showClock } = this.option();
359+
360+
if (_showClock) {
329361
this._updateClock();
330362
}
331363

0 commit comments

Comments
 (0)