-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
191 lines (177 loc) · 5.01 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React, { Component } from "react";
import {
StyleSheet,
Text,
TouchableHighlight,
TouchableOpacity,
View
} from "react-native";
import Autocomplete from "react-native-autocomplete-input";
export default class AutoTags extends Component {
state = {
query: ""
};
renderTags = () => {
if (this.props.renderTags) {
return this.props.renderTags(this.props.tagsSelected);
}
const tagMargins = this.props.tagsOrientedBelow
? { marginBottom: 5 }
: { marginTop: 5 };
return (
<View style={this.props.tagStyles || styles.tags}>
{this.props.tagsSelected.map((t, i) => {
return (
<TouchableHighlight
key={i}
style={[tagMargins, styles.tag]}
onPress={() => this.props.handleDelete(i)}
>
<Text>{t.name}</Text>
</TouchableHighlight>
);
})}
</View>
);
};
handleInput = text => {
if (this.submitting) return;
if (this.props.allowBackspace) {
//TODO: on ios, delete last tag on backspace event && empty query
//(impossible on android atm, no listeners for empty backspace)
}
if (this.props.onChangeText) return this.props.onChangeText(text);
if (
this.props.createTagOnSpace &&
this.props.onCustomTagCreated &&
text.length > 1 &&
text.charAt(text.length - 1) === " "
) {
this.setState({ query: "" });
return this.props.onCustomTagCreated(text.trim());
} else if (this.props.createTagOnSpace && !this.props.onCustomTagCreated) {
console.error(
"When enabling createTagOnSpace, you must provide an onCustomTagCreated function"
);
}
if (text.charAt(text.length - 1) === "\n") {
return; // prevent onSubmit bugs
}
this.setState({ query: text });
};
filterData = query => {
if (!query || query.trim() == "" || !this.props.suggestions) {
return;
}
if (this.props.filterData) {
return this.props.filterData(query);
}
let suggestions = this.props.suggestions;
let results = [];
query = query.toUpperCase();
suggestions.forEach(i => {
if (i.name.toUpperCase().includes(query)) {
results.push(i);
}
});
return results;
};
onSubmitEditing = () => {
const { query } = this.state;
if (!this.props.onCustomTagCreated || query.trim() === "") return;
this.setState({ query: "" }, () => this.props.onCustomTagCreated(query));
// prevents an issue where handleInput() will overwrite
// the query clear in some circumstances
this.submitting = true;
setTimeout(() => {
this.submitting = false;
}, 30);
};
addTag = tag => {
this.props.handleAddition(tag);
this.setState({ query: "" });
};
render() {
const { query } = this.state;
const data = this.filterData(query);
return (
<View style={styles.AutoTags}>
{!this.props.tagsOrientedBelow &&
this.props.tagsSelected &&
this.renderTags()}
<Autocomplete
data={data}
controlled={true}
placeholder={this.props.placeholder}
defaultValue={query}
value={query}
onChangeText={text => this.handleInput(text)}
onSubmitEditing={this.onSubmitEditing}
multiline={true}
autoFocus={this.props.autoFocus === false ? false : true}
renderItem={({ item, i }) => (
<TouchableOpacity onPress={e => this.addTag(item)}>
{this.props.renderSuggestion ? (
this.props.renderSuggestion(item)
) : (
<Text>{item.name}</Text>
)}
</TouchableOpacity>
)}
inputContainerStyle={
this.props.inputContainerStyle || styles.inputContainerStyle
}
containerStyle={this.props.containerStyle || styles.containerStyle}
underlineColorAndroid="transparent"
style={{ backgroundColor: "#efeaea" }}
listContainerStyle={{
backgroundColor: this.props.tagsOrientedBelow
? "#efeaea"
: "transparent"
}}
{...this.props}
/>
{this.props.tagsOrientedBelow &&
this.props.tagsSelected &&
this.renderTags()}
</View>
);
}
}
const styles = StyleSheet.create({
AutoTags: {
flexDirection: "row",
flexWrap: "wrap",
alignItems: "flex-start"
},
tags: {
flexDirection: "row",
flexWrap: "wrap",
alignItems: "flex-start",
backgroundColor: "#efeaea",
width: 300
},
tag: {
backgroundColor: "rgb(244, 244, 244)",
justifyContent: "center",
alignItems: "center",
height: 30,
marginLeft: 5,
borderRadius: 30,
padding: 8
},
inputContainerStyle: {
borderRadius: 0,
paddingLeft: 5,
height: 40,
width: 300,
justifyContent: "center",
borderColor: "transparent",
alignItems: "stretch",
backgroundColor: "#efeaea"
},
containerStyle: {
minWidth: 200,
maxWidth: 300
}
});