react-truncate icon indicating copy to clipboard operation
react-truncate copied to clipboard

wrong with lines calculation when page load and window resize

Open chjiyun opened this issue 6 years ago • 6 comments

I set 'lines' to 3, but get 2 lines instead. Does this bug exist all the time?

<ReadMore
  lines={3}
  more="更多"
  less="收起"
>
  {text}
</ReadMore>

ReadMore is a Component package base on Truncate

chjiyun avatar Jul 26 '18 10:07 chjiyun

@chjiyun can you provide more info for the case to test? For example what props are passed to Truncate or at least what is the text you want to truncate.

moubi avatar Aug 10 '18 12:08 moubi

@moubi emmm, For example, i set lines of attribute to 3 or 4, but only get 2 lines of text all the time, as if this property is invalid, can you solve it?

  static propTypes = {
    children: PropTypes.node.isRequired,
    more: PropTypes.string,
    less: PropTypes.string,
    text: PropTypes.node,
    lines: PropTypes.number,
  };
  
  static defaultProps = {
    lines: 3,
    more: '更多',
    less: '收起',
  };
  constructor(...args) {
    super(...args);

    this.state = {
      expanded: false,
      truncated: false,
    };

    this.handleTruncate = this.handleTruncate.bind(this);
    this.toggleLines = this.toggleLines.bind(this);
  }

  handleTruncate(truncated) {
    if (this.state.truncated !== truncated) {
      this.setState({
        truncated,
      });
    }
  }

  toggleLines(event) {
    event.preventDefault();

    this.setState({
      expanded: !this.state.expanded,
    });
  }
<Fragment>
   <Truncate
       lines={!expanded && lines}
       ellipsis={(
          <span>... <a href='#' onClick={this.toggleLines}>{more}</a></span>
       )}
       onTruncate={this.handleTruncate}
   >
       {children}
   </Truncate>
   {!truncated && expanded && (
      <span> <a href='#' onClick={this.toggleLines}>{less}</a></span>
   )}
</Fragment>

chjiyun avatar Sep 06 '18 02:09 chjiyun

I have created a sandbox with your example (https://codesandbox.io/s/6yj42pmlww) but can't reproduce what you are describing.

Please clarify if I misunderstood your problem.

Could you edit the sandbox and send the link that exactly reproduces your bug?

pablosichert avatar Sep 08 '18 11:09 pablosichert

@PabloSichert i apply it in Row and Col (a percent Component) Component, text spreads in three columns, and my version of react-truncate is 2.3.0

chjiyun avatar Jan 04 '19 02:01 chjiyun

@PabloSichert i try to copy chinese text to your example on codesandbox, but i exactly get wrong result of lines, you can have a try.

chjiyun avatar Jan 04 '19 03:01 chjiyun

@chjiyun From my experience, Chinese is not supported well. this library uses " " as separator and split the line which is fine for English but not working for Chinese or other languages that don't separator word by space.

https://github.com/One-com/react-truncate/issues/107

Ktchan825 avatar Jul 11 '19 09:07 Ktchan825