diff --git a/src/components/input/demoTextarea/index.html b/src/components/input/demoTextarea/index.html
new file mode 100644
index 0000000000..d09b50bda8
--- /dev/null
+++ b/src/components/input/demoTextarea/index.html
@@ -0,0 +1,28 @@
+
diff --git a/src/components/input/demoTextarea/script.js b/src/components/input/demoTextarea/script.js
new file mode 100644
index 0000000000..0339209495
--- /dev/null
+++ b/src/components/input/demoTextarea/script.js
@@ -0,0 +1,2 @@
+angular
+ .module('inputTextareaDemo', ['ngMaterial', 'ngMessages']);
\ No newline at end of file
diff --git a/src/components/input/demoTextarea/style.css b/src/components/input/demoTextarea/style.css
new file mode 100644
index 0000000000..5ce2e397b1
--- /dev/null
+++ b/src/components/input/demoTextarea/style.css
@@ -0,0 +1,4 @@
+.demoTextarea {
+ margin: 14px;
+ background-color: #f7f7f7;
+}
\ No newline at end of file
diff --git a/src/components/input/input.js b/src/components/input/input.js
index 6ce8fc4fbc..bad5c1165b 100644
--- a/src/components/input/input.js
+++ b/src/components/input/input.js
@@ -323,22 +323,27 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
var node = element[0];
var container = containerCtrl.element[0];
- var min_rows = NaN;
+ var rows = NaN;
+ var minRows = NaN;
var lineHeight = null;
// can't check if height was or not explicity set,
// so rows attribute will take precedence if present
if (node.hasAttribute('rows')) {
- min_rows = parseInt(node.getAttribute('rows'));
+ rows = parseInt(node.getAttribute('rows'));
+ } else if (node.hasAttribute('min-rows')) {
+ minRows = parseInt(node.getAttribute('min-rows'));
}
- var onChangeTextarea = $mdUtil.debounce(growTextarea, 1);
+ var onChangeTextarea = $mdUtil.debounce(function() {
+ $mdUtil.nextTick(growTextarea);
+ }, 1);
function pipelineListener(value) {
onChangeTextarea();
return value;
}
- if (ngModelCtrl) {
+ if (hasNgModel) {
ngModelCtrl.$formatters.push(pipelineListener);
ngModelCtrl.$viewChangeListeners.push(pipelineListener);
} else {
@@ -346,8 +351,8 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
}
element.on('keydown input', onChangeTextarea);
- if (isNaN(min_rows)) {
- element.attr('rows', '1');
+ if (isNaN(rows)) {
+ element.attr('rows', isNaN(minRows) ? '1' : minRows);
element.on('scroll', onScroll);
}
@@ -365,12 +370,13 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
// temporarily disables element's flex so its height 'runs free'
element.addClass('md-no-flex');
- if (isNaN(min_rows)) {
+ var contentHeight;
+ if (isNaN(rows) || !isNaN(minRows)) {
node.style.height = "auto";
node.scrollTop = 0;
- var height = getHeight();
- if (height) node.style.height = height + 'px';
- } else {
+ contentHeight = getHeight();
+ }
+ if (!isNaN(rows) || !isNaN(minRows)) {
node.setAttribute("rows", 1);
if (!lineHeight) {
@@ -381,8 +387,22 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
node.style.minHeight = null;
}
- var rows = Math.min(min_rows, Math.round(node.scrollHeight / lineHeight));
node.setAttribute("rows", rows);
+ }
+
+ if (isNaN(rows)) {
+ if (!isNaN(minRows) && contentHeight) {
+ var calcHeight;
+ var minHeight = lineHeight * minRows;
+
+ if (contentHeight <= minHeight) calcHeight = minHeight;
+ else calcHeight = minHeight + (contentHeight - minHeight);
+
+ node.style.height = calcHeight + "px";
+ } else if (contentHeight) {
+ node.style.height = contentHeight + 'px';
+ }
+ } else {
node.style.height = lineHeight * rows + "px";
}
diff --git a/src/components/input/input.spec.js b/src/components/input/input.spec.js
index 0da3a8f307..3ea65ea952 100644
--- a/src/components/input/input.spec.js
+++ b/src/components/input/input.spec.js
@@ -418,5 +418,89 @@ describe('md-input-container directive', function() {
var newHeight = textarea.offsetHeight;
expect(textarea.offsetHeight).toBeGreaterThan(oldHeight);
});
+
+ it('should not grow or shrink the textarea with rows attribute', function() {
+ var rows = 5;
+ createAndAppendElement('rows="' + rows +'"');
+
+ textarea.style.minHeight = '0';
+ var lineHeight = ngTextarea.prop('clientHeight');
+ textarea.style.minHeight = null;
+
+ ngTextarea.val('Multiple\nlines\nof\ntext');
+ ngTextarea.triggerHandler('input');
+
+ scope.$apply();
+ $timeout.flush();
+
+ expect(textarea.style.height).toBe(rows * lineHeight + "px");
+ });
+
+ it('should grow and shrink the textarea without any attribute', function() {
+ createAndAppendElement();
+ var lastHeight;
+
+ ngTextarea.val('Multiple\nlines\nof\ntext\nwhich\ncontains\neight\nlines');
+ ngTextarea.triggerHandler('input');
+
+ scope.$apply();
+ $timeout.flush();
+
+ lastHeight = textarea.scrollHeight;
+ expect(textarea.style.height).toBe(lastHeight + "px");
+
+ ngTextarea.val('Multiple\nlines\nof\ntext');
+ ngTextarea.triggerHandler('input');
+
+ scope.$apply();
+ $timeout.flush();
+
+ expect(textarea.style.height).toBeLessThan(lastHeight + "px");
+ lastHeight = textarea.scrollHeight;
+
+ ngTextarea.val('Multiple\nlines\nof\ntext\nwhich\ncontains\neight\nlines\nplus\nmore');
+ ngTextarea.triggerHandler('input');
+
+ scope.$apply();
+ $timeout.flush();
+
+ expect(textarea.style.height).toBeGreaterThan(lastHeight + "px");
+ });
+
+ it("should grow the textarea with min-rows attribute", function() {
+ var rows = 3;
+ var lastHeight;
+ createAndAppendElement('min-rows="' + rows +'"');
+
+ textarea.style.minHeight = '0';
+ var lineHeight = ngTextarea.prop('clientHeight');
+ textarea.style.minHeight = null;
+
+ ngTextarea.val('Multiple\nlines');
+ ngTextarea.triggerHandler('input');
+
+ scope.$apply();
+ $timeout.flush();
+
+ var minHeight = lastHeight = rows * lineHeight;
+ expect(textarea.style.height).toBe(lastHeight + "px");
+
+ ngTextarea.val('Multiple\nlines\nof\ntext\nwhich\ngot\nincreased');
+ ngTextarea.triggerHandler('input');
+
+ scope.$apply();
+ $timeout.flush();
+
+ expect(parseInt(textarea.style.height)).toBeGreaterThan(lastHeight);
+
+ ngTextarea.val('Multiple\nlines');
+ ngTextarea.triggerHandler('input');
+
+ scope.$apply();
+ $timeout.flush();
+
+ expect(textarea.style.height).not.toBeLessThan(minHeight);
+ });
+
});
});