tdesign-mobile-vue
Version:
tdesign-mobile-vue
65 lines (61 loc) • 2.67 kB
JavaScript
/**
* tdesign v1.9.3
* (c) 2025 TDesign Group
* @license MIT
*/
import { isNull } from 'lodash-es';
import { calculateNodeSize } from './helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/defineProperty';
var TEXTAREA_STYLE = "\n min-height:0 !important;\n max-height:none !important;\n height:0 !important;\n visibility:hidden !important;\n overflow:hidden !important;\n position:absolute !important;\n z-index:-1000 !important;\n top:0 !important;\n right:0 !important\n";
var hiddenTextarea;
function calcTextareaHeight(targetElement) {
var _hiddenTextarea;
var minRows = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var maxRows = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
if (!hiddenTextarea) {
hiddenTextarea = document.createElement("textarea");
document.body.appendChild(hiddenTextarea);
}
var _calculateNodeSize = calculateNodeSize(targetElement),
paddingSize = _calculateNodeSize.paddingSize,
borderSize = _calculateNodeSize.borderSize,
boxSizing = _calculateNodeSize.boxSizing,
sizingStyle = _calculateNodeSize.sizingStyle;
hiddenTextarea.setAttribute("style", "".concat(sizingStyle, ";").concat(TEXTAREA_STYLE));
hiddenTextarea.value = targetElement.value || targetElement.placeholder || "";
var height = hiddenTextarea.scrollHeight;
var result = {};
var isBorderbox = boxSizing === "border-box";
var isContentbox = boxSizing === "content-box";
if (isBorderbox) {
height += borderSize;
} else if (isContentbox) {
height -= paddingSize;
}
hiddenTextarea.value = "";
var singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
(_hiddenTextarea = hiddenTextarea) === null || _hiddenTextarea === void 0 || (_hiddenTextarea = _hiddenTextarea.parentNode) === null || _hiddenTextarea === void 0 || _hiddenTextarea.removeChild(hiddenTextarea);
hiddenTextarea = null;
var calcHeight = function calcHeight(rows) {
var rowsHeight = singleRowHeight * rows;
if (isBorderbox) {
rowsHeight = rowsHeight + paddingSize + borderSize;
}
return rowsHeight;
};
if (!isNull(minRows)) {
var minHeight = calcHeight(minRows);
height = Math.max(minHeight, height);
result.minHeight = "".concat(minHeight, "px");
}
if (!isNull(maxRows)) {
height = Math.min(calcHeight(maxRows), height);
}
result.height = "".concat(height, "px");
return result;
}
export { calcTextareaHeight as default };
//# sourceMappingURL=calcTextareaHeight.js.map