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 +1 -3
- package/README.md +34 -23
- package/example/App.js +15 -15
- package/package.json +15 -10
- package/src/expandtext.js +21 -30
- 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,5 +1,5 @@
|
|
|
1
1
|
# react-expand-text
|
|
2
|
-
A
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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`:
|
|
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
|
|
39
|
+
import { createRoot } from 'react-dom/client';
|
|
29
40
|
import ExpandText from 'react-expand-text';
|
|
30
41
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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": "
|
|
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": "^
|
|
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,29 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
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.
|
|
35
|
-
|
|
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
|
-
|
|
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;
|