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 +1 -3
- package/README.md +63 -18
- package/example/App.js +24 -15
- package/package.json +15 -10
- package/src/expandtext.js +24 -28
- package/webpack-example.config.js +12 -8
- package/.npmignore +0 -4
- package/build/expandtext.js +0 -71
package/.babelrc
CHANGED
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.
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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`:
|
|
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
|
-
|
|
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
|
|
62
|
+
import { createRoot } from 'react-dom/client';
|
|
29
63
|
import ExpandText from 'react-expand-text';
|
|
30
64
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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={
|
|
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
|
-
|
|
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
|
|
2
|
+
import { createRoot } from 'react-dom/client';
|
|
3
3
|
import ExpandText from '../src/expandtext.js';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
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
|
|
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": "^
|
|
23
|
-
"react-dom": "^
|
|
25
|
+
"react": "^18.0.0",
|
|
26
|
+
"react-dom": "^18.0.0",
|
|
27
|
+
"prop-types": "^15.8.1"
|
|
24
28
|
},
|
|
25
29
|
"devDependencies": {
|
|
26
|
-
"babel
|
|
27
|
-
"babel
|
|
28
|
-
"babel-
|
|
29
|
-
"babel
|
|
30
|
-
"babel-
|
|
31
|
-
"babel-
|
|
32
|
-
"webpack": "^
|
|
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
|
-
|
|
4
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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 =
|
|
17
|
-
const secondHalf =
|
|
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
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
const handleClick = () => {
|
|
21
|
+
setShowFull(!showFull);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return <span onClick={handleClick} className={className}>{visibleText}</span>;
|
|
32
25
|
};
|
|
33
26
|
|
|
34
|
-
ExpandText.
|
|
35
|
-
|
|
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
|
-
|
|
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
|
-
|
|
10
|
+
rules: [
|
|
11
11
|
{
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
package/build/expandtext.js
DELETED
|
@@ -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;
|