@helpscout/hsds-react
Version:
React component library for Help Scout's Design System
71 lines (58 loc) • 1.83 kB
JavaScript
exports.__esModule = true;
exports.isTextArea = isTextArea;
exports.isStateful = isStateful;
exports.moveCursorToEnd = exports.getTextAreaLineCurrent = exports.getTextAreaLineTotal = void 0;
/**
* Get the total number of lines (rows) of a textarea
*
* @param {InputNode} textarea
* @returns {number}
*/
var getTextAreaLineTotal = function getTextAreaLineTotal(textarea) {
if (!textarea) return 0;
return textarea.value.split(/\r*\n/).length;
};
/**
* Get the current line (row) of the textarea based on cursor position.
*
* @param {InputNode} textarea
* @returns {number}
*/
exports.getTextAreaLineTotal = getTextAreaLineTotal;
var getTextAreaLineCurrent = function getTextAreaLineCurrent(textarea) {
if (!textarea) return 0;
return textarea.value.substr(0, textarea.selectionStart).split('\n').length;
};
/**
* Moves the cursor caret to the end of the Input value
* Source: https://css-tricks.com/snippets/javascript/move-cursor-to-end-of-input/
*
* @param {InputNode} inputNode
*/
exports.getTextAreaLineCurrent = getTextAreaLineCurrent;
var moveCursorToEnd = function moveCursorToEnd(input) {
// Extra failsafe guard
if (!input) return;
if (typeof input.selectionStart === 'number') {
input.selectionStart = input.selectionEnd = input.value.length;
} else if (typeof input.createTextRange !== 'undefined') {
input.focus();
var range = input.createTextRange();
range.collapse(false);
range.select();
}
};
/**
* Check to see if Node is a textarea.
*
* @param {HTMLElement} node
* @returns {boolean}
*/
exports.moveCursorToEnd = moveCursorToEnd;
function isTextArea(node) {
return !!(node && node.tagName === 'TEXTAREA');
}
function isStateful(props) {
return props.state && ['error', 'success', 'warning'].includes(props.state);
}
;