forked from byteburgers/react-native-autocomplete-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (162 loc) · 4.21 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
import React, { Component, PropTypes } from 'react';
import {
ListView,
StyleSheet,
Text,
TextInput,
View
} from 'react-native';
class AutoComplete extends Component {
static propTypes = {
...TextInput.propTypes,
/**
* These styles will be applied to the container which
* surrounds the autocomplete component.
*/
containerStyle: View.propTypes.style,
/**
* Assign an array of data objects which should be
* rendered in respect to the entered text.
*/
data: PropTypes.array,
/*
* These styles will be applied to the container which surrounds
* the textInput component.
*/
inputContainerStyle: View.propTypes.style,
/**
* These style will be applied to the result list view.
*/
listStyle: ListView.propTypes.style,
/**
* `renderItem` will be called to render the data objects
* which will be displayed in the result view below the
* text input.
*/
renderItem: PropTypes.func,
/**
* `onShowResults` will be called when list is going to
* show/hide results.
*/
onShowResults: PropTypes.func,
/**
* renders custom TextInput. All props passed to this function.
*/
renderTextInput: PropTypes.func
};
static defaultProps = {
data: [],
defaultValue: '',
renderItem: rowData => <Text>{rowData}</Text>
};
constructor(props) {
super(props);
const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
this.state = {
dataSource: ds.cloneWithRows(props.data),
showResults: props.data && props.data.length > 0
};
}
componentWillReceiveProps(nextProps) {
if (!nextProps.defaultValue || nextProps.defaultValue === this.props.defaultValue) {
this._showResults(false);
return
}
const dataSource = this.state.dataSource.cloneWithRows(nextProps.data);
if (dataSource[0] && dataSource[0] === nextProps.defaultValue) {
this._showResults(false);
return
}
this._showResults(dataSource.getRowCount() > 0);
this.setState({ dataSource });
}
/**
* Proxy `blur()` to autocomplete's text input.
*/
blur() {
const { textInput } = this;
textInput && textInput.blur();
}
/**
* Proxy `focus()` to autocomplete's text input.
*/
focus() {
const { textInput } = this;
textInput && textInput.focus();
}
_renderItems() {
const { listStyle, renderItem } = this.props;
const { dataSource } = this.state;
return (
<ListView
dataSource={dataSource}
keyboardShouldPersistTaps={true}
renderRow={renderItem}
style={[styles.list, listStyle]}
/>
);
}
_showResults(show) {
const { showResults } = this.state;
const { onShowResults } = this.props;
if (!showResults && show) {
this.setState({ showResults: true });
onShowResults && onShowResults(true);
} else if (showResults && !show) {
this.setState({ showResults: false });
onShowResults && onShowResults(false);
}
}
_renderTextInput() {
const { onEndEditing, renderTextInput, style } = this.props;
const props = {
style: [styles.input, style],
ref: ref => (this.textInput = ref),
onEndEditing: e =>
this._showResults(false) || (onEndEditing && onEndEditing(e)),
...this.props
};
return renderTextInput
? renderTextInput(props)
: (<TextInput {...props} />);
}
render() {
const { showResults } = this.state;
const { containerStyle, inputContainerStyle } = this.props;
return (
<View style={[styles.container, containerStyle]}>
<View style={[styles.inputContainer, inputContainerStyle]}>
{this._renderTextInput()}
</View>
{showResults && this._renderItems()}
</View>
);
}
}
const border = {
borderColor: '#b9b9b9',
borderRadius: 1,
borderWidth: 1
};
const styles = StyleSheet.create({
container: {
zIndex: 1
},
inputContainer: {
...border
},
input: {
backgroundColor: 'white',
height: 40,
paddingLeft: 3
},
list: {
...border,
backgroundColor: 'white',
borderTopWidth: 0,
left: 0,
position: 'absolute',
right: 0
}
});
export default AutoComplete;