What I'm trying to achieve. Image sample here
Hi Guys I'm still learning and trying to truncate a long text but with few conditions. Let's say I have an array of 5 words, if the text is too long, it will need to truncate the text to accommodate the width of the container. But I wanted it to cut the whole word instead of just the letters. As example object of [one, two, three, four, five]
One, two, three, ... 2 (counter) I want this
One, two, three, fou ... 1 (counter) I don't want this
Below is what I have tried but I'm not sure where to go next also to make the counter dynamic based on truncated text number instead of an array length.
function App() {
const numbers = ["one", "two", "three", "four", "five"];
const testStyle = {
display: "flex",
justifyContent: "space-between",
background: "#D0D0D0",
padding: "6px 8px",
width: "150px",
whiteSpace: "nowrap",
};
const truncatedStyle = {
textOverflow: "ellipsis",
overflow: "hidden",
};
return (
<div className="App">
<div style={testStyle}>
<div style={truncatedStyle}>
{numbers.map((n, i) => (
<span>
{n}
{i === 4 ? "" : ", "}
</span>
))}
</div>
<div
style={{ background: "black", color: "white", padding: "2px 4px" }}
>
{numbers.length}
</div>
</div>
</div>
);
}