react-expand-text 2.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/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # react-expand-text
2
- A modern React component that shrinks and expands long text. If the `text` length is
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
@@ -32,8 +33,30 @@ const MyComponent = () => {
32
33
  * `text`: Text to display (string, required)
33
34
  * `maxLength`: Max length of text (number, required)
34
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)
35
37
 
36
- ## Example
38
+ ## Examples
39
+
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
37
60
  ```javascript
38
61
  import React from 'react';
39
62
  import { createRoot } from 'react-dom/client';
@@ -41,15 +64,26 @@ import ExpandText from 'react-expand-text';
41
64
 
42
65
  const App = () => {
43
66
  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
- />
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>
76
+ <ExpandText
77
+ maxLength={30}
78
+ className='my-css-class'
79
+ text={'I am a long string that is longer than max length'}
80
+ truncateAtEnd
81
+ />
82
+ </div>
49
83
  );
50
84
  };
51
85
 
52
86
  const container = document.getElementById('root');
53
87
  const root = createRoot(container);
54
88
  root.render(<App />);
55
- ```
89
+ ```
package/example/App.js CHANGED
@@ -5,11 +5,20 @@ import ExpandText from '../src/expandtext.js';
5
5
  const App = () => {
6
6
  return (
7
7
  <div>
8
+ <h3>Middle truncation (default):</h3>
8
9
  <ExpandText
9
- maxLength={10}
10
+ maxLength={30}
10
11
  className='my-css-class'
11
12
  text={'I am a long string that is longer than max length'}
12
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
+ />
13
22
  </div>
14
23
  );
15
24
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-expand-text",
3
- "version": "2.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": {
package/src/expandtext.js CHANGED
@@ -1,16 +1,20 @@
1
1
  import React, { useState } from 'react';
2
2
  import PropTypes from 'prop-types';
3
3
 
4
- const ExpandText = ({ text, maxLength, className = "" }) => {
4
+ const ExpandText = ({ text, maxLength, className = "", truncateAtEnd = false }) => {
5
5
  const [showFull, setShowFull] = useState(false);
6
6
 
7
7
  let visibleText;
8
8
  if (showFull || text.length <= maxLength) {
9
9
  visibleText = text;
10
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}`;
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
+ }
14
18
  }
15
19
 
16
20
  const handleClick = () => {
@@ -23,7 +27,8 @@ const ExpandText = ({ text, maxLength, className = "" }) => {
23
27
  ExpandText.propTypes = {
24
28
  text: PropTypes.string.isRequired,
25
29
  maxLength: PropTypes.number.isRequired,
26
- className: PropTypes.string
30
+ className: PropTypes.string,
31
+ truncateAtEnd: PropTypes.bool
27
32
  };
28
33
 
29
34
  export default ExpandText;