-
Notifications
You must be signed in to change notification settings - Fork 4
/
AlgoliaExample.js
50 lines (46 loc) · 1.33 KB
/
AlgoliaExample.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
import React, { Component } from 'react';
import algoliasearch from 'algoliasearch';
import AlgoliaInput from '../src';
const algoliaClient = algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76');
class AlgoliaExample extends Component {
constructor(...args) {
super(...args);
this.state = {
hits: []
};
}
onError = () => {
console.log('onError', arguments); // eslint-disable-line
}
onResults = (content) => {
this.setState({
hits: content.hits
});
}
onEmptyField = () => {
this.setState({
hits: []
});
}
renderResult(hit) {
return (<li key={ hit.objectID }>
<img className="thumb" src={ hit.image } style={ {float: 'left' } }/>
<b>{ hit.name}</b>
<br/>
<p style={ {fontSize: 10 } }>{ hit.description }</p>
</li>);
}
render() {
return (
<div className="example">
<h1>algolia-react-input</h1>
<AlgoliaInput placeholder="Product search..." client={ algoliaClient } options={ { hitsPerPage: 50 } } index='instant_search' onResults={ this.onResults } onEmptyField={ this.onEmptyField } onError={ this.onError }/>
<hr/>
<div className="results">
{ this.state.hits.map(this.renderResult) }
</div>
</div>
);
}
}
export default AlgoliaExample;