UNPKG

@eccenca/gui-elements

Version:

GUI elements based on other libraries, usable in React application, written in Typescript.

105 lines 7.54 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TextArea = void 0; const react_1 = __importDefault(require("react")); const core_1 = require("@blueprintjs/core"); const Intent_1 = require("../../common/Intent"); const constants_1 = require("../../configuration/constants"); const Icon_1 = require("../Icon"); const useTextValidation_1 = require("./useTextValidation"); const TextArea = (_a) => { var { className = "", rows = 5, invisibleCharacterWarning, leftIcon, rightElement, wrapperDivProps, intent } = _a, otherBlueprintTextAreaProps = __rest(_a, ["className", "rows", "invisibleCharacterWarning", "leftIcon", "rightElement", "wrapperDivProps", "intent"]); const textAreaCallback = react_1.default.useCallback((textAreaElement) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; if (textAreaElement && typeof textAreaElement === "object") { let textAreaStyle; if (!textAreaElement.dataset.processed) { textAreaStyle = getComputedStyle(textAreaElement); textAreaElement.dataset.processed = "yes"; textAreaElement.dataset.paddingtop = (_a = textAreaStyle.paddingTop) !== null && _a !== void 0 ? _a : "0px"; textAreaElement.dataset.paddingleft = (_b = textAreaStyle.paddingLeft) !== null && _b !== void 0 ? _b : "0px"; textAreaElement.dataset.paddingright = (_c = textAreaStyle.paddingRight) !== null && _c !== void 0 ? _c : "0px"; } else { textAreaStyle = { paddingTop: (_d = textAreaElement.dataset.paddingtop) !== null && _d !== void 0 ? _d : "0px", paddingLeft: (_e = textAreaElement.dataset.paddingleft) !== null && _e !== void 0 ? _e : "0px", paddingRight: (_f = textAreaElement.dataset.paddingright) !== null && _f !== void 0 ? _f : "0px", }; } const textAreaElementRect = textAreaElement.getBoundingClientRect(); const wrapperElement = textAreaElement.parentElement; if (leftIcon && wrapperElement) { const leftIconElement = wrapperElement.querySelector(`.${constants_1.CLASSPREFIX}-textarea__icon`); const leftIconElementRect = leftIconElement.getBoundingClientRect(); if (parseInt(textAreaStyle.paddingTop, 10) * 2 + ((_g = leftIconElementRect.height) !== null && _g !== void 0 ? _g : 0) <= ((_h = textAreaElementRect.height) !== null && _h !== void 0 ? _h : 0)) { leftIconElement.style.setProperty("top", textAreaStyle.paddingTop); } else { leftIconElement.style.setProperty("top", `${(((_j = textAreaElementRect.height) !== null && _j !== void 0 ? _j : 0) - ((_k = leftIconElementRect.height) !== null && _k !== void 0 ? _k : 0)) * 0.5}px`); } leftIconElement.style.setProperty("left", textAreaStyle.paddingLeft); textAreaElement.style.setProperty("padding-left", `calc(${leftIconElementRect.width ? 2 : 1} * ${textAreaStyle.paddingLeft} + ${(_l = leftIconElementRect.width) !== null && _l !== void 0 ? _l : 0}px)`); leftIconElement.addEventListener("click", () => { textAreaElement.focus(); }); } if (rightElement && wrapperElement) { const rightElementElement = wrapperElement.querySelector(`.${constants_1.CLASSPREFIX}-textarea__options`); const rightElementElementRect = rightElementElement.getBoundingClientRect(); if (parseInt(textAreaStyle.paddingTop, 10) * 2 + ((_m = rightElementElementRect.height) !== null && _m !== void 0 ? _m : 0) <= ((_o = textAreaElementRect.height) !== null && _o !== void 0 ? _o : 0)) { rightElementElement.style.setProperty("top", textAreaStyle.paddingTop); } else { rightElementElement.style.setProperty("top", `${(((_p = textAreaElementRect.height) !== null && _p !== void 0 ? _p : 0) - ((_q = rightElementElementRect.height) !== null && _q !== void 0 ? _q : 0)) * 0.5}px`); } rightElementElement.style.setProperty("right", textAreaStyle.paddingRight); textAreaElement.style.setProperty("padding-right", `calc(${rightElementElementRect.width ? 2 : 1} * ${textAreaStyle.paddingRight} + ${(_r = rightElementElementRect.width) !== null && _r !== void 0 ? _r : 0}px)`); } } }, [leftIcon, rightElement]); let iconIntent; switch (intent) { case "edited": iconIntent = Intent_1.Definitions.INFO; break; case "removed": iconIntent = Intent_1.Definitions.DANGER; break; default: iconIntent = intent; break; } const maybeWrappedOnChange = (0, useTextValidation_1.useTextValidation)(Object.assign(Object.assign({}, otherBlueprintTextAreaProps), { invisibleCharacterWarning })); const textarea = (react_1.default.createElement(core_1.TextArea, Object.assign({ inputRef: textAreaCallback, className: `${constants_1.CLASSPREFIX}-textarea` + (intent ? ` ${constants_1.CLASSPREFIX}-intent--${intent}` : "") + (className ? ` ${className}` : ""), intent: intent && !["info", "edited", "removed", "neutral"].includes(intent) ? intent : undefined, spellCheck: intent === "removed" ? false : undefined, rows: rows && !otherBlueprintTextAreaProps.autoResize && !otherBlueprintTextAreaProps.growVertically ? rows : 2 }, otherBlueprintTextAreaProps, { dir: "auto", onChange: maybeWrappedOnChange }))); const _b = wrapperDivProps !== null && wrapperDivProps !== void 0 ? wrapperDivProps : {}, { className: wrapperClassName } = _b, otherWrapperDivProps = __rest(_b, ["className"]); return wrapperDivProps || leftIcon || rightElement ? (react_1.default.createElement("div", Object.assign({ className: `${constants_1.CLASSPREFIX}-textarea__wrapper` + (wrapperClassName ? ` ${wrapperClassName}` : "") }, otherWrapperDivProps), textarea, leftIcon && (react_1.default.createElement("div", { className: `${constants_1.CLASSPREFIX}-textarea__icon` }, typeof leftIcon === "string" ? (react_1.default.createElement(Icon_1.Icon, { name: leftIcon, className: core_1.Classes.ICON, intent: iconIntent })) : (react_1.default.createElement("span", { className: core_1.Classes.ICON }, leftIcon)))), rightElement && react_1.default.createElement("div", { className: `${constants_1.CLASSPREFIX}-textarea__options` }, rightElement))) : (textarea); }; exports.TextArea = TextArea; exports.default = exports.TextArea; //# sourceMappingURL=TextArea.js.map