-
Notifications
You must be signed in to change notification settings - Fork 22
/
OpenGraphAwareInput.js
103 lines (87 loc) · 2.72 KB
/
OpenGraphAwareInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// @flow
import React, { Component } from 'react';
import { StyleSheet, TextInput, View } from 'react-native';
import debounce from 'lodash.debounce';
import OpenGraphDisplay from './OpenGraphDisplay';
import OpenGraphParser from './OpenGraphParser';
const styles = StyleSheet.create({
container: {},
input: {
height: 50,
marginBottom: 5,
},
});
type Props = {
debounceDelay?: Number,
resultLimit?: Number,
showIcon?: boolean,
containerStyle: StyleSheet,
textInputStyle: StyleSheet,
onIconPress: Function,
iconSource: Number | String,
iconStyle: StyleSheet,
};
type State = {
openGraphData: Array<*>,
};
export default class OpenGraphAwareInput extends Component<Props, State> {
static defaultProps = {
debounceDelay: 300,
resultLimit: 1,
showIcon: false,
};
state = {
openGraphData: [],
};
extractMetaAndSetState = debounce((text, event) => {
OpenGraphParser.extractMeta(text).then((data) => {
const customEvent = {};
this.setState({ openGraphData: data || [] });
if (this.props.onChange) {
customEvent.event = event;
customEvent.opengraphData = data || [];
customEvent.text = text;
this.props.onChange(customEvent);
}
});
}, this.props.debounceDelay);
handleDismissOpengraph = () => {
this.setState({
openGraphData: [],
});
};
handleTextInputChange = (event) => {
const text = event.nativeEvent.text;
this.extractMetaAndSetState(text, event);
};
render() {
const ogDataToDisplay = this.state.openGraphData.slice(
0,
this.props.resultLimit
);
const { containerStyle, iconStyle, iconSource, onIconPress, showIcon, textInputStyle, ...rest } = this.props;
return (
<View style={[styles.container, containerStyle]}>
<TextInput
onChange={this.handleTextInputChange}
style={[styles.input, textInputStyle]}
{...rest}
/>
{ogDataToDisplay.map((meta, i) => (
<OpenGraphDisplay
key={i}
data={meta}
onIconPress={
showIcon
? onIconPress
|| this.handleDismissOpengraph
: null
}
iconSource={iconSource}
iconStyle={iconStyle}
/>
))}
</View>
);
}
}