Skip to content

Commit 0c6bbc6

Browse files
committed
MC-17922: Date in filter area doesn't match with Active filters bar
1 parent f94b4a8 commit 0c6bbc6

File tree

3 files changed

+69
-20
lines changed

3 files changed

+69
-20
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,28 @@ 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);
126+
if (!shiftedValue.isValid()) {
127+
shiftedValue = moment(value, this.inputDateFormat);
128+
}
129129

130+
shiftedValue = shiftedValue.format(this.pickerDateTimeFormat);
130131
if (shiftedValue !== this.shiftedValue()) {
131132
this.shiftedValue(shiftedValue);
132133
}
134+
} else {
135+
if (this.shiftedValue()) {
136+
this.shiftedValue('');
137+
}
133138
}
134-
135139
},
136140

137141
/**

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

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,62 @@ define([
3535
init: function (el, valueAccessor) {
3636
var config = valueAccessor(),
3737
observable,
38-
options = {};
39-
40-
_.extend(options, defaults);
38+
options = defaults;
4139

4240
if (typeof config === 'object') {
4341
observable = config.storage;
44-
4542
_.extend(options, config.options);
4643
} else {
4744
observable = config;
4845
}
4946

5047
$(el).calendar(options);
5148

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

64-
ko.utils.registerEventHandler(el, 'change', function () {
65-
observable(this.value);
66-
});
87+
if ($(element).datepicker('getDate') == null ||
88+
newVal.valueOf() !== $(element).datepicker('getDate').valueOf()
89+
) {
90+
$(element).datepicker('setDate', newVal);
91+
$(element).blur();
92+
}
93+
}
6794
}
6895
};
6996
});

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)