关于前端:基于antd的文字展开折叠组件

最近因为需要,须要开发一个反对文字开展折叠的性能,看了下antd的官网,给的例子只反对开展,不反对折叠,搜查一番,找到一个Stack Overflow上的例子,传送门,依据例子批改了下。

思路:通过给Paragraph设置key,去动静的批改展现内容
代码:

import React, { PureComponent, Fragment } from 'react';
import { Typography } from 'antd';

const { Paragraph } = Typography;

export default class ParagraphExpand extends PureComponent {
  state = {
    fold: false,
    key: 0,
  };
  onExpand = () => {
    this.setState({ fold: true });
  };
  onCollapse = e => {
    e.preventDefault();
    this.setState(({ key }) => ({ fold: false, key: key + 1 }));
  };
  render() {
    const { rows = 1, content } = this.props;
    const { fold, key } = this.state;
    return (
      <Fragment>
        <div key={key}>
          <Paragraph ellipsis={{ rows, expandable: true, onExpand: this.onExpand }}>
            {content}
          </Paragraph>
        </div>
        {fold && <a href=":;" onClick={this.onCollapse}>Collapse</a>}
      </Fragment>
    );
  }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理