import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { StyleSheet, Text, View, TextInput, FlatList, Image, Keyboard, TouchableHighlight, } from 'react-native'; import algoliasearch from 'algoliasearch/lite'; import { InstantSearch, Configure, Index, connectSearchBox, connectInfiniteHits, connectHits, connectRefinementList, } from 'react-instantsearch-native'; import Highlight from './Highlight'; import Icon from 'react-native-vector-icons/FontAwesome'; const searchClient = algoliasearch( 'latency', '6be0576ff61c053d5f9a3225e2a90f76' ); const styles = StyleSheet.create({ container: { marginTop: 30, flex: 1, }, suggestionsContainer: { flex: 1, }, algoliaLogo: { width: 40, height: 40, margin: 10, }, searchBoxContainer: { flexDirection: 'row', alignItems: 'stretch', }, bestResults: { backgroundColor: 'lightgrey', height: 40, justifyContent: 'center', padding: 10, }, searchBox: { color: 'black', height: 50, width: 300, alignSelf: 'center', }, hitsContainer: { flexDirection: 'row', margin: 10, }, suggestions: { flexDirection: 'row', alignItems: 'center', padding: 10, }, suggestionsIcon: { marginRight: 10, }, hitsPicture: { width: 40, height: 40 }, hitsText: { alignSelf: 'center', paddingLeft: 5, flex: 1, flexWrap: 'wrap', }, hitsSeparator: { height: 1, backgroundColor: 'lightgrey', marginTop: 10, marginBottom: 10, }, categoryTextContainer: { flexDirection: 'row', alignItems: 'center', }, categoryTextIn: { fontStyle: 'italic' }, categoryText: { color: '#cc8008' }, }); export default class App extends React.Component { constructor(props) { super(props); this.state = { displaySuggestions: false, isFirstKeystroke: true, searchState: {}, query: '', category: null, }; this.displaySuggestions = this.displaySuggestions.bind(this); this.removeSuggestions = this.removeSuggestions.bind(this); this.setQuery = this.setQuery.bind(this); this.onSearchStateChange = this.onSearchStateChange.bind(this); this.firstKeystroke = this.firstKeystroke.bind(this); this.clearFilter = this.clearFilter.bind(this); } firstKeystroke() { this.setState({ isFirstKeystroke: false }); } displaySuggestions() { this.setState({ displaySuggestions: true }); } removeSuggestions() { this.setState({ displaySuggestions: false, isFirstKeystroke: true }); } setQuery(query, category) { const { query: _query, page: _page, ...searchState } = this.state.searchState; if (searchState.indices && searchState.indices.instant_search) { searchState.indices.instant_search.page = 0; } this.setState({ query, searchState, category, displaySuggestions: false, }); } clearFilter() { this.setState({ category: null, query: '', }); } onSearchStateChange(searchState) { this.setState({ searchState }); } render() { const suggestions = this.state.displaySuggestions ? ( ) : null; const results = this.state.displaySuggestions ? ( ) : ( ); return ( {suggestions} Best results {this.state.category ? ` in ${this.state.category}` : null} {results} ); } } class SearchBox extends Component { render() { return ( { if (text === '') { this.props.clearFilter(); } this.props.refine(text); }} value={this.props.currentRefinement} placeholder={'Search a product...'} placeholderTextColor={'black'} clearButtonMode={'always'} underlineColorAndroid={'white'} spellCheck={false} autoCorrect={false} autoCapitalize={'none'} onFocus={this.props.displaySuggestions} onChange={() => { if (this.props.isFirstKeystroke) { this.props.displaySuggestions(); this.props.firstKeystroke(); } }} /> ); } } const ConnectedSearchBox = connectSearchBox(SearchBox); SearchBox.propTypes = { currentRefinement: PropTypes.string, displaySuggestions: PropTypes.func, firstKeystroke: PropTypes.func, refine: PropTypes.func, isFirstKeystroke: PropTypes.bool, clearFilter: PropTypes.func, }; const HitsList = ({ hits, removeSuggestions, onEndReached }) => ( ( )} data={hits} keyExtractor={(item, index) => item.objectID + index} onEndReached={onEndReached} onScroll={() => { Keyboard.dismiss(); removeSuggestions(); }} ItemSeparatorComponent={() => } /> ); HitsList.propTypes = { hits: PropTypes.array, removeSuggestions: PropTypes.func, onEndReached: PropTypes.func, }; const ResultsInfiniteHits = connectInfiniteHits( ({ hits, hasMore, refine, removeSuggestions }) => ( { if (hasMore) { refine(); } }} /> ) ); const ResultsHits = connectHits(({ hits, removeSuggestions }) => ( )); const SuggestionsHits = connectHits(({ hits, onPressItem }) => ( { const category = index === 1 ? item.instant_search.facets.exact_matches.categories[0].value : null; return ( ); }} keyExtractor={(item, index) => item.objectID + index} data={hits.reduce((acc, hit, index) => { if (index === 0) { acc.push(hit); // we duplicate first hit to allow a refinement under or not category } acc.push(hit); return acc; }, [])} keyboardShouldPersistTaps="always" /> )); const buildItemCategoryText = (categoryText) => ( in {categoryText} ); const Item = ({ item, category, onPressItem, index }) => { let text = null; if (index === 0) { text = buildItemCategoryText('All our categories'); } if (category) { text = buildItemCategoryText(category); } return ( { Keyboard.dismiss(); onPressItem(item.query, category); }} underlayColor="white" > {text} ); }; Item.propTypes = { item: PropTypes.object, index: PropTypes.number, category: PropTypes.string, onPressItem: PropTypes.func, }; const VirtualRefinementList = connectRefinementList(() => null);