1. /*
    2. * @Descripttion:
    3. * @version:
    4. * @Author: WangPeng
    5. * @Date: 2021-09-24 11:11:44
    6. * @LastEditors: WangPeng
    7. * @LastEditTime: 2021-09-24 12:02:30
    8. */
    9. import { Typography } from 'antd';
    10. import React from 'react';
    11. import style from './index.less';
    12. const { Paragraph } = Typography;
    13. interface props {
    14. article: string;
    15. rows: number;
    16. symbol?: any;
    17. suffix?: string;
    18. }
    19. class Demo extends React.Component<props> {
    20. static defaultProps = {
    21. article: '', // 要展示的文本
    22. rows: 2, // 最多显示的行数 number
    23. symbol: '展开', // 自定义展开描述文案 ReactNode
    24. suffix: '', // 自定义省略内容后缀 string
    25. };
    26. state = {
    27. key: `${new Date()}`, // key 保持每一个div的key值 已备diff算法更新
    28. isShow: false,
    29. };
    30. // 控制收起
    31. onCollapse = () => {
    32. this.setState({
    33. key: `${new Date()}`,
    34. isShow: false,
    35. });
    36. };
    37. render() {
    38. const { article, rows } = this.props;
    39. const { key, isShow } = this.state;
    40. return (
    41. <div key={key}>
    42. <Paragraph
    43. ellipsis={{
    44. rows,
    45. expandable: true,
    46. symbol: '展开',
    47. suffix: '',
    48. }}
    49. >
    50. {isShow ? (
    51. article
    52. ) : (
    53. <>
    54. {article}
    55. <span className={style.collapseBtn} onClick={this.onCollapse}>
    56. 收起
    57. </span>
    58. </>
    59. )}
    60. </Paragraph>
    61. </div>
    62. );
    63. }
    64. }
    65. export default Demo;

    基于antd的Typography中的Paragraph