Skip to content

Commit 850a6c4

Browse files
tsanislavgatevilhan007
authored andcommitted
feat(ui5-date-picker): preventable change and input events (#3609)
In this change we introduce the preventable behaviour of the change event in the DatePicker control. This change will allow the application developers to control the whole value setting and value validation, this means that they will receive this event before we set the value and the value state and will have the possibility to prevent our setters and validators and update the value and the value state themselves. Fixes: #3516 Closes: #3516
1 parent 38a90ba commit 850a6c4

File tree

4 files changed

+72
-8
lines changed

4 files changed

+72
-8
lines changed

packages/main/src/DatePicker.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,41 @@ const metadata = {
230230
* Fired when the input operation has finished by pressing Enter or on focusout.
231231
*
232232
* @event
233+
* @allowPreventDefault
233234
* @public
235+
* @param {String} value The submitted value.
236+
* @param {Boolean} valid Indicator if the value is in correct format pattern and in valid range.
234237
*/
235-
change: {},
238+
change: {
239+
details: {
240+
value: {
241+
type: String,
242+
},
243+
valid: {
244+
type: Boolean,
245+
},
246+
},
247+
},
236248

237249
/**
238250
* Fired when the value of the <code>ui5-date-picker</code> is changed at each key stroke.
239251
*
240252
* @event
253+
* @allowPreventDefault
241254
* @public
255+
* @param {String} value The submitted value.
256+
* @param {Boolean} valid Indicator if the value is in correct format pattern and in valid range.
242257
*/
243-
input: {},
258+
input: {
259+
details: {
260+
value: {
261+
type: String,
262+
},
263+
valid: {
264+
type: Boolean,
265+
},
266+
},
267+
},
244268
},
245269
};
246270

@@ -478,14 +502,22 @@ class DatePicker extends DateComponentBase {
478502
value = this.normalizeValue(value); // transform valid values (in any format) to the correct format
479503
}
480504

505+
let executeEvent = true;
506+
507+
events.forEach(event => {
508+
if (!this.fireEvent(event, { value, valid }, true)) {
509+
executeEvent = false;
510+
}
511+
});
512+
513+
if (!executeEvent) {
514+
return;
515+
}
516+
481517
if (updateValue) {
482518
this.value = value;
483519
this._updateValueState(); // Change the value state to Error/None, but only if needed
484520
}
485-
486-
events.forEach(event => {
487-
this.fireEvent(event, { value, valid });
488-
});
489521
}
490522

491523
_updateValueState() {

packages/main/test/pages/DatePicker.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ <h3>placeholder + title + events</h3>
5757
<ui5-label id="lblChange">change: N/A</ui5-label><br/>
5858
<ui5-label id="lblLiveChange">input: N/A</ui5-label><br/>
5959

60+
<h3>placeholder + title + prevented default events</h3>
61+
<ui5-date-picker id='dpPrevent'
62+
placeholder='Delivery Date...'
63+
title='Delivery Date!'>
64+
</ui5-date-picker>
65+
<ui5-label id="lblChangePrevent">change: N/A</ui5-label><br/>
66+
<ui5-label id="lblLiveChangePrevent">input: N/A</ui5-label><br/>
67+
6068
<h3>format-pattern - 'short'</h3>
6169
<ui5-date-picker id='dp6' format-pattern='short'></ui5-date-picker>
6270

@@ -153,6 +161,20 @@ <h3>DatePicker with properties given in the wrong format</h3>
153161
labelLiveChange.innerHTML = 'input: ' + JSON.stringify(e.detail);
154162
});
155163

164+
var dpPrevent = document.getElementById('dpPrevent');
165+
var labelChangePrevent = document.getElementById('lblChangePrevent');
166+
var labelLiveChangePrevent = document.getElementById('lblLiveChangePrevent');
167+
168+
dpPrevent.addEventListener('ui5-change', function(e) {
169+
e.preventDefault();
170+
console.log('change: ', e.detail);
171+
labelChangePrevent.innerHTML = 'change: ' + JSON.stringify(e.detail);
172+
});
173+
dpPrevent.addEventListener('ui5-input', function(e) {
174+
console.log('input: ', e.detail);
175+
labelLiveChangePrevent.innerHTML = 'input: ' + JSON.stringify(e.detail);
176+
});
177+
156178
$('#ui5-datepicker--startDate').on('ui5-input change', (function(e) {
157179
var dp = $('#ui5-datepicker--startDate')[0];
158180
dp.setAttribute('value-state', e.detail.valid ? 'None' : 'Error');

packages/main/test/pages/DatePicker_test_page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ <h3>Test ariaLabel and ariaLabelledBy</h3>
105105
document.querySelector("#dp8").addEventListener("ui5-change", increaseCounter);
106106
document.querySelector("#dp13").addEventListener("ui5-change", function(e) {
107107
document.querySelector('#lblTomorrow').innerHTML = ++counterTomorrow;
108-
document.querySelector('#lblTomorrowDate').innerHTML = e.target.value;
108+
document.querySelector('#lblTomorrowDate').innerHTML = e.detail.value;
109109
});
110110

111111
var dp16 = document.getElementById('dp16');

packages/main/test/specs/DatePicker.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe("Date Picker Tests", () => {
284284

285285
// Two change events should be fired and the date should twice normalized
286286
assert.equal(lblChangeCounter.getHTML(false), "2", 'change event is being fired twice');
287-
assert.equal(lblTomorrowDate.getHTML(false), tomorrowDate, 'tomorrow is normalazid to date twice as well');
287+
assert.equal(lblTomorrowDate.getHTML(false), tomorrowDate, 'tomorrow is normalized to date twice as well');
288288
});
289289

290290
it("today value is normalized and correctly rounded to 00:00:00", () => {
@@ -923,4 +923,14 @@ describe("Date Picker Tests", () => {
923923

924924
assert.equal(datepicker.input.getProperty("valueState"), "Error", "value state of the input is valid");
925925
});
926+
927+
it("focusout fires change but doesn't change the value state if the default behaviour is prevented", () => {
928+
datepicker.id = "#dpPrevent";
929+
930+
datepicker.input.click();
931+
datepicker.root.keys("Jan 1, 1999999");
932+
browser.$("#dp5").shadow$("ui5-input").shadow$("input").click(); //click elsewhere to focusout
933+
934+
assert.equal(datepicker.input.getProperty("valueState"), "None", 'the value state is not changed');
935+
});
926936
});

0 commit comments

Comments
 (0)