support children

This commit is contained in:
Severiano Badajoz
2020-05-27 13:53:45 -07:00
parent 13d24b623c
commit 8c6d806d47
+22 -5
View File
@@ -1,4 +1,4 @@
import React, { Component } from "react"; import React, { Component, cloneElement } from "react";
import { Tooltip, Position } from "@blueprintjs/core"; import { Tooltip, Position } from "@blueprintjs/core";
import pixelWidth from "string-pixel-width"; import pixelWidth from "string-pixel-width";
@@ -53,17 +53,30 @@ export default class Truncate extends Component {
render() { render() {
const { children, size, fontSize } = this.props; const { children, size, fontSize } = this.props;
const originalString = children[0].text; // Truncate only support a single child with a text child
if (
React.Children.count(children) === 1 &&
React.Children.count(children.props?.children) === 1
) {
const originalString = children.props.children;
const truncatedString = this.maybeTruncateString( const truncatedString = this.maybeTruncateString(
originalString, originalString,
size, size,
fontSize fontSize
); );
children[0].text = truncatedString; // Only make tooltip if string has to be truncated
if (truncatedString) {
// clone children, changing the children(text) to the truncated string
const newChildren = React.Children.map(children, (child) =>
cloneElement(child, {
children: truncatedString,
"data-truncated": true,
})
);
return ( return (
<Tooltip <Tooltip
content={originalString} content={originalString}
disabled={truncatedString === null}
hoverOpenDelay={tooltipHoverOpenDelayQuick} hoverOpenDelay={tooltipHoverOpenDelayQuick}
position={Position.LEFT} position={Position.LEFT}
usePortal usePortal
@@ -72,8 +85,12 @@ export default class Truncate extends Component {
hide: { enabled: false }, hide: { enabled: false },
}} }}
> >
{children} {newChildren}
</Tooltip> </Tooltip>
); );
} }
return children;
}
throw Error("Only pass a single child with inner text to Truncate");
}
} }