Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 5d088d3

Browse files
Michael Chenjelbourn
authored andcommitted
feat(datepicker): Debounce datepicker input element event
1 parent 40c7a8f commit 5d088d3

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/components/datepicker/datePicker.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
/** Class applied to the container if the date is invalid. */
8989
var INVALID_CLASS = 'md-datepicker-invalid';
9090

91+
/** Default time in ms to debounce input event by. */
92+
var DEFAULT_DEBOUNCE_INTERVAL = 500;
93+
9194
/**
9295
* Controller for md-datepicker.
9396
*
@@ -189,7 +192,7 @@
189192
ngModelCtrl.$render = function() {
190193
self.date = self.ngModelCtrl.$viewValue;
191194
self.inputElement.value = self.dateLocale.formatDate(self.date);
192-
self.inputElement.size = self.inputElement.value.length + EXTRA_INPUT_SIZE;
195+
self.resizeInputElement();
193196
};
194197
};
195198

@@ -207,22 +210,10 @@
207210
self.closeCalendarPane();
208211
});
209212

210-
// TODO(jelbourn): Debounce this input event.
211-
self.inputElement.addEventListener('input', function() {
212-
var inputString = self.inputElement.value;
213-
var parsedDate = self.dateLocale.parseDate(inputString);
214-
215-
self.inputElement.size = inputString.length + EXTRA_INPUT_SIZE;
216-
217-
if (self.dateUtil.isValidDate(parsedDate) && self.dateLocale.isDateComplete(inputString)) {
218-
self.ngModelCtrl.$setViewValue(parsedDate);
219-
self.inputContainer.classList.remove(INVALID_CLASS);
220-
self.$scope.$apply();
221-
} else {
222-
// If there's an input string, it's an invalid date.
223-
self.inputContainer.classList.toggle(INVALID_CLASS, inputString);
224-
}
225-
});
213+
self.inputElement.addEventListener('input', angular.bind(self, self.resizeInputElement));
214+
// TODO(chenmike): Add ability for users to specify this interval.
215+
self.inputElement.addEventListener('input', self.$mdUtil.debounce(self.handleInputEvent,
216+
DEFAULT_DEBOUNCE_INTERVAL, self));
226217
};
227218

228219
/** Attach event listeners for user interaction. */
@@ -259,7 +250,7 @@
259250
});
260251

261252
Object.defineProperty(this, 'placeholder', {
262-
get: function() { return self.inputElement.placeholder },
253+
get: function() { return self.inputElement.placeholder; },
263254
set: function(value) { self.inputElement.placeholder = value; }
264255
});
265256
};
@@ -274,6 +265,31 @@
274265
this.calendarButton.disabled = isDisabled;
275266
};
276267

268+
/**
269+
* Resizes the input element based on the size of its content.
270+
*/
271+
DatePickerCtrl.prototype.resizeInputElement = function() {
272+
this.inputElement.size = this.inputElement.value.length + EXTRA_INPUT_SIZE;
273+
};
274+
275+
/**
276+
* Sets the model value if the user input is a valid date.
277+
* Adds an invalid class to the input element if not.
278+
*/
279+
DatePickerCtrl.prototype.handleInputEvent = function() {
280+
var inputString = this.inputElement.value;
281+
var parsedDate = this.dateLocale.parseDate(inputString);
282+
283+
if (this.dateUtil.isValidDate(parsedDate) && this.dateLocale.isDateComplete(inputString)) {
284+
this.ngModelCtrl.$setViewValue(parsedDate);
285+
this.inputContainer.classList.remove(INVALID_CLASS);
286+
this.$scope.$apply();
287+
} else {
288+
// If there's an input string, it's an invalid date.
289+
this.inputContainer.classList.toggle(INVALID_CLASS, inputString);
290+
}
291+
};
292+
277293
/** Position and attach the floating calendar to the document. */
278294
DatePickerCtrl.prototype.attachCalendarPane = function() {
279295
var calendarPane = this.calendarPane;

0 commit comments

Comments
 (0)