Giter Site home page Giter Site logo

vivaxy / react-text-more-less Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 2.0 6.59 MB

📃React component to show more text or show less

Home Page: https://github.com/vivaxy/react-text-more-less

License: MIT License

JavaScript 98.26% Shell 1.74%
react react-component shave truncate show-more truncate-text show-less more-less

react-text-more-less's Introduction

react-text-more-less

NPM Version NPM Downloads MIT License Conventional Commits

Initialized by vivaxy/gt-npm-package.

Based on dollarshaveclub/shave.

See demo.

Installation

npm install react-text-more-less

yarn add react-text-more-less

Usage

import React, { Component } from 'react';
import ReactTextMoreLess from 'react-text-more-less';

class Demo extends Component {
  state = {
    collapsed: true,
  };

  render() {
    const { collapsed } = this.state;
    return (
      <ReactTextMoreLess
        collapsed={collapsed}
        text="1926年1—6月,他一连发表了四篇论文,题目都是《量子化就是本征值问题》,系统地阐明了波动力学理论。在此以前,德国物理学家W.K.海森堡、M.玻恩和E.P.约旦于1925年7—9月通过另一途径建立了矩阵力学。1926年3月,薛定谔发现波动力学和矩阵力学在数学上是等价的,是量子力学的两种形式,可以通过数学变换,从一个理论转到另一个理论。薛定谔起初试图把波函数解释为三维空间中的振动,把振幅解释为电荷密度,把粒子解释为波包。但他无法解决“波包扩散”的困难。最后物理学界普遍接受了玻恩提出的波函数的几率解释。"
        lessHeight={72}
        showMoreText="... 展示更多"
        showMoreElement={
          <span>
            ... <span className="show-more-text">展示更多</span>
          </span>
        }
        showLessElement={<span className="show-more-text">收起</span>}
        onClick={() => {
          this.setState({ collapsed: !collapsed });
        }}
      />
    );
  }
}

If you like to prevent collapse on user select, add this before setting collapsed.

const selection = window.getSelection();
if (!selection.toString()) {
  /* Set `collapsed` prop */
}

Props

name type isRequired default description
text string N/A text in the container Be careful with XSS attacks, #82
lessHeight number N/A container collapsed height
collapsed bool true is the container collapsed according to the lessHeight
className string undefined className of the container
showMoreText string ... the ellipse text to show more
showMoreElement element undefined the element to show when container collapsed
showLessElement element undefined the element to show when container expanded
onClick func () => {} container click handler
rootProps object {} root container props, could be any props a div accepts

If showMoreElement supplied, showMoreText will be replaced, this props is useful when you want to custom showMoreText style or to use a more complex dom structure to display showMoreText.

Related Projects

Change Log

Change Log

Licence

MIT

react-text-more-less's People

Contributors

dependabot[bot] avatar jbernhardt23 avatar renovate-bot avatar vivaxy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-text-more-less's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Add support for dangerouslySetInnerHTML

Currently the component only accepts text in string form, though sometimes the data is raw HTML which errors when passed. I get this error:

Warning: Failed prop type: Invalid prop `text` of type `object` supplied to `ReactTextMoreLess`, expected `string`.
    in ReactTextMoreLess

The PropTypes for text should be object so we can pass HTML data as well

Do not collapse on user select

As a user, when the content/text is not collapsed it should be possible to select the text (for copy/paste purpose).

The text content should not collapse when the user has selected the text with the mouse. But currently, when I try to select the text, it collapses.

Word limit

Big Brother, can you add a word limit function? Pixels feel strange.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.