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

Commit

Permalink
feat(datepicker): Debounce datepicker input element event
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Chen authored and jelbourn committed Aug 13, 2015
1 parent 40c7a8f commit 5d088d3
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/components/datepicker/datePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
/** Class applied to the container if the date is invalid. */
var INVALID_CLASS = 'md-datepicker-invalid';

/** Default time in ms to debounce input event by. */
var DEFAULT_DEBOUNCE_INTERVAL = 500;

/**
* Controller for md-datepicker.
*
Expand Down Expand Up @@ -189,7 +192,7 @@
ngModelCtrl.$render = function() {
self.date = self.ngModelCtrl.$viewValue;
self.inputElement.value = self.dateLocale.formatDate(self.date);
self.inputElement.size = self.inputElement.value.length + EXTRA_INPUT_SIZE;
self.resizeInputElement();
};
};

Expand All @@ -207,22 +210,10 @@
self.closeCalendarPane();
});

// TODO(jelbourn): Debounce this input event.
self.inputElement.addEventListener('input', function() {
var inputString = self.inputElement.value;
var parsedDate = self.dateLocale.parseDate(inputString);

self.inputElement.size = inputString.length + EXTRA_INPUT_SIZE;

if (self.dateUtil.isValidDate(parsedDate) && self.dateLocale.isDateComplete(inputString)) {
self.ngModelCtrl.$setViewValue(parsedDate);
self.inputContainer.classList.remove(INVALID_CLASS);
self.$scope.$apply();
} else {
// If there's an input string, it's an invalid date.
self.inputContainer.classList.toggle(INVALID_CLASS, inputString);
}
});
self.inputElement.addEventListener('input', angular.bind(self, self.resizeInputElement));
// TODO(chenmike): Add ability for users to specify this interval.
self.inputElement.addEventListener('input', self.$mdUtil.debounce(self.handleInputEvent,
DEFAULT_DEBOUNCE_INTERVAL, self));
};

/** Attach event listeners for user interaction. */
Expand Down Expand Up @@ -259,7 +250,7 @@
});

Object.defineProperty(this, 'placeholder', {
get: function() { return self.inputElement.placeholder },
get: function() { return self.inputElement.placeholder; },
set: function(value) { self.inputElement.placeholder = value; }
});
};
Expand All @@ -274,6 +265,31 @@
this.calendarButton.disabled = isDisabled;
};

/**
* Resizes the input element based on the size of its content.
*/
DatePickerCtrl.prototype.resizeInputElement = function() {
this.inputElement.size = this.inputElement.value.length + EXTRA_INPUT_SIZE;
};

/**
* Sets the model value if the user input is a valid date.
* Adds an invalid class to the input element if not.
*/
DatePickerCtrl.prototype.handleInputEvent = function() {
var inputString = this.inputElement.value;
var parsedDate = this.dateLocale.parseDate(inputString);

if (this.dateUtil.isValidDate(parsedDate) && this.dateLocale.isDateComplete(inputString)) {
this.ngModelCtrl.$setViewValue(parsedDate);
this.inputContainer.classList.remove(INVALID_CLASS);
this.$scope.$apply();
} else {
// If there's an input string, it's an invalid date.
this.inputContainer.classList.toggle(INVALID_CLASS, inputString);
}
};

/** Position and attach the floating calendar to the document. */
DatePickerCtrl.prototype.attachCalendarPane = function() {
var calendarPane = this.calendarPane;
Expand Down

0 comments on commit 5d088d3

Please sign in to comment.