react-expand-text 1.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.babelrc CHANGED
@@ -1,5 +1,3 @@
1
1
  {
2
- "plugins": ["transform-react-jsx"],
3
- "presets": ["es2015", "stage-2"],
4
- "ignore": []
2
+ "presets": ["@babel/preset-env", "@babel/preset-react"]
5
3
  }
package/README.md CHANGED
@@ -1,44 +1,89 @@
1
1
  # react-expand-text
2
2
  A simple React component that shrinks and expands long text. If the `text` length is
3
3
  longer than `maxLength` the text field will collapse, and truncated text will be replaced
4
- with an ellipsis. Clicking the text will alternately expand/collapse the long text.
4
+ with an ellipsis. The ellipsis can be placed in the middle (default) or at the end of the text.
5
+ Clicking the text will alternately expand/collapse the long text.
5
6
 
6
7
  ## Installation
7
8
  ```bash
8
9
  npm install --save react-expand-text
9
10
  ```
10
11
 
12
+ ## Requirements
13
+ - Node.js >= 18.0.0
14
+ - React >= 18.0.0
15
+
11
16
  ## Usage
12
17
  ```javascript
13
- ExpandText.propTypes = {
14
- text: React.PropTypes.string.isRequired,
15
- maxLength: React.PropTypes.number.isRequired,
16
- className: React.PropTypes.string
18
+ import React from 'react';
19
+ import ExpandText from 'react-expand-text';
20
+
21
+ const MyComponent = () => {
22
+ return (
23
+ <ExpandText
24
+ text="Your long text here"
25
+ maxLength={50}
26
+ className="optional-css-class"
27
+ />
28
+ );
17
29
  };
18
30
  ```
19
31
 
20
32
  ### Props
21
- * `text`: Text to display
22
- * `maxLength`: Max length of text
23
- * `className`: *Optional* class name to be applied to the inner span
33
+ * `text`: Text to display (string, required)
34
+ * `maxLength`: Max length of text (number, required)
35
+ * `className`: Optional CSS class name to be applied to the span element
36
+ * `truncateAtEnd`: If true, truncates at the end with ellipsis. If false (default), shows beginning and end with ellipsis in the middle (boolean, optional)
37
+
38
+ ## Examples
24
39
 
25
- ## Example
40
+ ### Middle truncation (default)
41
+ ```javascript
42
+ <ExpandText
43
+ maxLength={30}
44
+ text="I am a long string that is longer than max length"
45
+ />
46
+ // Output: "I am a long st...max length" (click to expand)
47
+ ```
48
+
49
+ ### End truncation
50
+ ```javascript
51
+ <ExpandText
52
+ maxLength={30}
53
+ text="I am a long string that is longer than max length"
54
+ truncateAtEnd
55
+ />
56
+ // Output: "I am a long string that is lo..." (click to expand)
57
+ ```
58
+
59
+ ### Complete example
26
60
  ```javascript
27
61
  import React from 'react';
28
- import ReactDOM from 'react-dom';
62
+ import { createRoot } from 'react-dom/client';
29
63
  import ExpandText from 'react-expand-text';
30
64
 
31
- class App extends React.Component {
32
- render() {
33
- return (
65
+ const App = () => {
66
+ return (
67
+ <div>
68
+ <h3>Middle truncation (default):</h3>
69
+ <ExpandText
70
+ maxLength={30}
71
+ className='my-css-class'
72
+ text={'I am a long string that is longer than max length'}
73
+ />
74
+
75
+ <h3>End truncation:</h3>
34
76
  <ExpandText
35
- maxLength={10}
77
+ maxLength={30}
36
78
  className='my-css-class'
37
79
  text={'I am a long string that is longer than max length'}
80
+ truncateAtEnd
38
81
  />
39
- );
40
- }
41
- }
82
+ </div>
83
+ );
84
+ };
42
85
 
43
- ReactDOM.render(<App />, document.getElementById('root'));
86
+ const container = document.getElementById('root');
87
+ const root = createRoot(container);
88
+ root.render(<App />);
44
89
  ```
package/example/App.js CHANGED
@@ -1,19 +1,28 @@
1
1
  import React from 'react';
2
- import ReactDOM from 'react-dom';
2
+ import { createRoot } from 'react-dom/client';
3
3
  import ExpandText from '../src/expandtext.js';
4
4
 
5
- class App extends React.Component {
6
- render() {
7
- return (
8
- <div>
9
- <ExpandText
10
- maxLength={10}
11
- className='my-css-class'
12
- text={'I am a long string that is longer than max length'}
13
- />
14
- </div>
15
- );
16
- }
17
- }
5
+ const App = () => {
6
+ return (
7
+ <div>
8
+ <h3>Middle truncation (default):</h3>
9
+ <ExpandText
10
+ maxLength={30}
11
+ className='my-css-class'
12
+ text={'I am a long string that is longer than max length'}
13
+ />
18
14
 
19
- ReactDOM.render(<App />, document.getElementById('root'));
15
+ <h3>End truncation:</h3>
16
+ <ExpandText
17
+ maxLength={30}
18
+ className='my-css-class'
19
+ text={'I am a long string that is longer than max length'}
20
+ truncateAtEnd
21
+ />
22
+ </div>
23
+ );
24
+ };
25
+
26
+ const container = document.getElementById('root');
27
+ const root = createRoot(container);
28
+ root.render(<App />);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-expand-text",
3
- "version": "1.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A simple react component that shrinks and expands long text",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,17 +18,22 @@
18
18
  "url": "https://github.com/madjemian/react-expand-text/issues"
19
19
  },
20
20
  "homepage": "https://github.com/madjemian/react-expand-text#readme",
21
+ "engines": {
22
+ "node": ">=18.0.0"
23
+ },
21
24
  "dependencies": {
22
- "react": "^15.6.1",
23
- "react-dom": "^15.6.1"
25
+ "react": "^18.0.0",
26
+ "react-dom": "^18.0.0",
27
+ "prop-types": "^15.8.1"
24
28
  },
25
29
  "devDependencies": {
26
- "babel-cli": "^6.24.1",
27
- "babel-core": "^6.25.0",
28
- "babel-loader": "^7.1.1",
29
- "babel-preset-es2015": "^6.24.1",
30
- "babel-preset-react": "^6.24.1",
31
- "babel-preset-stage-2": "^6.24.1",
32
- "webpack": "^3.1.0"
30
+ "@babel/cli": "^7.23.0",
31
+ "@babel/core": "^7.23.0",
32
+ "@babel/preset-env": "^7.23.0",
33
+ "@babel/preset-react": "^7.23.0",
34
+ "@babel/plugin-transform-react-jsx": "^7.23.0",
35
+ "babel-loader": "^9.1.0",
36
+ "webpack": "^5.89.0",
37
+ "webpack-cli": "^5.1.0"
33
38
  }
34
39
  }
package/src/expandtext.js CHANGED
@@ -1,38 +1,34 @@
1
- import React from 'react';
1
+ import React, { useState } from 'react';
2
+ import PropTypes from 'prop-types';
2
3
 
3
- class ExpandText extends React.Component {
4
- constructor(props) {
5
- super(props);
6
- this.state = {
7
- showFull: false
8
- };
9
- }
4
+ const ExpandText = ({ text, maxLength, className = "", truncateAtEnd = false }) => {
5
+ const [showFull, setShowFull] = useState(false);
10
6
 
11
- render() {
12
- let visibleText = null;
13
- if (this.state.showFull || this.props.text.length <= this.props.maxLength) {
14
- visibleText = this.props.text;
7
+ let visibleText;
8
+ if (showFull || text.length <= maxLength) {
9
+ visibleText = text;
10
+ } else {
11
+ if (truncateAtEnd) {
12
+ visibleText = `${text.substring(0, maxLength)}...`;
15
13
  } else {
16
- const firstHalf = this.props.text.substring(0, this.props.maxLength / 2);
17
- const secondHalf = this.props.text.substring(this.props.text.length - (this.props.maxLength / 2), this.props.text.length)
14
+ const firstHalf = text.substring(0, maxLength / 2);
15
+ const secondHalf = text.substring(text.length - (maxLength / 2), text.length);
18
16
  visibleText = `${firstHalf}...${secondHalf}`;
19
17
  }
20
- const self = this;
21
- const clickHandler = () => {
22
- self.setState({showFull: !self.state.showFull});
23
- }
24
- return <span onClick={clickHandler} className={this.props.className}>{visibleText}</span>;
25
- }
26
- }
18
+ }
27
19
 
28
- ExpandText.propTypes = {
29
- text: React.PropTypes.string.isRequired,
30
- maxLength: React.PropTypes.number.isRequired,
31
- className: React.PropTypes.string
20
+ const handleClick = () => {
21
+ setShowFull(!showFull);
22
+ };
23
+
24
+ return <span onClick={handleClick} className={className}>{visibleText}</span>;
32
25
  };
33
26
 
34
- ExpandText.defaultProps = {
35
- className: ""
27
+ ExpandText.propTypes = {
28
+ text: PropTypes.string.isRequired,
29
+ maxLength: PropTypes.number.isRequired,
30
+ className: PropTypes.string,
31
+ truncateAtEnd: PropTypes.bool
36
32
  };
37
33
 
38
- export default ExpandText;
34
+ export default ExpandText;
@@ -1,4 +1,4 @@
1
- var path = require('path');
1
+ const path = require('path');
2
2
 
3
3
  module.exports = {
4
4
  entry: './example/App.js',
@@ -7,14 +7,18 @@ module.exports = {
7
7
  path: path.resolve(__dirname, 'example')
8
8
  },
9
9
  module: {
10
- loaders: [
10
+ rules: [
11
11
  {
12
- loader: 'babel-loader',
13
- exclude: /node_modules/,
14
- query: {
15
- presets: ['es2015', 'react']
16
- }
12
+ test: /\.js$/,
13
+ use: {
14
+ loader: 'babel-loader',
15
+ options: {
16
+ presets: ['@babel/preset-env', '@babel/preset-react']
17
+ }
18
+ },
19
+ exclude: /node_modules/
17
20
  }
18
21
  ]
19
- }
22
+ },
23
+ mode: 'development'
20
24
  };
package/.npmignore DELETED
@@ -1,4 +0,0 @@
1
- node_modules
2
- example/transpiled.js
3
- npm-debug.log
4
- example/bundle.js
@@ -1,71 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
-
9
- var _react = require("react");
10
-
11
- var _react2 = _interopRequireDefault(_react);
12
-
13
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
-
15
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
16
-
17
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
18
-
19
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
20
-
21
- var ExpandText = function (_React$Component) {
22
- _inherits(ExpandText, _React$Component);
23
-
24
- function ExpandText(props) {
25
- _classCallCheck(this, ExpandText);
26
-
27
- var _this = _possibleConstructorReturn(this, (ExpandText.__proto__ || Object.getPrototypeOf(ExpandText)).call(this, props));
28
-
29
- _this.state = {
30
- showFull: false
31
- };
32
- return _this;
33
- }
34
-
35
- _createClass(ExpandText, [{
36
- key: "render",
37
- value: function render() {
38
- var visibleText = null;
39
- if (this.state.showFull || this.props.text.length <= this.props.maxLength) {
40
- visibleText = this.props.text;
41
- } else {
42
- var firstHalf = this.props.text.substring(0, this.props.maxLength / 2);
43
- var secondHalf = this.props.text.substring(this.props.text.length - this.props.maxLength / 2, this.props.text.length);
44
- visibleText = firstHalf + "..." + secondHalf;
45
- }
46
- var self = this;
47
- var clickHandler = function clickHandler() {
48
- self.setState({ showFull: !self.state.showFull });
49
- };
50
- return _react2.default.createElement(
51
- "span",
52
- { onClick: clickHandler, className: this.props.className },
53
- visibleText
54
- );
55
- }
56
- }]);
57
-
58
- return ExpandText;
59
- }(_react2.default.Component);
60
-
61
- ExpandText.propTypes = {
62
- text: _react2.default.PropTypes.string.isRequired,
63
- maxLength: _react2.default.PropTypes.number.isRequired,
64
- className: _react2.default.PropTypes.string
65
- };
66
-
67
- ExpandText.defaultProps = {
68
- className: ""
69
- };
70
-
71
- exports.default = ExpandText;