-
Notifications
You must be signed in to change notification settings - Fork 332
docs(examples): add Vue 3 example #576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { h } from 'vue'; | ||
|
||
// Parcel picks the `source` field of the monorepo packages and thus doesn't | ||
// apply the Babel config. We therefore need to manually override the constants | ||
// in the app, as well as the React pragmas. | ||
// See https://twitter.com/devongovett/status/1134231234605830144 | ||
(global as any).__DEV__ = process.env.NODE_ENV !== 'production'; | ||
(global as any).__TEST__ = false; | ||
(global as any).h = h; | ||
(global as any).createElement = h; | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
|
||
<link rel="shortcut icon" href="favicon.png" type="image/x-icon" /> | ||
<link rel="stylesheet" href="style.css" /> | ||
|
||
<title>Vue | Autocomplete</title> | ||
</head> | ||
|
||
<body> | ||
<div id="app"></div> | ||
|
||
<script src="env.ts"></script> | ||
<script src="src/main.js"></script> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "@algolia/autocomplete-example-vue", | ||
"description": "Autocomplete with Vue", | ||
"version": "1.0.0", | ||
"private": true, | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "parcel build index.html", | ||
"start": "parcel index.html" | ||
}, | ||
"dependencies": { | ||
"@algolia/autocomplete-js": "1.0.0", | ||
"@algolia/autocomplete-theme-classic": "1.0.0", | ||
"algoliasearch": "4.9.1", | ||
"vue": "3.0.11" | ||
}, | ||
"devDependencies": { | ||
"@algolia/client-search": "4.9.1", | ||
"@parcel/transformer-vue": "2.0.0-beta.2", | ||
"parcel": "2.0.0-beta.2" | ||
}, | ||
"keywords": [ | ||
"algolia", | ||
"autocomplete", | ||
"javascript" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<template> | ||
<div class="container"> | ||
<div id="autocomplete" /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { h, Fragment, render, onMounted } from 'vue'; | ||
import { autocomplete, getAlgoliaResults } from '@algolia/autocomplete-js'; | ||
import algoliasearch from 'algoliasearch/lite'; | ||
|
||
import '@algolia/autocomplete-theme-classic'; | ||
|
||
import { createElement } from './adapter'; | ||
import ProductItem from './ProductItem.vue'; | ||
|
||
const appId = 'latency'; | ||
const apiKey = '6be0576ff61c053d5f9a3225e2a90f76'; | ||
const searchClient = algoliasearch(appId, apiKey); | ||
|
||
export default { | ||
name: 'App', | ||
setup() { | ||
onMounted(() => { | ||
autocomplete({ | ||
container: '#autocomplete', | ||
placeholder: 'Search', | ||
getSources({ query }) { | ||
return [ | ||
{ | ||
sourceId: 'products', | ||
getItems() { | ||
return getAlgoliaResults({ | ||
searchClient, | ||
queries: [ | ||
{ | ||
indexName: 'instant_search', | ||
query, | ||
}, | ||
], | ||
}); | ||
}, | ||
templates: { | ||
item({ item, components }) { | ||
return ( | ||
<ProductItem item={item} highlight={components.Highlight} /> | ||
); | ||
}, | ||
}, | ||
}, | ||
]; | ||
}, | ||
renderer: { | ||
createElement, | ||
Fragment, | ||
}, | ||
render({ children }, root) { | ||
render(children, root); | ||
}, | ||
}); | ||
}); | ||
}, | ||
}; | ||
</script> | ||
|
||
<style> | ||
.container { | ||
margin: 0 auto; | ||
max-width: 640px; | ||
width: 100%; | ||
} | ||
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<template> | ||
<div class="aa-ItemWrapper"> | ||
<div class="aa-ItemContent"> | ||
<div class="aa-ItemIcon aa-ItemIcon--picture aa-ItemIcon--alignTop"> | ||
<img :src="item.image" :alt="item.name" width="40" height="40" /> | ||
</div> | ||
<div className="aa-ItemContentBody"> | ||
<div class="aa-ItemContentTitle"> | ||
<component :is="highlight" :hit="item" attribute="name" /> | ||
</div> | ||
<div class="aa-ItemContentDescription"> | ||
By <strong>{{ item.brand }}</strong> in | ||
<strong>{{ item.categories[0] }}</strong> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="aa-ItemActions"> | ||
<button | ||
class="aa-ItemActionButton aa-TouchOnly aa-ActiveOnly" | ||
type="button" | ||
title="Select" | ||
> | ||
<svg fill="currentColor" viewBox="0 0 24 24" width="20" height="20"> | ||
<path | ||
d="M18.984 6.984h2.016v6h-15.188l3.609 3.609-1.406 1.406-6-6 6-6 1.406 1.406-3.609 3.609h13.172v-4.031z" | ||
/> | ||
</svg> | ||
</button> | ||
<button | ||
class="aa-ItemActionButton" | ||
type="button" | ||
title="Add to cart" | ||
@click="onAddToCart" | ||
> | ||
<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"> | ||
<path | ||
d="M19 5h-14l1.5-2h11zM21.794 5.392l-2.994-3.992c-0.196-0.261-0.494-0.399-0.8-0.4h-12c-0.326 0-0.616 0.156-0.8 0.4l-2.994 3.992c-0.043 0.056-0.081 0.117-0.111 0.182-0.065 0.137-0.096 0.283-0.095 0.426v14c0 0.828 0.337 1.58 0.879 2.121s1.293 0.879 2.121 0.879h14c0.828 0 1.58-0.337 2.121-0.879s0.879-1.293 0.879-2.121v-14c0-0.219-0.071-0.422-0.189-0.585-0.004-0.005-0.007-0.010-0.011-0.015zM4 7h16v13c0 0.276-0.111 0.525-0.293 0.707s-0.431 0.293-0.707 0.293h-14c-0.276 0-0.525-0.111-0.707-0.293s-0.293-0.431-0.293-0.707zM15 10c0 0.829-0.335 1.577-0.879 2.121s-1.292 0.879-2.121 0.879-1.577-0.335-2.121-0.879-0.879-1.292-0.879-2.121c0-0.552-0.448-1-1-1s-1 0.448-1 1c0 1.38 0.561 2.632 1.464 3.536s2.156 1.464 3.536 1.464 2.632-0.561 3.536-1.464 1.464-2.156 1.464-3.536c0-0.552-0.448-1-1-1s-1 0.448-1 1z" | ||
/> | ||
</svg> | ||
</button> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
export default { | ||
props: { | ||
item: Object, | ||
highlight: Function, | ||
}, | ||
setup() { | ||
function onAddToCart(event) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
console.log('Add to cart'); | ||
} | ||
|
||
return { | ||
onAddToCart, | ||
}; | ||
}, | ||
}; | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { h } from 'vue'; | ||
|
||
export function createElement(type, props, ...children) { | ||
const adaptedProps = Object.entries(props || {}).reduce( | ||
(acc, [key, value]) => { | ||
// Vue 3 accepts lower-case event names so we need to transform props like | ||
// `onMouseMove` to `onMousemove`. | ||
const property = | ||
key[0] === 'o' && key[1] === 'n' | ||
? key.slice(0, 3) + key.slice(3).toLowerCase() | ||
: key; | ||
|
||
acc[property] = value; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
|
||
return h(type, adaptedProps, ...children); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createApp } from 'vue'; | ||
|
||
import App from './App.vue'; | ||
|
||
createApp(App).mount('#app'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
body { | ||
background-color: rgb(244, 244, 249); | ||
color: rgb(65, 65, 65); | ||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', | ||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', | ||
sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
padding: 1rem; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.