react-expand-text 1.0.0 → 2.0.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,5 +1,5 @@
1
1
  # react-expand-text
2
- A simple React component that shrinks and expands long text. If the `text` length is
2
+ A modern 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
4
  with an ellipsis. Clicking the text will alternately expand/collapse the long text.
5
5
 
@@ -8,37 +8,48 @@ with an ellipsis. Clicking the text will alternately expand/collapse the long te
8
8
  npm install --save react-expand-text
9
9
  ```
10
10
 
11
+ ## Requirements
12
+ - Node.js >= 18.0.0
13
+ - React >= 18.0.0
14
+
11
15
  ## Usage
12
16
  ```javascript
13
- ExpandText.propTypes = {
14
- text: React.PropTypes.string.isRequired,
15
- maxLength: React.PropTypes.number.isRequired,
16
- className: React.PropTypes.string
17
+ import React from 'react';
18
+ import ExpandText from 'react-expand-text';
19
+
20
+ const MyComponent = () => {
21
+ return (
22
+ <ExpandText
23
+ text="Your long text here"
24
+ maxLength={50}
25
+ className="optional-css-class"
26
+ />
27
+ );
17
28
  };
18
29
  ```
19
30
 
20
31
  ### Props
21
- * `text`: Text to display
22
- * `maxLength`: Max length of text
23
- * `className`: *Optional* class name to be applied to the inner span
32
+ * `text`: Text to display (string, required)
33
+ * `maxLength`: Max length of text (number, required)
34
+ * `className`: Optional CSS class name to be applied to the span element
24
35
 
25
36
  ## Example
26
37
  ```javascript
27
38
  import React from 'react';
28
- import ReactDOM from 'react-dom';
39
+ import { createRoot } from 'react-dom/client';
29
40
  import ExpandText from 'react-expand-text';
30
41
 
31
- class App extends React.Component {
32
- render() {
33
- return (
34
- <ExpandText
35
- maxLength={10}
36
- className='my-css-class'
37
- text={'I am a long string that is longer than max length'}
38
- />
39
- );
40
- }
41
- }
42
-
43
- ReactDOM.render(<App />, document.getElementById('root'));
44
- ```
42
+ const App = () => {
43
+ return (
44
+ <ExpandText
45
+ maxLength={10}
46
+ className='my-css-class'
47
+ text={'I am a long string that is longer than max length'}
48
+ />
49
+ );
50
+ };
51
+
52
+ const container = document.getElementById('root');
53
+ const root = createRoot(container);
54
+ root.render(<App />);
55
+ ```
package/example/App.js CHANGED
@@ -1,19 +1,19 @@
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
+ <ExpandText
9
+ maxLength={10}
10
+ className='my-css-class'
11
+ text={'I am a long string that is longer than max length'}
12
+ />
13
+ </div>
14
+ );
15
+ };
18
16
 
19
- ReactDOM.render(<App />, document.getElementById('root'));
17
+ const container = document.getElementById('root');
18
+ const root = createRoot(container);
19
+ 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.0.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,29 @@
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
- };
4
+ const ExpandText = ({ text, maxLength, className = "" }) => {
5
+ const [showFull, setShowFull] = useState(false);
6
+
7
+ let visibleText;
8
+ if (showFull || text.length <= maxLength) {
9
+ visibleText = text;
10
+ } else {
11
+ const firstHalf = text.substring(0, maxLength / 2);
12
+ const secondHalf = text.substring(text.length - (maxLength / 2), text.length);
13
+ visibleText = `${firstHalf}...${secondHalf}`;
9
14
  }
10
15
 
11
- render() {
12
- let visibleText = null;
13
- if (this.state.showFull || this.props.text.length <= this.props.maxLength) {
14
- visibleText = this.props.text;
15
- } 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)
18
- visibleText = `${firstHalf}...${secondHalf}`;
19
- }
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
- }
16
+ const handleClick = () => {
17
+ setShowFull(!showFull);
18
+ };
27
19
 
28
- ExpandText.propTypes = {
29
- text: React.PropTypes.string.isRequired,
30
- maxLength: React.PropTypes.number.isRequired,
31
- className: React.PropTypes.string
20
+ return <span onClick={handleClick} className={className}>{visibleText}</span>;
32
21
  };
33
22
 
34
- ExpandText.defaultProps = {
35
- className: ""
23
+ ExpandText.propTypes = {
24
+ text: PropTypes.string.isRequired,
25
+ maxLength: PropTypes.number.isRequired,
26
+ className: PropTypes.string
36
27
  };
37
28
 
38
- export default ExpandText;
29
+ 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;