Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 660fe3e

Browse files
committed
Merge remote-tracking branch 'origin/MC-17922' into 2.3-develop-pr27
2 parents 37dcbf5 + 0ed2711 commit 660fe3e

File tree

3 files changed

+70
-19
lines changed

3 files changed

+70
-19
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,26 @@ define([
114114
* @param {String} value
115115
*/
116116
onValueChange: function (value) {
117-
var dateFormat,
118-
shiftedValue;
117+
var shiftedValue;
119118

120119
if (value) {
121120
if (this.options.showsTime) {
122121
shiftedValue = moment.tz(value, 'UTC').tz(this.storeTimeZone);
123122
} else {
124-
dateFormat = this.shiftedValue() ? this.outputDateFormat : this.inputDateFormat;
125-
shiftedValue = moment(value, dateFormat);
123+
shiftedValue = moment(value, this.outputDateFormat);
126124
}
127125

128-
shiftedValue = shiftedValue.format(this.pickerDateTimeFormat);
129-
130-
if (shiftedValue !== this.shiftedValue()) {
131-
this.shiftedValue(shiftedValue);
126+
if (!shiftedValue.isValid()) {
127+
shiftedValue = moment(value, this.inputDateFormat);
132128
}
129+
shiftedValue = shiftedValue.format(this.pickerDateTimeFormat);
130+
} else {
131+
shiftedValue = '';
133132
}
134133

134+
if (shiftedValue !== this.shiftedValue()) {
135+
this.shiftedValue(shiftedValue);
136+
}
135137
},
136138

137139
/**

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

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,60 @@ define([
4141

4242
if (typeof config === 'object') {
4343
observable = config.storage;
44-
4544
_.extend(options, config.options);
4645
} else {
4746
observable = config;
4847
}
4948

5049
$(el).calendar(options);
5150

52-
observable() && $(el).datepicker(
53-
'setDate',
54-
moment(
51+
ko.utils.registerEventHandler(el, 'change', function () {
52+
observable(this.value);
53+
});
54+
},
55+
56+
/**
57+
* Update calendar widget on element and stores it's value to observable property.
58+
* Datepicker binding takes either observable property or object
59+
* { storage: {ko.observable}, options: {Object} }.
60+
* @param {HTMLElement} element - Element, that binding is applied to
61+
* @param {Function} valueAccessor - Function that returns value, passed to binding
62+
*/
63+
update: function (element, valueAccessor) {
64+
var config = valueAccessor(),
65+
observable,
66+
options = {},
67+
newVal;
68+
69+
_.extend(options, defaults);
70+
71+
if (typeof config === 'object') {
72+
observable = config.storage;
73+
_.extend(options, config.options);
74+
} else {
75+
observable = config;
76+
}
77+
78+
if (_.isEmpty(observable())) {
79+
if ($(element).datepicker('getDate')) {
80+
$(element).datepicker('setDate', null);
81+
$(element).blur();
82+
}
83+
} else {
84+
newVal = moment(
5585
observable(),
5686
utils.convertToMomentFormat(
5787
options.dateFormat + (options.showsTime ? ' ' + options.timeFormat : '')
5888
)
59-
).toDate()
60-
);
89+
).toDate();
6190

62-
$(el).blur();
63-
64-
ko.utils.registerEventHandler(el, 'change', function () {
65-
observable(this.value);
66-
});
91+
if ($(element).datepicker('getDate') == null ||
92+
newVal.valueOf() !== $(element).datepicker('getDate').valueOf()
93+
) {
94+
$(element).datepicker('setDate', newVal);
95+
$(element).blur();
96+
}
97+
}
6798
}
6899
};
69100
});

dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/lib/ko/bind/datepicker.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,23 @@ define([
5353

5454
expect(todayDate).toEqual(result);
5555
});
56+
57+
it('update picked date\'s value after update observable value', function () {
58+
var date = '06/21/2019',
59+
inputFormat = 'M/d/yy',
60+
expectedDate;
61+
62+
expectedDate = moment(date, utils.convertToMomentFormat(inputFormat)).toDate();
63+
observable(date);
64+
65+
expect(expectedDate.valueOf()).toEqual(element.datepicker('getDate').valueOf());
66+
});
67+
68+
it('clear picked date\'s value after clear observable value', function () {
69+
element.datepicker('setTimezoneDate').blur().trigger('change');
70+
observable('');
71+
72+
expect(null).toEqual(element.datepicker('getDate'));
73+
});
5674
});
5775
});

0 commit comments

Comments
 (0)