Skip to content

Commit

Permalink
feat(js): autocomplete with results page
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss committed Oct 9, 2018
1 parent d127509 commit b41b66f
Show file tree
Hide file tree
Showing 14 changed files with 5,867 additions and 0 deletions.
9 changes: 9 additions & 0 deletions InstantSearch.js/autocomplete-results-page/.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 InstantSearch.js/autocomplete-results-page/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
/dist
/.cache
3 changes: 3 additions & 0 deletions InstantSearch.js/autocomplete-results-page/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: 'algolia',
};
22 changes: 22 additions & 0 deletions InstantSearch.js/autocomplete-results-page/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/dist
/.cache

# 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 InstantSearch.js/autocomplete-results-page/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"proseWrap": "never",
"trailingComma": "es5"
}
21 changes: 21 additions & 0 deletions InstantSearch.js/autocomplete-results-page/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# autocomplete-results-page

_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
```

Open http://localhost:3000 to see your app.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions InstantSearch.js/autocomplete-results-page/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!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="./manifest.webmanifest">
<link rel="shortcut icon" href="./favicon.png">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.js@2.8.0/dist/instantsearch.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.js@2.8.0/dist/instantsearch-theme-algolia.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/selectize@0.12.6/dist/css/selectize.css">
<link rel="stylesheet" href="./src/index.css">
<link rel="stylesheet" href="./src/app.css">

<title>autocomplete-results-page</title>
</head>

<body>
<div class="container">
<h1>InstantSearch.js - Results page with an autocomplete</h1>
<div id="autocomplete"></div>
<div id="hits"></div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/selectize@0.12.6/dist/js/standalone/selectize.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/algoliasearch@3.30.0/dist/algoliasearchLite.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@2.10.2"></script>
<script src="./src/app.js"></script>
</body>

</html>
15 changes: 15 additions & 0 deletions InstantSearch.js/autocomplete-results-page/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "autocomplete-results-page",
"name": "autocomplete-results-page 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"
}
21 changes: 21 additions & 0 deletions InstantSearch.js/autocomplete-results-page/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "autocomplete-results-page",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "parcel index.html --port 3000",
"build": "parcel build index.html",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix"
},
"devDependencies": {
"babel-eslint": "8.2.6",
"eslint": "5.5.0",
"eslint-config-algolia": "13.2.3",
"eslint-config-prettier": "3.0.1",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-prettier": "2.6.2",
"parcel-bundler": "1.9.7",
"prettier": "1.14.2"
}
}
32 changes: 32 additions & 0 deletions InstantSearch.js/autocomplete-results-page/src/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
h1 {
margin-bottom: 1rem;
}

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

.container {
max-width: 960px;
margin: 0 auto;
padding: 1em;
}

.ais-hits {
margin-top: 1em;
}

.ais-Hits-item {
margin-bottom: 2em;
}

.hit-name {
margin-bottom: 0.5em;
}

.hit-description {
color: #888;
font-size: 14px;
margin-bottom: 0.5em;
}
92 changes: 92 additions & 0 deletions InstantSearch.js/autocomplete-results-page/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* global $ instantsearch algoliasearch */

const searchClient = algoliasearch(
'B1G2GM9NG0',
'aadef574be1f9252bb48d4ea09b5cfe5'
);

const autocomplete = instantsearch.connectors.connectAutocomplete(
({ indices, refine, widgetParams }, isFirstRendering) => {
const { container, onSelectChange } = widgetParams;

if (isFirstRendering) {
container.html('<select id="ais-autocomplete"></select>');

container.find('select').selectize({
options: [],
valueField: 'name',
labelField: 'name',
searchField: 'name',
highlight: false,
onType: refine,
onChange(value) {
onSelectChange(value);
refine(value);
},
});

return;
}

const [select] = container.find('select');

indices.forEach(index => {
select.selectize.clearOptions();
index.results.hits.forEach(h => select.selectize.addOption(h));
select.selectize.refreshOptions(select.selectize.isOpen);
});
}
);

const suggestions = instantsearch({
indexName: 'demo_ecommerce',
searchClient,
});

suggestions.addWidget(
instantsearch.widgets.configure({
hitsPerPage: 5,
})
);

suggestions.addWidget(
autocomplete({
container: $('#autocomplete'),
onSelectChange(value) {
// eslint-disable-next-line
search.helper.setQuery(value).search();
},
})
);

const search = instantsearch({
indexName: 'demo_ecommerce',
searchClient,
});

search.addWidget(
instantsearch.widgets.configure({
hitsPerPage: 10,
})
);

search.addWidget(
instantsearch.widgets.hits({
container: '#hits',
templates: {
item: `
<div class="ais-Hits-item">
<header class="hit-name">
{{{_highlightResult.name.value}}}
</header>
<p class="hit-description">
{{{_highlightResult.description.value}}}
</p>
</div>
`,
},
})
);

suggestions.start();
search.start();
10 changes: 10 additions & 0 deletions InstantSearch.js/autocomplete-results-page/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';
}
Loading

0 comments on commit b41b66f

Please sign in to comment.