@fawazahmed/react-native-read-more
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -13,4 +13,4 @@ { | ||
"dependencies": { | ||
"react": "16.11.0", | ||
"react-native": "0.62.2" | ||
"react": "16.13.1", | ||
"react-native": "0.63.2" | ||
}, | ||
@@ -17,0 +17,0 @@ "devDependencies": { |
@@ -1,2 +0,9 @@ | ||
import React, { memo, useState, useEffect, useCallback, Component, PureComponent } from 'react'; | ||
import React, { | ||
memo, | ||
useState, | ||
useEffect, | ||
useCallback, | ||
Component, | ||
PureComponent, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
@@ -19,102 +26,114 @@ import { | ||
const ReadMore = memo(({ | ||
numberOfLines, | ||
style, | ||
wrapperStyle, | ||
children, | ||
seeMoreStyle, | ||
seeMoreText, | ||
seeLessStyle, | ||
seeLessText, | ||
animate, | ||
backgroundColor, | ||
customTextComponent: TextComponent, | ||
...restProps | ||
}) => { | ||
const [textHeight, setTextHeight] = useState(0); | ||
const [hiddenTextHeight, setHiddenTextHeight] = useState(0); | ||
const [seeMore, setSeeMore] = useState(false); | ||
const [collapsed, setCollapsed] = useState(true); | ||
const [afterCollapsed, setAfterCollapsed] = useState(true); | ||
const ReadMore = memo( | ||
({ | ||
numberOfLines, | ||
style, | ||
wrapperStyle, | ||
children, | ||
seeMoreStyle, | ||
seeMoreText, | ||
seeLessStyle, | ||
seeLessText, | ||
animate, | ||
backgroundColor, | ||
customTextComponent: TextComponent, | ||
...restProps | ||
}) => { | ||
const [textHeight, setTextHeight] = useState(0); | ||
const [hiddenTextHeight, setHiddenTextHeight] = useState(0); | ||
const [seeMore, setSeeMore] = useState(false); | ||
const [collapsed, setCollapsed] = useState(true); | ||
const [afterCollapsed, setAfterCollapsed] = useState(true); | ||
const onTextLayout = useCallback(({ nativeEvent: { layout: { height } } }) => { | ||
setTextHeight(height); | ||
}, [setTextHeight]); | ||
const onTextLayout = useCallback( | ||
({ | ||
nativeEvent: { | ||
layout: {height}, | ||
}, | ||
}) => { | ||
setTextHeight(height); | ||
}, | ||
[setTextHeight], | ||
); | ||
const onHiddenTextLayout = useCallback(({ nativeEvent: { layout: { height } } }) => { | ||
setHiddenTextHeight(height); | ||
}, [setHiddenTextHeight]); | ||
const onHiddenTextLayout = useCallback( | ||
({ | ||
nativeEvent: { | ||
layout: {height}, | ||
}, | ||
}) => { | ||
setHiddenTextHeight(height); | ||
}, | ||
[setHiddenTextHeight], | ||
); | ||
const toggle = useCallback(() => { | ||
setCollapsed(prev => !prev); | ||
if (animate) { | ||
LayoutAnimation.configureNext(LayoutAnimation.create( | ||
300, | ||
LayoutAnimation.Types.linear, | ||
LayoutAnimation.Properties.opacity | ||
)); | ||
} | ||
}, [setCollapsed, animate]); | ||
const toggle = useCallback(() => { | ||
setCollapsed((prev) => !prev); | ||
if (animate) { | ||
LayoutAnimation.configureNext( | ||
LayoutAnimation.create( | ||
300, | ||
LayoutAnimation.Types.linear, | ||
LayoutAnimation.Properties.opacity, | ||
), | ||
); | ||
} | ||
}, [setCollapsed, animate]); | ||
useEffect(() => { | ||
if (!hiddenTextHeight || !textHeight) return; | ||
useEffect(() => { | ||
if (!hiddenTextHeight || !textHeight) { | ||
return; | ||
} | ||
setSeeMore(hiddenTextHeight > textHeight); | ||
}, [textHeight, hiddenTextHeight]) | ||
setSeeMore(hiddenTextHeight > textHeight); | ||
}, [textHeight, hiddenTextHeight]); | ||
useEffect(() => { | ||
setAfterCollapsed(collapsed) | ||
}, [collapsed]) | ||
useEffect(() => { | ||
setAfterCollapsed(collapsed); | ||
}, [collapsed]); | ||
const textProps = collapsed ? { | ||
onLayout: onTextLayout, | ||
numberOfLines, | ||
ellipsizeMode: 'tail' | ||
} : {}; | ||
const textProps = collapsed | ||
? { | ||
onLayout: onTextLayout, | ||
numberOfLines, | ||
ellipsizeMode: 'tail', | ||
} | ||
: {}; | ||
return ( | ||
<View style={wrapperStyle}> | ||
<TextComponent | ||
style={StyleSheet.flatten([ | ||
Array.isArray(style) ? StyleSheet.flatten(style) : style, | ||
styles.hiddenText, | ||
])} | ||
numberOfLines={numberOfLines + 1} | ||
ellipsizeMode={'clip'} | ||
onLayout={onHiddenTextLayout} | ||
> | ||
{children || ''} | ||
</TextComponent> | ||
<TextComponent | ||
{...restProps} | ||
style={style} | ||
{...textProps} | ||
> | ||
{children || ''} | ||
</TextComponent> | ||
{seeMore && collapsed && afterCollapsed && ( | ||
<View style={styles.seeMoreContainer}> | ||
<TouchableOpacity | ||
onPress={toggle} | ||
style={[styles.seeMoreButton, { backgroundColor }]} | ||
> | ||
<TextComponent {...restProps} style={style}> | ||
{'... '} | ||
</TextComponent> | ||
<Text style={seeMoreStyle}> | ||
{seeMoreText} | ||
</Text> | ||
return ( | ||
<View style={wrapperStyle}> | ||
<TextComponent | ||
style={StyleSheet.flatten([ | ||
Array.isArray(style) ? StyleSheet.flatten(style) : style, | ||
styles.hiddenText, | ||
])} | ||
numberOfLines={numberOfLines + 1} | ||
ellipsizeMode={'clip'} | ||
onLayout={onHiddenTextLayout}> | ||
{children || ''} | ||
</TextComponent> | ||
<TextComponent {...restProps} style={style} {...textProps}> | ||
{children || ''} | ||
</TextComponent> | ||
{seeMore && collapsed && afterCollapsed && ( | ||
<View style={styles.seeMoreContainer}> | ||
<TouchableOpacity | ||
onPress={toggle} | ||
style={[styles.seeMoreButton, {backgroundColor}]}> | ||
<TextComponent {...restProps} style={style}> | ||
{'... '} | ||
</TextComponent> | ||
<Text style={seeMoreStyle}>{seeMoreText}</Text> | ||
</TouchableOpacity> | ||
</View> | ||
)} | ||
{seeMore && !collapsed && ( | ||
<TouchableOpacity onPress={toggle} style={styles.seeLessContainer}> | ||
<Text style={seeLessStyle}>{seeLessText}</Text> | ||
</TouchableOpacity> | ||
</View> | ||
)} | ||
{seeMore && !collapsed && ( | ||
<TouchableOpacity onPress={toggle} style={styles.seeLessContainer}> | ||
<Text style={seeLessStyle}> | ||
{seeLessText} | ||
</Text> | ||
</TouchableOpacity> | ||
)} | ||
</View> | ||
); | ||
}); | ||
)} | ||
</View> | ||
); | ||
}, | ||
); | ||
@@ -156,18 +175,6 @@ const styles = StyleSheet.create({ | ||
ReadMore.propTypes = { | ||
style: PropTypes.oneOfType([ | ||
PropTypes.object, | ||
PropTypes.array, | ||
]), | ||
seeMoreStyle: PropTypes.oneOfType([ | ||
PropTypes.object, | ||
PropTypes.array, | ||
]), | ||
seeLessStyle: PropTypes.oneOfType([ | ||
PropTypes.object, | ||
PropTypes.array, | ||
]), | ||
wrapperStyle: PropTypes.oneOfType([ | ||
PropTypes.object, | ||
PropTypes.array, | ||
]), | ||
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]), | ||
seeMoreStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]), | ||
seeLessStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]), | ||
wrapperStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]), | ||
children: PropTypes.any, | ||
@@ -190,10 +197,4 @@ numberOfLines: PropTypes.number, | ||
style: styles.defaultText, | ||
seeMoreStyle: StyleSheet.flatten([ | ||
styles.defaultText, | ||
styles.seeMoreText, | ||
]), | ||
seeLessStyle: StyleSheet.flatten([ | ||
styles.defaultText, | ||
styles.seeLessText, | ||
]), | ||
seeMoreStyle: StyleSheet.flatten([styles.defaultText, styles.seeMoreText]), | ||
seeLessStyle: StyleSheet.flatten([styles.defaultText, styles.seeLessText]), | ||
wrapperStyle: styles.container, | ||
@@ -200,0 +201,0 @@ text: '', |
{ | ||
"name": "@fawazahmed/react-native-read-more", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "A simple react native library to show large blocks of text in a condensed manner with the ability to collapse and expand.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
313
542524
66