Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add silent option on setTime API for preventing firing change event #82

Merged
merged 4 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/js/timepicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,17 +441,18 @@ var TimePicker = defineClass(
/**
* Set values in spinboxes from time
* @private
* @param {boolean} silent prevents firing 'change' event if it is true.
*/
syncToInputs: function() {
syncToInputs: function(silent) {
var hour = this.hour;
var minute = this.minute;

if (this.showMeridiem) {
hour = util.getMeridiemHour(hour);
}

this.hourInput.setValue(hour);
this.minuteInput.setValue(minute);
this.hourInput.setValue(hour, silent);
this.minuteInput.setValue(minute, silent);
},

/**
Expand Down Expand Up @@ -626,16 +627,17 @@ var TimePicker = defineClass(
* Set time
* @param {number} hour for time picker - (0~23)
* @param {number} minute for time picker
* @param {boolean} [silent] if it set true, 'change' event will not be fired.
*/
setTime: function(hour, minute) {
setTime: function(hour, minute, silent) {
if (!this.validItems(hour, minute)) {
return;
}

this.hour = hour;
this.minute = minute;

this.syncToInputs();
this.syncToInputs(silent);
if (this.showMeridiem) {
this.syncToMeridiemElements();
}
Expand All @@ -651,10 +653,12 @@ var TimePicker = defineClass(
* console.log(e.hour, e.minute);
* });
*/
this.fire('change', {
hour: this.hour,
minute: this.minute
});
if (!silent) {
this.fire('change', {
hour: this.hour,
minute: this.minute
});
}
},

/**
Expand Down Expand Up @@ -902,6 +906,7 @@ var TimePicker = defineClass(
this.removeEvents();
removeElement(this.element);

// eslint-disable-next-line max-len
this.container = this.showMeridiem = this.hourInput = this.minuteInput = this.hour = this.minute = this.inputType = this.element = this.meridiemElement = this.amEl = this.pmEl = null;
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/js/timepicker/selectbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,16 @@ var Selectbox = defineClass(
/**
* Set new value
* @private
* @param {boolean} silent prevents firing 'change' event if it is true.
*/
_setNewValue: function() {
_setNewValue: function(silent) {
var newValue = Number(this._element.value);
this._selectedIndex = inArray(newValue, this._items);
this.fire('change', {
value: newValue
});
if (!silent) {
this.fire('change', {
value: newValue
});
}
},

/**
Expand All @@ -192,14 +195,15 @@ var Selectbox = defineClass(
/**
* Set value
* @param {number} value - New value
* @param {boolean} silent - prevents firing 'change' event if it is true.
*/
setValue: function(value) {
setValue: function(value, silent) {
var newIndex = inArray(value, this._items);

if (newIndex > -1 && newIndex !== this._selectedIndex) {
this._selectedIndex = newIndex;
this._element.value = value;
this._setNewValue();
this._setNewValue(silent);
}
},

Expand Down
19 changes: 12 additions & 7 deletions src/js/timepicker/spinbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ var Spinbox = defineClass(
/**
* Change value to input-box if it is valid.
* @private
* @param {boolean} silent prevents firing 'change' event if it is true.
*/
_changeToInputValue: function() {
_changeToInputValue: function(silent) {
var newValue = Number(this._inputElement.value);
var newIndex = inArray(newValue, this._items);

Expand All @@ -260,22 +261,26 @@ var Spinbox = defineClass(
}

if (newIndex === -1) {
this.setValue(this._items[this._selectedIndex]);
this.setValue(this._items[this._selectedIndex], silent);
} else {
this._selectedIndex = newIndex;
this.fire('change', {
value: newValue
});

if (!silent) {
this.fire('change', {
value: newValue
});
}
}
},

/**
* Set value to input-box.
* @param {number} value - Value
* @param {boolean} silent - prevents firing 'change' event if it is true.
*/
setValue: function(value) {
setValue: function(value, silent) {
this._inputElement.value = util.formatTime(value, this._format);
this._changeToInputValue();
this._changeToInputValue(silent);
},

/**
Expand Down
18 changes: 18 additions & 0 deletions test/timepicker/timepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,21 @@ describe('Set selectable range', function() {
expect(timepickerNoMeridiem.getMinute()).toBe(36);
});
});

describe('custom event', function() {
it('should fire change event when the value is changed', function() {
var spy = jest.fn();
timepickerNoMeridiem.on('change', spy);

timepickerNoMeridiem.setTime(10, 9);
expect(spy).toHaveBeenCalled();
});

it('should not fire change event when the value is changed', function() {
var spy = jest.fn();
timepickerNoMeridiem.on('change', spy);

timepickerNoMeridiem.setTime(10, 9, true);
expect(spy).not.toHaveBeenCalled();
});
});