Giter Club home page Giter Club logo

aurelia-autocomplete's Introduction

aurelia-autocomplete

aurelia-autocomplete is a plugin for the Aurelia platform for a autocomplete control.

Shamelessly ripped, improved, and packaged from a @jdanyow gist https://gist.github.com/jdanyow/acf8253329939b2e046cd0e3394351fe. Inspired by a want to escape a jQuery implementation we had been using in drivesoftware/aurelia-widgets.

Currently implemented for bootstrap but this could be abstracted in future.

Installation

npm install drivesoftware/aurelia-autocomplete

aurelia.json:

{
   "name": "aurelia-autocomplete",
   "main": "index",
   "path": "../node_modules/aurelia-autocomplete/dist/amd",
   "resources": [
      "*.js",
      "*.html",
      "autocomplete.css"
   ]
}

Basic Usage

Activate the plugin in your application's aurelia configure callback:

export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .plugin('aurelia-autocomplete');

  aurelia.start().then(a => a.setRoot());
}

In your view

<autocomplete value.bind="selectedValue" controller.bind="clientAutoCompleteController"></autocomplete>

In your view model

import {AutoCompleteController} from 'aurelia-autocomplete';

//...

this.selectedValue = null;
this.clientAutoCompleteController = new AutoCompleteController((searchText) => this.clientApi.search(searchText));

The default controller implementation provided expects a single constructor argument which is a search function suggestion[] search(string searchText). The result of the search function should be an array of suggestions based on the search text. A second optional constructor argument allows provision of a callback of the form string formatSuggestion(suggestion) if no value is provided, toString() is used on the suggestion objects.

Advanced Usage

AutoCompleteController

Provide a format suggestion callback

string formatSuggestion(suggestion)

Used to convert a suggestion to a string of text describing that suggestion. Default implementation just calls suggestion.toString()

Example Given suggestion results

{
  "code": "A-SUGGESTION",
  "description": "A Suggestion Result"
}

you could format suggestions by creating a controller using

new AutoCompleteController(someSearchCallback, (suggestion) => `${suggestion.code} ${suggestion.description}`);
}

// example suggestion when selected or listed would be formatted as 'A-SUGGESTION A Suggestion Result'

suggestion createSuggestion(suggestion)

Used to create suggestion objects for the autocomplete control. Default implementation adds a selectedText property (using formatSuggestion) to the suggestion which is used by the default suggestion result template (which is a replacable part described below).

The default suggestion template used by the autocomplete control uses a compose element to render a configurable/dynamic view

<!-- the replacable suggestion part -->
<template replaceable part="suggestion">
  <compose view.bind="suggestionView" view-model.bind="suggestion"></compose>
</template>

<!-- the default view template passed to suggestionView -->
<template>${selectedText}</template

where the selectedText property will be created by createSuggestion

Example

Use the code and description properties of the above suggestion results.

Create suggestions that include code and description properties:

createSuggestion(suggestion) {
  return {
    id: suggestion.id,
    code: suggestion.code,
    description: suggestion.description
  };
}

Format the autocomplete results with code in bold by replacing the suggestion part:

<autocomplete value.bind="selectedValue" controller.bind="clientAutoCompleteController">
  <template replace-part="suggestion"><strong>${suggestion.code}</strong> ${suggestion.description}</template>
</autocomplete>

If you have a common format across your application that you would like to use for suggestions in all places where the autocomplete used (and the default selectedText does not satisfy your needs) you can configure the template used by the compose in the fallback slot. This means you will not need the extra part replacement line in your markup <template replaceable part="suggestion"><strong>${suggestion.code}</strong> ${suggestion.description}</template> for every autocomplete control. This is done via plugin configuration:

export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .plugin('aurelia-autocomplete', config => {
      config.settings.suggestionTemplate = '<template><strong>${code}</strong> ${description}</template>'
    });

  aurelia.start().then(a => a.setRoot());
}

NOTE: the properties used by the template are not suggestion. prefixed as they are when replacing the slot content inline.

Webpack Installation

If you are using Webpack and Aurelia, there are a few additional steps you must complete before you can make usage of this plugin.

  1. Run npm install drivesoftware/aurelia-autocomplete
  2. Insert aurelia.use.plugin(PLATFORM.moduleName('aurelia-autocomplete')); into your main bootstrapping file (such as main.ts)
  3. Add the following code to the webpack.config.js file. Insert it between new AureliaPlugin(), and new ProvidePlugin({ 'Promise': 'bluebird' }),:
new ModuleDependenciesPlugin({
  'aurelia-autocomplete': [
    './autocomplete.js',
    './autocomplete.html',
    './autocompleteconfiguration.js',
    './autocompletecontroller.js',
    './autocompleteoptions.js',
  ],
}),

Now you should be able to successfully use the plugin like normal.

aurelia-autocomplete's People

Contributors

graywolf336 avatar gunsakimbo avatar maren123 avatar simonfox avatar tinokoschinski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

aurelia-autocomplete's Issues

Error using autocomplete with aurelia-cli

Hi,

I'm having issues with this module with an aurelia-cli based project.

I get this error in the browser console when I load the application:

Uncaught TypeError: Cannot read property '__useDefault' of undefined
    at ensureOriginOnExports (vendor-bundle.js:13786)
    at vendor-bundle.js:13894
    at Object.execCb (vendor-bundle.js:5484)
    at Module.check (vendor-bundle.js:4671)
    at Module.enable (vendor-bundle.js:4964)
    at Module.init (vendor-bundle.js:4576)
    at vendor-bundle.js:5248

No errors are displayed during compilation and bundling. The error also appears even if i comment out the autocomplete-tags i app.html, so I believe this error is related to installation/reference to the autocomplete module.

Can you see what's wrong? Do you have a live demo somewhere? Or a download link with a working project?

This is my configuration:

package.json:

"dependencies": {
    "aurelia-autocomplete": "git+https://github.com/drivesoftware/aurelia-autocomplete.git"
}

aurelia.json:

{
  "name": "aurelia-autocomplete",
  "main": "index",
  "path": "../node_modules/aurelia-autocomplete/dist/amd",
  "resources": [
  "autocomplete.css"
  ]
}

main.ts:

export function configure(aurelia: Aurelia) {
  aurelia.use
    .standardConfiguration()
    .feature('resources')
    .plugin('aurelia-autocomplete');

  aurelia.start().then(() => aurelia.setRoot());
}

app.ts:

import { autoinject } from 'aurelia-dependency-injection';
import { ClientAPI } from './client-api';
import { AutoCompleteController } from 'aurelia-autocomplete';

@autoinject()
export class App {
  client: any;
  constructor(private clientAutoCompleteController: AutoCompleteController, private clientApi: ClientAPI) {
    clientAutoCompleteController = new AutoCompleteController((searchText) => this.clientApi.suggestNames(searchText));
  }
}

app.html:

<template>
  <require from="bootstrap/css/bootstrap.css"></require>
  
  <autocomplete value.bind="client" controller.bind="clientAutoCompleteController">
    <template replace-part="suggestion"><strong>${suggestion.nameID}</strong> ${suggestion.name}</template>
  </autocomplete>
</template>

Running tests on the Autocomplete component

Hi, I want to extend some functionality into the component, but I'm having a difficult time instantiating the Autocomplete-class to start unit testing. Using nulls in the constructor fails. Are you able to add a working test of the Autocomplete-class?

Edit: what I want to add is a new bindable parameter that enables/disables the automatic select whenever a single suggestion is returned. autocomplete.js:95

Input field gets cleared/freeze/blocked while typing on IE11

Whenever I use your autocomplete on Chrome, Edge it works fine. If I use it in IE11, when I start typing I get blocked and cannot continue typing. When the response is received from the server with the suggestions whatever was in the input filed gets cleared and the suggestions do not appears. I can start typing again once the field is cleared.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.