-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlacesInput.js
More file actions
61 lines (56 loc) · 1.87 KB
/
PlacesInput.js
File metadata and controls
61 lines (56 loc) · 1.87 KB
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
import React, { PropTypes, Component } from 'react';
import placesAutocompleteDataset from 'places.js/autocompleteDataset.js';
import algoliasearch from 'algoliasearch';
import autocomplete from 'autocomplete.js';
export default class PlacesInput extends Component {
static propTypes = {
client : PropTypes.object.isRequired,
index : PropTypes.string.isRequired,
onSelect : PropTypes.func.isRequired,
displayKey : PropTypes.string.isRequired,
customHeader : PropTypes.string.isRequired,
placesHeader : PropTypes.string.isRequired,
customHitsPerPage : PropTypes.number.isRequired,
placesHitsPerPage : PropTypes.number.isRequired,
customClass : PropTypes.string,
countries : PropTypes.array
}
constructor (props) {
super(props);
this.index = this.props.client.initIndex(this.props.index);
this.customDataset = {
source : autocomplete.sources.hits(this.index, { hitsPerPage : this.props.customHitsPerPage }),
displayKey : this.props.displayKey,
templates : {
header : this.props.customHeader
}
}
this.placesDataset = placesAutocompleteDataset({
algoliasearch : algoliasearch,
templates : {
header : this.props.placesHeader
},
countries: this.props.countries,
hitsPerPage : this.props.placesHitsPerPage
});
}
componentDidMount () {
autocomplete(document.querySelector('#alg-places-input'), {
cssClasses : { prefix : 'alg-places' }
}, [
this.placesDataset,
this.customDataset
]).on('autocomplete:selected', (event, suggestion, dataset) => {
if (suggestion) {
this.props.onSelect(event, suggestion, dataset);
}
});
}
render () {
return (
<div>
<input type='search' id='alg-places-input' className={this.props.customClass} placeholder={this.props.inputPlaceholder}/>
</div>
)
}
}