Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ or with yarn
```
yarn add react-native-read-more-text
```
## Props

| Prop | Type | Required | Note |
|---|---|---|---|
| `onReady` | `function` | no | callback function to know when the component is ready
| `children` | `string` | yes | String to render on read more component
| `renderTruncatedFooter` | `function` | no | function that will replace the `Read more` label
| `renderRevealedFooter` | `function` | no | function that will replace the `Hide` label
## Props

| Prop | Type | Required | Note |
| ----------------------- | ---------- | -------- | --------------------------------------------------------------------------------------- |
| `onReady` | `function` | no | callback function to know when the component is ready |
| `children` | `string` | yes | String to render on read more component |
| `renderTruncatedFooter` | `function` | no | function that will replace the `Read more` label |
| `renderRevealedFooter` | `function` | no | function that will replace the `Hide` label |
| `textClickable` | `bool` | no | if true, tapping anywhere on the text will trigger the expension of text and vice versa |

[Try it on Expo](https://exp.host/@notbrent/read-more-text-example)

Expand All @@ -28,10 +29,9 @@ yarn add react-native-read-more-text
### Usage

```javascript

import * as React from 'react';
import { View, Text } from 'react-native';
import ReadMore from 'react-native-read-more-text';
import * as React from "react";
import { View, Text } from "react-native";
import ReadMore from "react-native-read-more-text";

export class DescriptionCard extends React.Component {
render() {
Expand All @@ -40,9 +40,7 @@ export class DescriptionCard extends React.Component {
return (
<View>
<View style={styles.cardLabel}>
<Text style={styles.cardLabelText}>
Description
</Text>
<Text style={styles.cardLabelText}>Description</Text>
</View>

<View style={styles.card}>
Expand All @@ -51,10 +49,9 @@ export class DescriptionCard extends React.Component {
numberOfLines={3}
renderTruncatedFooter={this._renderTruncatedFooter}
renderRevealedFooter={this._renderRevealedFooter}
onReady={this._handleTextReady}>
<Text style={styles.cardText}>
{text}
</Text>
onReady={this._handleTextReady}
>
<Text style={styles.cardText}>{text}</Text>
</ReadMore>
</View>
</View>
Expand All @@ -64,22 +61,28 @@ export class DescriptionCard extends React.Component {

_renderTruncatedFooter = (handlePress) => {
return (
<Text style={{color: Colors.tintColor, marginTop: 5}} onPress={handlePress}>
<Text
style={{ color: Colors.tintColor, marginTop: 5 }}
onPress={handlePress}
>
Read more
</Text>
);
}
};

_renderRevealedFooter = (handlePress) => {
return (
<Text style={{color: Colors.tintColor, marginTop: 5}} onPress={handlePress}>
<Text
style={{ color: Colors.tintColor, marginTop: 5 }}
onPress={handlePress}
>
Show less
</Text>
);
}
};

_handleTextReady = () => {
// ...
}
};
}
```
51 changes: 32 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import { StyleSheet, Text, View, TouchableWithoutFeedback } from "react-native";

export default class ReadMore extends React.Component {
static defaultProps = {
textClickable: false,
};

state = {
measured: false,
shouldShowReadMore: false,
showAllText: false
showAllText: false,
};

async componentDidMount() {
Expand Down Expand Up @@ -47,22 +51,31 @@ export default class ReadMore extends React.Component {
let { numberOfLines } = this.props;

return (
<View>
<Text
numberOfLines={measured && !showAllText ? numberOfLines : 0}
style={this.props.textStyle}
ref={text => {
this._text = text;
}}
>
{this.props.children}
</Text>

{this._maybeRenderReadMore()}
</View>
<TouchableWithoutFeedback
disabled={!this.props.textClickable}
onPress={this._handleSwitch}
>
<View>
<Text
numberOfLines={measured && !showAllText ? numberOfLines : 0}
style={this.props.textStyle}
ref={(text) => {
this._text = text;
}}
>
{this.props.children}
</Text>

{this._maybeRenderReadMore()}
</View>
</TouchableWithoutFeedback>
);
}

_handleSwitch = () => {
this.setState({ showAllText: !this.state.showAllText });
};

_handlePressReadMore = () => {
this.setState({ showAllText: true });
};
Expand Down Expand Up @@ -99,20 +112,20 @@ export default class ReadMore extends React.Component {
}

function measureHeightAsync(component) {
return new Promise(resolve => {
return new Promise((resolve) => {
component.measure((x, y, w, h) => {
resolve(h);
});
});
}

function nextFrameAsync() {
return new Promise(resolve => requestAnimationFrame(() => resolve()));
return new Promise((resolve) => requestAnimationFrame(() => resolve()));
}

const styles = StyleSheet.create({
button: {
color: "#888",
marginTop: 5
}
marginTop: 5,
},
});