@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
83 lines • 3.68 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextReducer = void 0;
const react_1 = __importDefault(require("react"));
const server_1 = require("react-dom/server");
const ReactIs = __importStar(require("react-is"));
const constants_1 = require("../../configuration/constants");
const Typography_1 = require("./../Typography");
/**
* Component to reduce HTML markup content to simple text.
* Display can be wrapped easily in `OverflowText`.
*/
const TextReducer = ({ children, maxNodes, maxLength, useOverflowTextWrapper, overflowTextProps, }) => {
const nodesCount = 0;
const onlyText = (children, maxNodes) => {
if (typeof maxNodes !== "undefined" && nodesCount >= maxNodes) {
return "";
}
if (children instanceof Array) {
return children
.slice(0, maxNodes)
.map((child) => {
return onlyText(child, maxNodes);
})
.join(" ");
}
return react_1.default.Children.toArray(children)
.slice(0, maxNodes)
.map((child) => {
var _a;
if (ReactIs.isFragment(child)) {
return onlyText((_a = child.props) === null || _a === void 0 ? void 0 : _a.children, maxNodes);
}
if (typeof child === "string") {
return child;
}
if (typeof child === "number") {
return child.toString();
}
if (ReactIs.isElement(child)) {
// for some reasons `renderToString` returns empty string if not wrappe in a `span`
return (0, server_1.renderToString)(react_1.default.createElement("span", null, child));
}
return "";
})
.join(" ")
.replaceAll("\n", " ");
};
const shrinkedContent = onlyText(children, maxNodes)
.replaceAll(/<[^\s][^>]*>/g, "")
.slice(0, maxLength);
return useOverflowTextWrapper ? (react_1.default.createElement(Typography_1.OverflowText, Object.assign({}, overflowTextProps, { className: `${constants_1.CLASSPREFIX}-textreducer` +
(overflowTextProps && overflowTextProps.className ? ` ${overflowTextProps.className}` : "") }), shrinkedContent)) : (react_1.default.createElement(react_1.default.Fragment, null, shrinkedContent));
};
exports.TextReducer = TextReducer;
exports.default = exports.TextReducer;
//# sourceMappingURL=TextReducer.js.map