Skip to content

Commit

Permalink
feat(react): add mutli-index-autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss committed Jul 16, 2018
1 parent 0a656aa commit 324ceb3
Show file tree
Hide file tree
Showing 15 changed files with 7,750 additions and 0 deletions.
9 changes: 9 additions & 0 deletions React InstantSearch/multi-index-autocomplete/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
3 changes: 3 additions & 0 deletions React InstantSearch/multi-index-autocomplete/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: 'algolia/react',
};
21 changes: 21 additions & 0 deletions React InstantSearch/multi-index-autocomplete/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
5 changes: 5 additions & 0 deletions React InstantSearch/multi-index-autocomplete/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"proseWrap": "never",
"trailingComma": "es5"
}
19 changes: 19 additions & 0 deletions React InstantSearch/multi-index-autocomplete/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# multi-index-autocomplete

_This project was generated with [create-instantsearch-app](https://github.com/algolia/create-instantsearch-app) by [Algolia](https://algolia.com)._

## Get started

To run this project locally, install the dependencies and run the local server:

```sh
npm install
npm start
```

Alternatively, you may use [Yarn](https://http://yarnpkg.com/):

```sh
yarn
yarn start
```
28 changes: 28 additions & 0 deletions React InstantSearch/multi-index-autocomplete/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "multi-index-autocomplete",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix"
},
"dependencies": {
"react": "16.4.1",
"react-autosuggest": "9.3.4",
"react-dom": "16.4.1",
"react-instantsearch-dom": "5.2.2",
"react-scripts": "1.1.4"
},
"devDependencies": {
"eslint": "4.19.1",
"eslint-config-algolia": "13.1.0",
"eslint-config-prettier": "2.9.0",
"eslint-plugin-import": "2.13.0",
"eslint-plugin-prettier": "2.6.1",
"eslint-plugin-react": "7.10.0",
"prettier": "1.13.5",
"prop-types": "15.6.2"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions React InstantSearch/multi-index-autocomplete/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">

<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.png">

<!--
Do not use @7 in production, use a complete version like x.x.x, see website for latest version:
https://community.algolia.com/react-instantsearch/Getting_started.html#load-the-algolia-theme
-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.css@7/themes/algolia-min.css">

<title>multi-index-autocomplete</title>
</head>

<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>

<div id="root"></div>
</body>

</html>
15 changes: 15 additions & 0 deletions React InstantSearch/multi-index-autocomplete/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "multi-index-autocomplete",
"name": "multi-index-autocomplete Sample",
"icons": [
{
"src": "favicon.png",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
78 changes: 78 additions & 0 deletions React InstantSearch/multi-index-autocomplete/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
h1 {
margin-bottom: 1rem;
}

em {
background: cyan;
font-style: normal;
}

.ais-InstantSearch__root {
max-width: 1200px;
margin: 0 auto;
padding: 1rem;
}

.react-autosuggest__container {
position: relative;
}

.react-autosuggest__input {
width: 100%;
box-sizing: border-box;
padding: 20px 20px;
font-family: Helvetica, sans-serif;
font-weight: 300;
font-size: 16px;
border: 1px solid #aaa;
border-radius: 4px;
}

.react-autosuggest__input--focused {
outline: none;
}

.react-autosuggest__input--open {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}

.react-autosuggest__suggestions-container {
display: none;
}

.react-autosuggest__suggestions-container--open {
display: block;
position: absolute;
box-sizing: border-box;
top: 51px;
width: 100%;
border: 1px solid #aaa;
background-color: #fff;
font-family: Helvetica, sans-serif;
font-weight: 300;
font-size: 16px;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
z-index: 2;
}

.react-autosuggest__section-title {
padding: 10px 20px;
font-weight: 600;
}

.react-autosuggest__suggestions-list {
margin: 0;
padding: 0;
list-style-type: none;
}

.react-autosuggest__suggestion {
cursor: pointer;
padding: 10px 20px;
}

.react-autosuggest__suggestion--highlighted {
background-color: #ddd;
}
31 changes: 31 additions & 0 deletions React InstantSearch/multi-index-autocomplete/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { Component } from 'react';
import { InstantSearch, Index, Configure } from 'react-instantsearch-dom';
import AutoComplete from './AutoComplete';
import './App.css';

class App extends Component {
render() {
return (
<InstantSearch
appId="latency"
apiKey="6be0576ff61c053d5f9a3225e2a90f76"
indexName="instant_search"
>
<h1>React InstantSearch - Autocomplete with multiple indices</h1>

<Configure hitsPerPage={3} />
<AutoComplete
onSuggestionSelected={(event, { suggestion, suggestionValue }) => {
console.log('Suggestion:', suggestion);
console.log('Suggestion value:', suggestionValue);
}}
/>

<Index indexName="instant_search" />
<Index indexName="bestbuy" />
</InstantSearch>
);
}
}

export default App;
75 changes: 75 additions & 0 deletions React InstantSearch/multi-index-autocomplete/src/AutoComplete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Highlight, connectAutoComplete } from 'react-instantsearch-dom';
import AutoSuggest from 'react-autosuggest';

class AutoComplete extends Component {
static propTypes = {
hits: PropTypes.arrayOf(PropTypes.object).isRequired,
currentRefinement: PropTypes.string.isRequired,
refine: PropTypes.func.isRequired,
onSuggestionSelected: PropTypes.func.isRequired,
};

state = {
value: this.props.currentRefinement,
};

onChange = (event, { newValue }) => {
this.setState({
value: newValue,
});
};

onSuggestionsFetchRequested = ({ value }) => {
this.props.refine(value);
};

onSuggestionsClearRequested = () => {
this.props.refine();
};

getSuggestionValue(hit) {
return hit.name;
}

renderSuggestion(hit) {
return <Highlight attribute="name" hit={hit} tagName="mark" />;
}

renderSectionTitle(section) {
return section.index;
}

getSectionSuggestions(section) {
return section.hits;
}

render() {
const { hits, onSuggestionSelected } = this.props;
const { value } = this.state;

const inputProps = {
placeholder: 'Search for a product...',
onChange: this.onChange,
value,
};

return (
<AutoSuggest
suggestions={hits}
multiSection={true}
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
onSuggestionSelected={onSuggestionSelected}
getSuggestionValue={this.getSuggestionValue}
renderSuggestion={this.renderSuggestion}
inputProps={inputProps}
renderSectionTitle={this.renderSectionTitle}
getSectionSuggestions={this.getSectionSuggestions}
/>
);
}
}

export default connectAutoComplete(AutoComplete);
10 changes: 10 additions & 0 deletions React InstantSearch/multi-index-autocomplete/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
body,
h1 {
margin: 0;
padding: 0;
}

body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,
Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
}
6 changes: 6 additions & 0 deletions React InstantSearch/multi-index-autocomplete/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));
Loading

0 comments on commit 324ceb3

Please sign in to comment.