Skip to content

Commit 3a0c7d5

Browse files
authored
fix(ui5-timepicker): fix firing "change" event for the same value (#1764)
Previously the TImePicker used to fire "change" event unconditionally upon "Submit" press. Now, the value is checked and if it is changed, the event will be fired.
1 parent 377075a commit 3a0c7d5

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

packages/main/src/TimePicker.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ class TimePicker extends UI5Element {
281281
constructor() {
282282
super();
283283

284-
this.readonly = false;
285-
this.disabled = false;
284+
this.prevValue = null;
286285
this._isPickerOpen = false;
287286
this.i18nBundle = getI18nBundle("@ui5/webcomponents");
288287

@@ -531,9 +530,13 @@ class TimePicker extends UI5Element {
531530
selectedDate.setMinutes(minutes);
532531
selectedDate.setSeconds(seconds);
533532

533+
this.setPrevValue(this.value);
534534
this.setValue(this.getFormat().format(selectedDate));
535535

536-
this.fireEvent("change", { value: this.value, valid: true });
536+
if (this.prevValue !== this.value) {
537+
this.fireEvent("change", { value: this.value, valid: true });
538+
this.previousValue = this.value;
539+
}
537540

538541
this.closePicker();
539542
}
@@ -700,6 +703,12 @@ class TimePicker extends UI5Element {
700703
}
701704
}
702705

706+
setPrevValue(value) {
707+
if (this.isValid(value)) {
708+
this.prevValue = this.normalizeValue(value);
709+
}
710+
}
711+
703712
/**
704713
* Formats a Java Script date object into a string representing a locale date and time
705714
* according to the <code>formatPattern</code> property of the TimePicker instance

packages/main/test/pages/TimePicker.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
<br /><br />
3232
<ui5-title>Test "change" event</ui5-title>
33-
<ui5-timepicker id="timepickerChange"></ui5-timepicker>
34-
<ui5-input id="changeResult"></ui5-input>
33+
<ui5-timepicker id="timepickerChange" format-pattern="HH:mm" value="12:00"></ui5-timepicker>
34+
<ui5-input id="changeResult" value="0"></ui5-input>
3535

3636
<br /><br />
3737
<ui5-title>Test "input" event</ui5-title>
@@ -57,12 +57,12 @@
5757

5858
<script>
5959
var counter = 0;
60-
timepickerChange.addEventListener("change", function() {
60+
timepickerChange.addEventListener("ui5-change", function() {
6161
changeResult.value = ++counter;
6262
});
6363

6464
var inputCounter = 0;
65-
timepickerInput.addEventListener("input", function() {
65+
timepickerInput.addEventListener("ui5-input", function() {
6666
inputResult.value = ++inputCounter;
6767
});
6868

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

+38
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,44 @@ describe("TimePicker general interaction", () => {
7171
assert.notOk(slot.error, "cValue State message slot is working");
7272
});
7373

74+
it("tests change event", () => {
75+
const staticAreaItemClassName = browser.getStaticAreaItemClassName("#timepickerChange");
76+
const timepicker = browser.$("#timepickerChange");
77+
const icon = timepicker.shadow$("ui5-input").$("ui5-icon");
78+
const changeResult = browser.$("#changeResult");
79+
80+
// act - submit the same time
81+
icon.click();
82+
const timepickerPopover = browser.$(`.${staticAreaItemClassName}`).shadow$("ui5-responsive-popover");
83+
timepickerPopover.$("#submit").click();
84+
85+
// assert
86+
assert.strictEqual(changeResult.getProperty("value"), "0", "Change not fired as expected");
87+
88+
// act - submit value after changing time
89+
icon.click();
90+
timepickerPopover.$(".ui5-timepicker-hours-wheelslider").setProperty("value", "10");
91+
timepickerPopover.$("#submit").click();
92+
93+
// assert
94+
assert.strictEqual(changeResult.getProperty("value"), "1", "Change fired as expected");
95+
96+
// act - submit the same time
97+
icon.click();
98+
timepickerPopover.$("#submit").click();
99+
100+
// assert
101+
assert.strictEqual(changeResult.getProperty("value"), "1", "Change not fired as expected");
102+
103+
// act - submit value after changing time
104+
icon.click();
105+
timepickerPopover.$(".ui5-timepicker-hours-wheelslider").setProperty("value", "11");
106+
timepickerPopover.$("#submit").click();
107+
108+
// assert
109+
assert.strictEqual(changeResult.getProperty("value"), "2", "Change fired as expected");
110+
});
111+
74112
it("tests value state", () => {
75113
const timepicker = browser.$("#timepickerEmptyValue");
76114
const button = browser.$("#testBtn");

0 commit comments

Comments
 (0)