Skip to content

Commit 24f34d1

Browse files
marker-daomarker dao ®
andauthored
DateBox: Use localized day period names (T1327076) (#33365)
Co-authored-by: marker dao ® <youdontknow@marker-dao.eth>
1 parent 6872f1d commit 24f34d1

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import eventsEngine from '@js/common/core/events/core/events_engine';
22
import { addNamespace, isCommandKeyPressed, normalizeKeyName } from '@js/common/core/events/utils/index';
3-
import defaultDateNames from '@js/common/core/localization/default_date_names';
43
import { getFormat } from '@js/common/core/localization/ldml/date.format';
54
import { getRegExpInfo } from '@js/common/core/localization/ldml/date.parser';
65
import numberLocalization from '@js/common/core/localization/number';
@@ -136,7 +135,6 @@ class DateBoxMask extends DateBoxBase {
136135
const delta = currentValue - originalValue;
137136

138137
this._loadMaskValue(this._initialMaskValue);
139-
140138
this._changePartValue(delta + step, true);
141139
}
142140

@@ -153,12 +151,12 @@ class DateBoxMask extends DateBoxBase {
153151

154152
_toggleAmPm(): void {
155153
const currentValue = this._getActivePartProp('text');
156-
const indexOfCurrentValue = defaultDateNames
157-
// @ts-expect-error getPeriodNames type should be fixed
158-
.getPeriodNames(this._formatPattern)
159-
.indexOf(currentValue);
154+
const periodNames = dateLocalization.getPeriodNames(this._formatPattern);
155+
const indexOfCurrentValue = periodNames.indexOf(currentValue);
156+
160157
// eslint-disable-next-line no-bitwise
161158
const newValue = indexOfCurrentValue ^ 1;
159+
162160
this._setActivePartValue(newValue);
163161
}
164162

@@ -516,7 +514,7 @@ class DateBoxMask extends DateBoxBase {
516514

517515
const activePartIndex = this._activePartIndex ?? 0;
518516
let index = fitIntoRange(activePartIndex + step, 0, this._dateParts.length - 1);
519-
if (this._dateParts[index].isStub) {
517+
if (this._dateParts[index]?.isStub) {
520518
const isBoundaryIndex = (index === 0 && step < 0)
521519
|| (index === this._dateParts.length - 1 && step > 0);
522520
if (!isBoundaryIndex) {
@@ -554,8 +552,11 @@ class DateBoxMask extends DateBoxBase {
554552
_getActivePartValue(dateValue?: Date | null): number {
555553
const date = dateValue ?? this._maskValue as Date;
556554
const getter = this._getActivePartProp('getter');
555+
const isGetterFunction = isFunction(getter);
556+
557+
const activePartValue = isGetterFunction ? getter(date) : date[getter]() as number;
557558

558-
return isFunction(getter) ? getter(date) : date[getter]() as number;
559+
return activePartValue;
559560
}
560561

561562
_addLeadingZeroes(value: number): string {
@@ -568,6 +569,7 @@ class DateBoxMask extends DateBoxBase {
568569

569570
_setActivePartValue(value: number | string, dateValue?: Date): void {
570571
let newValue: number | string = +value;
572+
571573
const newDateValue = dateValue ?? this._maskValue as Date;
572574
const setter = this._getActivePartProp('setter');
573575
const limits = this._getActivePartLimits();
@@ -580,8 +582,8 @@ class DateBoxMask extends DateBoxBase {
580582
} else {
581583
newDateValue[setter](newValue);
582584
}
583-
this._renderDisplayText(this._getDisplayedText(newDateValue));
584585

586+
this._renderDisplayText(this._getDisplayedText(newDateValue));
585587
this._renderDateParts();
586588
}
587589

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ 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';
89
import keyboardMock from '../../helpers/keyboardMock.js';
910
import devices from '__internal/core/m_devices';
11+
import es from 'localization/messages/es.json!';
1012

1113
const { test, module } = QUnit;
1214

@@ -1290,6 +1292,32 @@ module('Date AM/PM Handling', setupModule, () => {
12901292
assert.strictEqual(this.$input.val(), 'PM');
12911293
});
12921294
});
1295+
1296+
test('up/down arrows should continue to switch AM/PM on subsequent presses when locale has localized period names (T1327076)', function(assert) {
1297+
const defaultLocale = localization.locale();
1298+
1299+
try {
1300+
localization.locale('es-ES');
1301+
1302+
const [amName, pmName] = dateLocalization.getPeriodNames();
1303+
1304+
this.instance.option({
1305+
value: new Date('10/10/2012 22:00'),
1306+
displayFormat: 'a',
1307+
useMaskBehavior: true,
1308+
});
1309+
1310+
assert.strictEqual(this.$input.val(), pmName, 'initial value is localized PM');
1311+
1312+
this.keyboard.press('up');
1313+
assert.strictEqual(this.$input.val(), amName, 'value changed to localized AM after first press');
1314+
1315+
this.keyboard.press('up');
1316+
assert.strictEqual(this.$input.val(), pmName, 'value returned to localized PM after second press');
1317+
} finally {
1318+
localization.locale(defaultLocale);
1319+
}
1320+
});
12931321
});
12941322

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

0 commit comments

Comments
 (0)