Skip to content

Commit 28bdd51

Browse files
committed
Merge remote-tracking branch 'origin/imported-magento-magento2-31549' into 2.4-develop-pr119
2 parents 07b1289 + 53b893f commit 28bdd51

File tree

3 files changed

+36
-10
lines changed
  • app/code/Magento/Ui/view/base/web/js
  • dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element

3 files changed

+36
-10
lines changed

app/code/Magento/Ui/view/base/web/js/form/element/date.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ define([
124124
var shiftedValue;
125125

126126
if (value) {
127-
if (this.options.showsTime) {
127+
if (this.options.showsTime && !this.options.timeOnly) {
128128
shiftedValue = moment.tz(value, 'UTC').tz(this.storeTimeZone);
129129
} else {
130130
shiftedValue = moment(value, this.outputDateFormat, true);
@@ -157,7 +157,7 @@ define([
157157
if (shiftedValue) {
158158
momentValue = moment(shiftedValue, this.pickerDateTimeFormat);
159159

160-
if (this.options.showsTime) {
160+
if (this.options.showsTime && !this.options.timeOnly) {
161161
formattedValue = moment(momentValue).format(this.timezoneFormat);
162162
value = moment.tz(formattedValue, this.storeTimeZone).tz('UTC').toISOString();
163163
} else {
@@ -177,10 +177,13 @@ define([
177177
* with moment.js library.
178178
*/
179179
prepareDateTimeFormats: function () {
180-
this.pickerDateTimeFormat = this.options.dateFormat;
181-
182-
if (this.options.showsTime) {
183-
this.pickerDateTimeFormat += ' ' + this.options.timeFormat;
180+
if (this.options.timeOnly) {
181+
this.pickerDateTimeFormat = this.options.timeFormat;
182+
} else {
183+
this.pickerDateTimeFormat = this.options.dateFormat;
184+
if (this.options.showsTime) {
185+
this.pickerDateTimeFormat += ' ' + this.options.timeFormat;
186+
}
184187
}
185188

186189
this.pickerDateTimeFormat = utils.convertToMomentFormat(this.pickerDateTimeFormat);
@@ -189,8 +192,12 @@ define([
189192
this.outputDateFormat = this.options.dateFormat;
190193
}
191194

192-
this.inputDateFormat = utils.convertToMomentFormat(this.inputDateFormat);
193-
this.outputDateFormat = utils.convertToMomentFormat(this.outputDateFormat);
195+
this.inputDateFormat = this.options.timeOnly ?
196+
utils.convertToMomentFormat(this.pickerDefaultTimeFormat) :
197+
utils.convertToMomentFormat(this.inputDateFormat);
198+
this.outputDateFormat = this.options.timeOnly ?
199+
utils.convertToMomentFormat(this.options.timeFormat) :
200+
utils.convertToMomentFormat(this.outputDateFormat);
194201

195202
this.validationParams.dateFormat = this.outputDateFormat;
196203
}

app/code/Magento/Ui/view/base/web/js/lib/knockout/bindings/datepicker.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ define([
8787
).toDate();
8888
}
8989

90-
$element.datepicker('setDate', newVal);
91-
$element.blur();
90+
if (!options.timeOnly) {
91+
$element.datepicker('setDate', newVal);
92+
$element.blur();
93+
}
9294
});
9395
}
9496
};

dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element/date.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,22 @@ define([
6565
expect(model.getPreview()).toBe('28-11-2020');
6666
});
6767

68+
it('Check date will have correct value with timeOnly config value.', function () {
69+
model.options.timeOnly = true;
70+
model.options.timeFormat = 'h:mm a';
71+
model.prepareDateTimeFormats();
72+
model.value('02:43:58');
73+
expect(model.getPreview()).toBe('2:43 am');
74+
75+
model.options.timeFormat = 'HH:mm:ss';
76+
model.prepareDateTimeFormats();
77+
model.value('02:43:58');
78+
expect(model.getPreview()).toBe('02:43:58');
79+
80+
model.options.timeFormat = 'HH:mm:ss';
81+
model.prepareDateTimeFormats();
82+
model.value('2:43 am');
83+
expect(model.getPreview()).toBe('02:43:00');
84+
});
6885
});
6986
});

0 commit comments

Comments
 (0)