bluepeter / react-middle-ellipsis

Truncate a long string in the middle, instead of the end.
https://bluepeter.github.io/react-middle-ellipsis/
MIT License
63 stars 10 forks source link

inner text not updating when new value passed in #4

Open 4m2c4bl3 opened 4 years ago

4m2c4bl3 commented 4 years ago

There seems to be an issue where the value inside the ellipsis is not updating when the text is long enough to be truncated. Here's a demo: https://codesandbox.io/s/demo-middle-ellipsis-l8wq1

bluepeter commented 4 years ago

What browser? It appears ellipsed for me in Chrome?

screenshot-codesandbox io-2019 12

4m2c4bl3 commented 4 years ago

Try changing the text in the input to a shorter text, you'll see the value does not change. I'm using the latest version of Chrome in Ubuntu. image

To work around this behavior, I'm giving the MiddleEllipsis component a key based off the input value, which forces it to re-render.

bluepeter commented 4 years ago

I see... yeah, that looks to be a problem. Possibly related to https://github.com/bluepeter/react-middle-ellipsis/issues/2

axeldouglas commented 3 years ago

Putting the children as useCallback prop:

measuredParent = useCallback(node => {
    if (node !== null) {
        window.addEventListener("resize", () => prepEllipse(node));
        prepEllipse(node);
        return () => window.removeEventListener("resize", () => prepEllipse(node));
    }
}, [props.children]);

And removing this if:

// (Re)-set text back to data-original-text if it exists.
if (txtToEllipse.hasAttribute('data-original')) {
    txtToEllipse.textContent = txtToEllipse.getAttribute( 'data-original');
}

Worked for me.