react-expand-text 2.1.0 → 2.2.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/README.md +20 -0
- package/build/expandtext.d.ts +9 -0
- package/build/expandtext.js +47 -0
- package/package.json +17 -6
- package/.babelrc +0 -3
- package/example/App.js +0 -28
- package/example/index.html +0 -8
- package/src/expandtext.js +0 -34
- package/webpack-example.config.js +0 -24
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@ longer than `maxLength` the text field will collapse, and truncated text will be
|
|
|
4
4
|
with an ellipsis. The ellipsis can be placed in the middle (default) or at the end of the text.
|
|
5
5
|
Clicking the text will alternately expand/collapse the long text.
|
|
6
6
|
|
|
7
|
+
**Now with full TypeScript support!** 🚀
|
|
8
|
+
|
|
7
9
|
## Installation
|
|
8
10
|
```bash
|
|
9
11
|
npm install --save react-expand-text
|
|
@@ -14,6 +16,24 @@ npm install --save react-expand-text
|
|
|
14
16
|
- React >= 18.0.0
|
|
15
17
|
|
|
16
18
|
## Usage
|
|
19
|
+
|
|
20
|
+
### TypeScript
|
|
21
|
+
```tsx
|
|
22
|
+
import React from 'react';
|
|
23
|
+
import ExpandText from 'react-expand-text';
|
|
24
|
+
|
|
25
|
+
const MyComponent: React.FC = () => {
|
|
26
|
+
return (
|
|
27
|
+
<ExpandText
|
|
28
|
+
text="Your long text here"
|
|
29
|
+
maxLength={50}
|
|
30
|
+
className="optional-css-class"
|
|
31
|
+
/>
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### JavaScript
|
|
17
37
|
```javascript
|
|
18
38
|
import React from 'react';
|
|
19
39
|
import ExpandText from 'react-expand-text';
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports["default"] = void 0;
|
|
8
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
9
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|
10
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
11
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
12
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
13
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
14
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
15
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
16
|
+
var ExpandText = function ExpandText(_ref) {
|
|
17
|
+
var text = _ref.text,
|
|
18
|
+
maxLength = _ref.maxLength,
|
|
19
|
+
_ref$className = _ref.className,
|
|
20
|
+
className = _ref$className === void 0 ? "" : _ref$className,
|
|
21
|
+
_ref$truncateAtEnd = _ref.truncateAtEnd,
|
|
22
|
+
truncateAtEnd = _ref$truncateAtEnd === void 0 ? false : _ref$truncateAtEnd;
|
|
23
|
+
var _useState = (0, _react.useState)(false),
|
|
24
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
25
|
+
showFull = _useState2[0],
|
|
26
|
+
setShowFull = _useState2[1];
|
|
27
|
+
var visibleText;
|
|
28
|
+
if (showFull || text.length <= maxLength) {
|
|
29
|
+
visibleText = text;
|
|
30
|
+
} else {
|
|
31
|
+
if (truncateAtEnd) {
|
|
32
|
+
visibleText = "".concat(text.substring(0, maxLength), "...");
|
|
33
|
+
} else {
|
|
34
|
+
var firstHalf = text.substring(0, maxLength / 2);
|
|
35
|
+
var secondHalf = text.substring(text.length - maxLength / 2, text.length);
|
|
36
|
+
visibleText = "".concat(firstHalf, "...").concat(secondHalf);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
var handleClick = function handleClick() {
|
|
40
|
+
setShowFull(!showFull);
|
|
41
|
+
};
|
|
42
|
+
return /*#__PURE__*/_react["default"].createElement("span", {
|
|
43
|
+
onClick: handleClick,
|
|
44
|
+
className: className
|
|
45
|
+
}, visibleText);
|
|
46
|
+
};
|
|
47
|
+
var _default = exports["default"] = ExpandText;
|
package/package.json
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-expand-text",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "A simple react component that shrinks and expands long text",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"types": "build/expandtext.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"build",
|
|
9
|
+
"index.js",
|
|
10
|
+
"LICENSE",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
6
13
|
"scripts": {
|
|
7
14
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
-
"example": "webpack --config webpack-example.config.js && open example/index.html",
|
|
9
|
-
"build": "babel src --out-dir build"
|
|
15
|
+
"example": "npm run build && webpack --config webpack-example.config.js && open example/index.html",
|
|
16
|
+
"build": "babel src --out-dir build --extensions \".ts,.tsx,.js,.jsx\" && tsc --emitDeclarationOnly"
|
|
10
17
|
},
|
|
11
18
|
"repository": {
|
|
12
19
|
"type": "git",
|
|
@@ -22,17 +29,21 @@
|
|
|
22
29
|
"node": ">=18.0.0"
|
|
23
30
|
},
|
|
24
31
|
"dependencies": {
|
|
32
|
+
"prop-types": "^15.8.1",
|
|
25
33
|
"react": "^18.0.0",
|
|
26
|
-
"react-dom": "^18.0.0"
|
|
27
|
-
"prop-types": "^15.8.1"
|
|
34
|
+
"react-dom": "^18.0.0"
|
|
28
35
|
},
|
|
29
36
|
"devDependencies": {
|
|
30
37
|
"@babel/cli": "^7.23.0",
|
|
31
38
|
"@babel/core": "^7.23.0",
|
|
39
|
+
"@babel/plugin-transform-react-jsx": "^7.23.0",
|
|
32
40
|
"@babel/preset-env": "^7.23.0",
|
|
33
41
|
"@babel/preset-react": "^7.23.0",
|
|
34
|
-
"@babel/
|
|
42
|
+
"@babel/preset-typescript": "^7.28.5",
|
|
43
|
+
"@types/react": "^19.2.8",
|
|
44
|
+
"@types/react-dom": "^19.2.3",
|
|
35
45
|
"babel-loader": "^9.1.0",
|
|
46
|
+
"typescript": "^5.9.3",
|
|
36
47
|
"webpack": "^5.89.0",
|
|
37
48
|
"webpack-cli": "^5.1.0"
|
|
38
49
|
}
|
package/.babelrc
DELETED
package/example/App.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { createRoot } from 'react-dom/client';
|
|
3
|
-
import ExpandText from '../src/expandtext.js';
|
|
4
|
-
|
|
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
|
-
/>
|
|
14
|
-
|
|
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/example/index.html
DELETED
package/src/expandtext.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import React, { useState } from 'react';
|
|
2
|
-
import PropTypes from 'prop-types';
|
|
3
|
-
|
|
4
|
-
const ExpandText = ({ text, maxLength, className = "", truncateAtEnd = false }) => {
|
|
5
|
-
const [showFull, setShowFull] = useState(false);
|
|
6
|
-
|
|
7
|
-
let visibleText;
|
|
8
|
-
if (showFull || text.length <= maxLength) {
|
|
9
|
-
visibleText = text;
|
|
10
|
-
} else {
|
|
11
|
-
if (truncateAtEnd) {
|
|
12
|
-
visibleText = `${text.substring(0, maxLength)}...`;
|
|
13
|
-
} else {
|
|
14
|
-
const firstHalf = text.substring(0, maxLength / 2);
|
|
15
|
-
const secondHalf = text.substring(text.length - (maxLength / 2), text.length);
|
|
16
|
-
visibleText = `${firstHalf}...${secondHalf}`;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const handleClick = () => {
|
|
21
|
-
setShowFull(!showFull);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
return <span onClick={handleClick} className={className}>{visibleText}</span>;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
ExpandText.propTypes = {
|
|
28
|
-
text: PropTypes.string.isRequired,
|
|
29
|
-
maxLength: PropTypes.number.isRequired,
|
|
30
|
-
className: PropTypes.string,
|
|
31
|
-
truncateAtEnd: PropTypes.bool
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export default ExpandText;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
|
|
3
|
-
module.exports = {
|
|
4
|
-
entry: './example/App.js',
|
|
5
|
-
output: {
|
|
6
|
-
filename: 'bundle.js',
|
|
7
|
-
path: path.resolve(__dirname, 'example')
|
|
8
|
-
},
|
|
9
|
-
module: {
|
|
10
|
-
rules: [
|
|
11
|
-
{
|
|
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/
|
|
20
|
-
}
|
|
21
|
-
]
|
|
22
|
-
},
|
|
23
|
-
mode: 'development'
|
|
24
|
-
};
|