Skip to content

Commit

Permalink
Display multiple OpenGraphDisplay for each url in input text (#19)
Browse files Browse the repository at this point in the history
* Support multiple urls in input text

* OpenGraphAwareInput resultLimit prop; update README
  • Loading branch information
apparition47 authored and adrienthiery committed Mar 2, 2018
1 parent bddefda commit abc6c2d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
32 changes: 19 additions & 13 deletions OpenGraphAwareInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ export default class OpenGraphAwareInput extends Component {
onIconPress: PropTypes.func,
showIcon: PropTypes.bool,
textInputStyle: TextInput.propTypes.style,
resultLimit: PropTypes.number,
};

static defaultProps = {
debounceDelay: 300,
showIcon: false,
resultLimit: 1,
};

constructor(props) {
super(props);

this.state = {
openGraphData: {},
openGraphData: [],
};
}

handleDismissOpengraph = () => {
this.setState({
openGraphData: {},
openGraphData: [],
});
}

Expand All @@ -57,11 +59,11 @@ export default class OpenGraphAwareInput extends Component {
(data) => {
const customEvent = {};

this.setState({ openGraphData: data || {} });
this.setState({ openGraphData: data || [] });

if (this.props.onChange) {
customEvent.event = event;
customEvent.opengraphData = data || {};
customEvent.opengraphData = data || [];
customEvent.text = text;

this.props.onChange(customEvent);
Expand All @@ -79,6 +81,7 @@ export default class OpenGraphAwareInput extends Component {
};

render() {
const ogDataToDisplay = this.state.openGraphData.slice(0, this.props.resultLimit);
return (
<View
style={[
Expand All @@ -93,15 +96,18 @@ export default class OpenGraphAwareInput extends Component {
this.props.textInputStyle,
]}
/>
<OpenGraphDisplay
data={this.state.openGraphData}
onIconPress={this.props.showIcon
? this.props.onIconPress || this.handleDismissOpengraph
: null
}
iconSource={this.props.iconSource}
iconStyle={this.props.iconStyle}
/>
{ogDataToDisplay.map((meta, i) =>
<OpenGraphDisplay
key={i}
data={meta}
onIconPress={this.props.showIcon
? this.props.onIconPress || this.handleDismissOpengraph
: null
}
iconSource={this.props.iconSource}
iconStyle={this.props.iconStyle}
/>
)}
</View>
);
}
Expand Down
11 changes: 6 additions & 5 deletions OpenGraphParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,21 @@ async function extractMeta(textContent = '', options = { fallbackOnHTMLTags: tru
try {
const urls = getUrls(textContent);

let metaData = null;
let metaData = [];
let i = 0;

while (!metaData && i < urls.length) {
while (i < urls.length) {
if (urls[i].indexOf('youtube.com') >= 0) {
metaData = await fetchJSON(`https://www.youtube.com/oembed?url=${urls[i]}&format=json`, urls[i])
metaData.push(await fetchJSON(`https://www.youtube.com/oembed?url=${urls[i]}&format=json`, urls[i]));
} else {
metaData = await fetchHtml(urls[i])
metaData.push(await fetchHtml(urls[i])
.then(
(html) => ({
...html ? parseMeta(html, urls[i], options) : {},
url: urls[i],
})
);
)
);
}

i++;
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ onIconPress | React.PropTypes.func | A function to call when the Icon is pressed
iconSource | Image.propTypes.source | The Image Source to use as Icon (see `OpenGraphDisplay`)
iconStyle | Image.propTypes.style | The style of the Icon (see `OpenGraphDisplay`)
showIcon | React.PropTypes.bool | Should we show the Icon or not? (default is `false`)
resultLimit | React.PropTypes.number | Max number of parsed OpenGraph links to display (default is `1`)

## OpenGraphDisplay

Expand Down
10 changes: 6 additions & 4 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { OpenGraphAwareInput, OpenGraphDisplay, OpenGraphParser } from 'react-na

export default class Example extends Component {
state = {
data: {},
data: [],
};

handleIconPress = () => {
Expand Down Expand Up @@ -48,9 +48,11 @@ export default class Example extends Component {
onChange={this.handleTextChange}
/>
<Text style={styles.title}>OpenGraphDisplay</Text>
<OpenGraphDisplay
data={this.state.data}
/>
{this.state.data.map((meta, i) =>
<OpenGraphDisplay
data={meta}
/>
)}
</View>
);
}
Expand Down

0 comments on commit abc6c2d

Please sign in to comment.