Skip to content

Commit 5c040f8

Browse files
authored
Merge pull request #56 from fawaz-ahmed/lib-update
update lib to deal with nested View to 1 level, update readme, update…
2 parents 96b35ec + 248c49e commit 5c040f8

File tree

5 files changed

+30
-56
lines changed

5 files changed

+30
-56
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,13 @@ export default Home;
7575
| `onExpand` | `func` | no | optional callback executed when expanded
7676
| `onCollapse` | `func` | no | optional callback executed when collapsed
7777
| `onReady` | `func` | no | optional callback executed when see more placement measurements are completed
78-
| `seeMoreContainerStyleSecondary` | `object` | no | Incase of text overlap, pass { position: 'relative' } see [issue](https://github.com/fawaz-ahmed/react-native-read-more/issues/52)
78+
| `seeMoreContainerStyleSecondary` | `object` | no | Incase of text overlap, pass { position: 'relative' } see [issue](https://github.com/fawaz-ahmed/react-native-read-more/issues/52) (not recommended)
7979

8080
Any additional props are passed down to underlying `Text` component.
8181

82+
# Usage with HTML
83+
HTML rendering is not part of this package, but can be done easily with the help of any custom html to text library. For sample code, refer to this [issue](https://github.com/fawaz-ahmed/react-native-read-more/issues/55#issuecomment-1046941770)
84+
8285
# Run example
8386
```
8487
git clone https://github.com/fawaz-ahmed/react-native-read-more.git
@@ -93,13 +96,14 @@ yarn android
9396
```
9497

9598
# Why another library ?
96-
This module will calculate where to position `See more` and `See less` within the same paragraph instead of occupying another line. It is a drop-in replacement for `Text` component and you can control when to apply the see more functionality by configuring the `numberOfLines` prop. Moreover, you can also pass your own custom implementation of `Text` component like `ParsedText` etc.
99+
This module will calculate where to position `See more` and `See less` within the same paragraph instead of occupying another line. It is a drop-in replacement for `Text` component and you can control when to apply the see more functionality by configuring the `numberOfLines` prop. Moreover, you can also pass your own custom implementation of `Text` component like `ParsedText` ([sample code](https://github.com/fawaz-ahmed/react-native-read-more/issues/37#issuecomment-1047029209)) etc.
97100

98101
## Seeing issues or any feedback or feature suggest ?
99102
Create an [issue](https://github.com/fawaz-ahmed/react-native-read-more/issues) with github.
100103

101104
## Troubleshooting
102105
- If you observe `See more` shown always in android, pass prop `allowFontScaling={false}`, refer to this [issue](https://github.com/fawaz-ahmed/react-native-read-more/issues/17)
106+
- If you have any nested components other than `Text`, refer to this [issue](https://github.com/fawaz-ahmed/react-native-read-more/issues/52)
103107

104108
### jest - running unit tests
105109
This package is not transpiled. So inorder for your test cases to work, this package should be transpiled by babel. For this you need to add this path `!node_modules/@fawazahmed/react-native-read-more/` under `transformIgnorePatterns` option provided by `jest`. In your `package.json` you will see this `jest` config:

example/src/ReadMore.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
LayoutAnimation,
88
Platform,
99
UIManager,
10+
TextPropTypes,
1011
} from 'react-native';
11-
import {getText, insertAt, linesToCharacters} from './helper';
12+
import {getTextByChildren, insertAt, linesToCharacters} from './helper';
1213

1314
if (Platform.OS === 'android') {
1415
if (UIManager.setLayoutAnimationEnabledExperimental) {
@@ -298,7 +299,7 @@ const ReadMore = ({
298299
// go to this position and insert \n
299300
let charactersToTraverse = textBreakPosition;
300301
let nodeFound = false;
301-
const modifiedChildrenObjects = getText(children, TextComponent, true)
302+
const modifiedChildrenObjects = getTextByChildren(children, TextComponent)
302303
?.map(_childObject => {
303304
if (nodeFound) {
304305
return _childObject;
@@ -671,12 +672,10 @@ const styles = StyleSheet.create({
671672
});
672673

673674
ReadMore.propTypes = {
674-
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
675+
...TextPropTypes,
675676
seeMoreStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
676677
seeLessStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
677678
wrapperStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
678-
children: PropTypes.any,
679-
numberOfLines: PropTypes.number,
680679
seeMoreText: PropTypes.string,
681680
seeLessText: PropTypes.string,
682681
animate: PropTypes.bool,
@@ -686,14 +685,11 @@ ReadMore.propTypes = {
686685
PropTypes.elementType,
687686
]),
688687
ellipsis: PropTypes.string,
689-
allowFontScaling: PropTypes.bool,
690688
onExpand: PropTypes.func,
691689
onCollapse: PropTypes.func,
692690
expandOnly: PropTypes.bool,
693691
seeMoreOverlapCount: PropTypes.number,
694692
debounceSeeMoreCalc: PropTypes.number,
695-
onLayout: PropTypes.func,
696-
onTextLayout: PropTypes.func,
697693
onReady: PropTypes.func,
698694
seeMoreContainerStyleSecondary: PropTypes.object,
699695
};
@@ -703,7 +699,6 @@ ReadMore.defaultProps = {
703699
seeMoreStyle: StyleSheet.flatten([styles.defaultText, styles.seeMoreText]),
704700
seeLessStyle: StyleSheet.flatten([styles.defaultText, styles.seeLessText]),
705701
wrapperStyle: styles.container,
706-
text: '',
707702
numberOfLines: 3,
708703
seeMoreText: 'See more',
709704
seeLessText: 'See less',

example/src/helper.js

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,51 @@
11
import React from 'react';
22

3-
const getStringChild = (child, preserveLinebreaks = false) => {
4-
const content = preserveLinebreaks ? child : child?.split('\n').join(' ');
3+
const getStringChild = child => {
54
return {
65
type: 'string',
7-
content,
8-
child: content,
6+
content: child,
7+
child,
98
};
109
};
1110

12-
const getTextChild = (child, preserveLinebreaks = false) => {
13-
const content = preserveLinebreaks
14-
? child.props.children
15-
: child.props.children?.split('\n').join(' ');
11+
const getTextChild = child => {
1612
return {
1713
type: child?.type?.displayName,
18-
content,
19-
child: React.cloneElement(child, child.props, content),
14+
content: child.props.children,
15+
child: React.cloneElement(child, child.props, child.props.children),
2016
};
2117
};
2218

23-
export const getText = (children, TextComponent, preserveLinebreaks) => {
19+
export const getTextByChildren = (children, TextComponent) => {
2420
if (typeof children === 'string') {
25-
return [getStringChild(children, preserveLinebreaks)];
21+
return [getStringChild(children)];
22+
}
23+
24+
if (typeof children === 'object' && children.props?.children) {
25+
return getTextByChildren(React.Children.toArray(children.props.children));
2626
}
2727

2828
if (Array.isArray(children)) {
2929
return children
30-
.filter((_child) => {
30+
.filter(_child => {
3131
return (
3232
typeof _child === 'string' ||
3333
_child?.type?.displayName === TextComponent?.displayName
3434
);
3535
})
36-
.map((_child) => {
36+
.map(_child => {
3737
if (typeof _child === 'string') {
38-
return getStringChild(_child, preserveLinebreaks);
38+
return getStringChild(_child);
3939
}
40-
4140
return getTextChild(_child);
4241
});
4342
}
4443

4544
return null;
4645
};
4746

48-
export const childrenToText = (children, TextComponent, preserveLinebreaks) => {
49-
const _textChildren = getText(children, TextComponent, preserveLinebreaks);
50-
return _textChildren.map((_t) => _t.content).join(' ');
51-
};
52-
53-
export const childrenToTextChildren = (
54-
children,
55-
TextComponent,
56-
preserveLinebreaks,
57-
) => {
58-
const _textChildren = getText(children, TextComponent, preserveLinebreaks);
59-
return _textChildren.map((_t) => _t.child);
60-
};
61-
62-
export const childrenObjectsToChildren = (childrenObjects) => {
63-
return childrenObjects.map((_t) => _t.child);
64-
};
65-
66-
export const linesToCharacters = (lines) => {
67-
return lines.map((_line) => _line?.text || '').join('');
47+
export const linesToCharacters = lines => {
48+
return lines.map(_line => _line?.text || '').join('');
6849
};
6950

7051
export const insertAt = (str, sub, pos) =>

index.d.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
import React from 'react';
2-
import { StyleProp, TextStyle, ViewStyle, LayoutChangeEvent, NativeSyntheticEvent, TextLayoutEventData } from 'react-native';
2+
import { StyleProp, TextStyle, ViewStyle, TextProps } from 'react-native';
33

4-
export interface ReadMoreProps {
5-
style?: StyleProp<TextStyle>;
4+
export interface ReadMoreProps extends TextProps {
65
seeMoreStyle?: StyleProp<TextStyle>;
76
seeLessStyle?: StyleProp<TextStyle>;
87
wrapperStyle?: StyleProp<ViewStyle>;
9-
children?: React.ReactNode;
10-
numberOfLines?: number;
118
seeMoreText?: string;
129
seeLessText?: string;
1310
animate?: boolean;
1411
customTextComponent?: React.ReactNode;
1512
ellipsis?: string;
16-
allowFontScaling?: boolean;
1713
onExpand?: () => void;
1814
onCollapse?: () => void;
1915
expandOnly?: boolean;
2016
seeMoreOverlapCount?: number;
2117
debounceSeeMoreCalc?: number;
22-
onLayout?: (event: LayoutChangeEvent) => void;
23-
onTextLayout?: (event: NativeSyntheticEvent<TextLayoutEventData>) => void;
2418
onReady?: () => void;
2519
seeMoreContainerStyleSecondary: StyleProp<ViewStyle>;
2620
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fawazahmed/react-native-read-more",
3-
"version": "2.3.2",
3+
"version": "2.3.3",
44
"description": "A simple react native library to show large blocks of text in a condensed manner with the ability to collapse and expand.",
55
"main": "index.js",
66
"types": "index.d.ts",

0 commit comments

Comments
 (0)