Skip to content

Commit

Permalink
MDL-59758 js: auto_rows module easier to use
Browse files Browse the repository at this point in the history
Allow passing the direct text area, or a container.
Trigger change events on the text area, not the container.
Properly handle starting with rows > 1
  • Loading branch information
Damyon Wiese committed Nov 7, 2017
1 parent 159b4e5 commit b9ae53f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/amd/build/auto_rows.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 32 additions & 11 deletions lib/amd/src/auto_rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ define(['jquery'], function($) {
*/
var calculateRows = function(element) {
var currentRows = element.attr('rows');
var minRows = element.data('min-rows');
var maxRows = element.attr('data-max-rows');

var height = element.height();
Expand All @@ -58,13 +59,38 @@ define(['jquery'], function($) {
// based on the row attribute.
element.css('height', '');

if (maxRows && rows >= maxRows) {
if (rows < minRows) {
return minRows;
} else if (maxRows && rows >= maxRows) {
return maxRows;
} else {
return rows;
}
};

/**
* Listener for change events to trigger resizing of the element.
*
* @method changeListener
* @param {Event} e The triggered event.
* @private
*/
var changeListener = function(root, e) {
var element = $(e.target);
var minRows = element.data('min-rows');
var currentRows = element.attr('rows');

if (typeof minRows === "undefined") {
element.data('min-rows', currentRows);
}
var rows = calculateRows(element);

if (rows != currentRows) {
element.attr('rows', rows);
element.trigger(EVENTS.ROW_CHANGE);
}
};

/**
* Add the event listeners for all text areas within the given element.
*
Expand All @@ -73,16 +99,11 @@ define(['jquery'], function($) {
* @public
*/
var init = function(root) {
$(root).on('input propertychange', SELECTORS.ELEMENT, function(e) {
var element = $(e.target);
var currentRows = element.attr('rows');
var rows = calculateRows(element);

if (rows != currentRows) {
element.attr('rows', rows);
$(root).trigger(EVENTS.ROW_CHANGE);
}
});
if ($(root).data('auto-rows')) {
$(root).on('input propertychange', changeListener.bind(this, root));
} else {
$(root).on('input propertychange', SELECTORS.ELEMENT, changeListener.bind(this, root));
}
};

return /** @module core/auto_rows */ {
Expand Down

0 comments on commit b9ae53f

Please sign in to comment.