This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(textarea): make textarea autogrow with size of content
- Loading branch information
Showing
4 changed files
with
51 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,8 @@ angular.module('material.components.input', [ | |
]) | ||
.directive('mdInputContainer', mdInputContainerDirective) | ||
.directive('label', labelDirective) | ||
.directive('input', inputDirective) | ||
.directive('textarea', inputDirective); | ||
.directive('input', inputTextareaDirective) | ||
.directive('textarea', inputTextareaDirective); | ||
|
||
/** | ||
* @ngdoc directive | ||
|
@@ -86,7 +86,7 @@ function labelDirective() { | |
}; | ||
} | ||
|
||
function inputDirective($mdUtil) { | ||
function inputTextareaDirective($mdUtil, $window) { | ||
return { | ||
restrict: 'E', | ||
require: ['^?mdInputContainer', '?ngModel'], | ||
|
@@ -104,6 +104,10 @@ function inputDirective($mdUtil) { | |
|
||
if ( !containerCtrl ) return; | ||
|
||
if (element[0].tagName.toLowerCase() === 'textarea') { | ||
setupTextarea(); | ||
} | ||
|
||
if (containerCtrl.input) { | ||
throw new Error("<md-input-container> can only have *one* <input> or <textarea> child element!"); | ||
} | ||
|
@@ -143,10 +147,41 @@ function inputDirective($mdUtil) { | |
containerCtrl.input = null; | ||
}); | ||
|
||
function isNotEmpty(value) { | ||
value = angular.isUndefined(value) ? element.val() : value; | ||
return (angular.isDefined(value) && (value!==null) && | ||
(value.toString().trim() !== "")); | ||
function setupTextarea() { | ||
var node = element[0]; | ||
|
||
if (ngModelCtrl) { | ||
ngModelCtrl.$formatters.push(growTextarea); | ||
ngModelCtrl.$parsers.push(growTextarea); | ||
} else { | ||
element.on('input', growTextarea); | ||
growTextarea(); | ||
} | ||
element.on('keydown', growTextarea); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
element.on('scroll', onScroll); | ||
angular.element($window).on('resize', growTextarea); | ||
|
||
scope.$on('$destroy', function() { | ||
angular.element($window).off('resize', growTextarea); | ||
}); | ||
|
||
function growTextarea(value) { | ||
node.style.height = "auto"; | ||
var line = node.scrollHeight - node.offsetHeight; | ||
node.scrollTop = 0; | ||
height = node.offsetHeight + (line > 0 ? line : 0); | ||
This comment has been minimized.
Sorry, something went wrong.
gkalpak
Member
|
||
node.style.height = height + 'px'; | ||
|
||
return value; // for $formatter/$parser | ||
} | ||
|
||
function onScroll(e) { | ||
node.scrollTop = 0; | ||
// for smooth new line adding | ||
var line = node.scrollHeight - node.offsetHeight; | ||
height = node.offsetHeight + line; | ||
This comment has been minimized.
Sorry, something went wrong.
gkalpak
Member
|
||
node.style.height = height + 'px'; | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit 1c65369
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need additional CSS code for IE10, otherwise autoresize is not working.
md-input-container textarea.md-input {
-ms-flex-preferred-size: auto;
}
@ajoslin: Why is this needed ?