Skip to content

Commit 01a1d56

Browse files
marker-daomarker dao ®
andauthored
DateBox: Use localized day period names (T1327076) (#33366)
Co-authored-by: marker dao ® <youdontknow@marker-dao.eth>
1 parent acdc6d5 commit 01a1d56

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
/* eslint-disable class-methods-use-this */
21
import eventsEngine from '@js/common/core/events/core/events_engine';
32
import { addNamespace, isCommandKeyPressed, normalizeKeyName } from '@js/common/core/events/utils/index';
4-
import defaultDateNames from '@js/common/core/localization/default_date_names';
53
import { getFormat } from '@js/common/core/localization/ldml/date.format';
64
import { getRegExpInfo } from '@js/common/core/localization/ldml/date.parser';
75
import numberLocalization from '@js/common/core/localization/number';
@@ -125,7 +123,6 @@ class DateBoxMask extends DateBoxBase {
125123
const delta = currentValue - originalValue;
126124

127125
this._loadMaskValue(this._initialMaskValue);
128-
129126
this._changePartValue(delta + step, true);
130127
}
131128

@@ -142,12 +139,12 @@ class DateBoxMask extends DateBoxBase {
142139

143140
_toggleAmPm(): void {
144141
const currentValue = this._getActivePartProp('text');
145-
const indexOfCurrentValue = defaultDateNames
146-
// @ts-expect-error getPeriodNames type should be fixed
147-
.getPeriodNames(this._formatPattern)
148-
.indexOf(currentValue);
142+
const periodNames = dateLocalization.getPeriodNames(this._formatPattern);
143+
const indexOfCurrentValue = periodNames.indexOf(currentValue);
144+
149145
// eslint-disable-next-line no-bitwise
150146
const newValue = indexOfCurrentValue ^ 1;
147+
151148
this._setActivePartValue(newValue);
152149
}
153150

@@ -505,7 +502,7 @@ class DateBoxMask extends DateBoxBase {
505502

506503
const activePartIndex = this._activePartIndex ?? 0;
507504
let index = fitIntoRange(activePartIndex + step, 0, this._dateParts.length - 1);
508-
if (this._dateParts[index].isStub) {
505+
if (this._dateParts[index]?.isStub) {
509506
const isBoundaryIndex = (index === 0 && step < 0)
510507
|| (index === this._dateParts.length - 1 && step > 0);
511508
if (!isBoundaryIndex) {
@@ -543,8 +540,11 @@ class DateBoxMask extends DateBoxBase {
543540
_getActivePartValue(dateValue?: Date | null): number {
544541
const date = dateValue ?? this._maskValue as Date;
545542
const getter = this._getActivePartProp('getter');
543+
const isGetterFunction = isFunction(getter);
544+
545+
const activePartValue = isGetterFunction ? getter(date) : date[getter]() as number;
546546

547-
return isFunction(getter) ? getter(date) : date[getter]() as number;
547+
return activePartValue;
548548
}
549549

550550
_addLeadingZeroes(value: number): string {
@@ -557,6 +557,7 @@ class DateBoxMask extends DateBoxBase {
557557

558558
_setActivePartValue(value: number | string, dateValue?: Date): void {
559559
let newValue: number | string = +value;
560+
560561
const newDateValue = dateValue ?? this._maskValue as Date;
561562
const setter = this._getActivePartProp('setter');
562563
const limits = this._getActivePartLimits();
@@ -569,8 +570,8 @@ class DateBoxMask extends DateBoxBase {
569570
} else {
570571
newDateValue[setter](newValue);
571572
}
572-
this._renderDisplayText(this._getDisplayedText(newDateValue));
573573

574+
this._renderDisplayText(this._getDisplayedText(newDateValue));
574575
this._renderDateParts();
575576
}
576577

packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/datebox.mask.tests.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import $ from 'jquery';
22
import { renderDateParts, getDatePartIndexByPosition } from '__internal/ui/date_box/date_box.mask.parts';
33
import dateParser from '__internal/core/localization/ldml/dateParserModule';
44
import dateLocalization from 'common/core/localization/date';
5+
import localization from 'localization';
56
import { noop } from 'core/utils/common';
67
import pointerMock from '../../helpers/pointerMock.js';
78
import 'ui/date_box';
@@ -1290,6 +1291,32 @@ module('Date AM/PM Handling', setupModule, () => {
12901291
assert.strictEqual(this.$input.val(), 'PM');
12911292
});
12921293
});
1294+
1295+
test('up/down arrows should continue to switch AM/PM on subsequent presses when locale has localized period names (T1327076)', function(assert) {
1296+
const defaultLocale = localization.locale();
1297+
1298+
try {
1299+
localization.locale('es-ES');
1300+
1301+
const [amName, pmName] = dateLocalization.getPeriodNames();
1302+
1303+
this.instance.option({
1304+
value: new Date('10/10/2012 22:00'),
1305+
displayFormat: 'a',
1306+
useMaskBehavior: true,
1307+
});
1308+
1309+
assert.strictEqual(this.$input.val(), pmName, 'initial value is localized PM');
1310+
1311+
this.keyboard.press('up');
1312+
assert.strictEqual(this.$input.val(), amName, 'value changed to localized AM after first press');
1313+
1314+
this.keyboard.press('up');
1315+
assert.strictEqual(this.$input.val(), pmName, 'value returned to localized PM after second press');
1316+
} finally {
1317+
localization.locale(defaultLocale);
1318+
}
1319+
});
12931320
});
12941321

12951322
module('TimeZone Handling', setupModule, () => {

0 commit comments

Comments
 (0)