Skip to content

Commit 6daac94

Browse files
author
marker dao ®
committed
DateBox: Use localized day period names (T1327076)
1 parent c743d88 commit 6daac94

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable class-methods-use-this */
22
import eventsEngine from '@js/common/core/events/core/events_engine';
33
import { addNamespace, isCommandKeyPressed, normalizeKeyName } from '@js/common/core/events/utils/index';
4-
import defaultDateNames from '@js/common/core/localization/default_date_names';
54
import { getFormat } from '@js/common/core/localization/ldml/date.format';
65
import { getRegExpInfo } from '@js/common/core/localization/ldml/date.parser';
76
import numberLocalization from '@js/common/core/localization/number';
@@ -125,7 +124,6 @@ class DateBoxMask extends DateBoxBase {
125124
const delta = currentValue - originalValue;
126125

127126
this._loadMaskValue(this._initialMaskValue);
128-
129127
this._changePartValue(delta + step, true);
130128
}
131129

@@ -142,12 +140,12 @@ class DateBoxMask extends DateBoxBase {
142140

143141
_toggleAmPm(): void {
144142
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);
143+
const periodNames = dateLocalization.getPeriodNames(this._formatPattern);
144+
const indexOfCurrentValue = periodNames.indexOf(currentValue);
145+
149146
// eslint-disable-next-line no-bitwise
150147
const newValue = indexOfCurrentValue ^ 1;
148+
151149
this._setActivePartValue(newValue);
152150
}
153151

@@ -543,8 +541,11 @@ class DateBoxMask extends DateBoxBase {
543541
_getActivePartValue(dateValue?: Date | null): number {
544542
const date = dateValue ?? this._maskValue as Date;
545543
const getter = this._getActivePartProp('getter');
544+
const isGetterFunction = isFunction(getter);
545+
546+
const activePartValue = isGetterFunction ? getter(date) : date[getter]() as number;
546547

547-
return isFunction(getter) ? getter(date) : date[getter]() as number;
548+
return activePartValue;
548549
}
549550

550551
_addLeadingZeroes(value: number): string {
@@ -557,6 +558,7 @@ class DateBoxMask extends DateBoxBase {
557558

558559
_setActivePartValue(value: number | string, dateValue?: Date): void {
559560
let newValue: number | string = +value;
561+
560562
const newDateValue = dateValue ?? this._maskValue as Date;
561563
const setter = this._getActivePartProp('setter');
562564
const limits = this._getActivePartLimits();
@@ -569,8 +571,8 @@ class DateBoxMask extends DateBoxBase {
569571
} else {
570572
newDateValue[setter](newValue);
571573
}
572-
this._renderDisplayText(this._getDisplayedText(newDateValue));
573574

575+
this._renderDisplayText(this._getDisplayedText(newDateValue));
574576
this._renderDateParts();
575577
}
576578

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

Lines changed: 26 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,31 @@ 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+
});
1307+
1308+
assert.strictEqual(this.$input.val(), pmName, 'initial value is localized PM');
1309+
1310+
this.keyboard.press('up');
1311+
assert.strictEqual(this.$input.val(), amName, 'value changed to localized AM after first press');
1312+
1313+
this.keyboard.press('up');
1314+
assert.strictEqual(this.$input.val(), pmName, 'value returned to localized PM after second press');
1315+
} finally {
1316+
localization.locale(defaultLocale);
1317+
}
1318+
});
12931319
});
12941320

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

0 commit comments

Comments
 (0)