UNPKG

@carbon/ibm-security

Version:

Carbon for Cloud & Cognitive IBM Security UI components

59 lines (58 loc) 2.1 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["className", "lines", "tooltipDirection", "truncate", "value", "width"]; /** * @file String formatter. * @copyright IBM Security 2019 */ import classnames from 'classnames'; import { bool, string, number, oneOf } from 'prop-types'; import React from 'react'; import TooltipDefinition from '../TooltipDefinition'; import { getComponentNamespace } from '../../globals/namespace'; export var namespace = getComponentNamespace('string-formatter'); var StringFormatter = function StringFormatter(_ref) { var className = _ref.className, lines = _ref.lines, tooltipDirection = _ref.tooltipDirection, truncate = _ref.truncate, value = _ref.value, width = _ref.width, other = _objectWithoutProperties(_ref, _excluded); var content = /*#__PURE__*/React.createElement("span", _extends({ className: classnames(namespace, className, _defineProperty({}, "".concat(namespace, "--truncate"), truncate)), style: { maxWidth: width, WebkitLineClamp: lines } }, other), value); return truncate ? /*#__PURE__*/React.createElement(TooltipDefinition, { className: "".concat(namespace, "__tooltip"), align: "start", direction: tooltipDirection, tooltipText: value }, content) : content; }; StringFormatter.propTypes = { /** @type {string} Optional class name. */ className: string, /** @type {number} Number of lines to clamp value. */ lines: number, /** Specify the direction of the tooltip. Can be either top or bottom. */ tooltipDirection: oneOf(['top', 'bottom']), /** Whether or not the value should be truncated. */ truncate: bool, /** Value to format. */ value: string.isRequired, /** Maximum width of value. */ width: string }; StringFormatter.defaultProps = { className: null, lines: 1, tooltipDirection: 'bottom', truncate: false, width: null }; export default StringFormatter;