Coder Social home page Coder Social logo

solenya-autocomplete's Introduction

Solenya Autocomplete

A databound autocomplete input for solenya.

Installation

Assuming you have solenya installed, you will need the following npm packages:

solenya-autocomplete
bootstrap
popper.js
jquery

Currently solenya-autocomplete superficially depends on bootstrap & jquery for styling & menus. Those dependencies will be removed in the future.

Usage

The usage of AutoComplete is as follows:

export class AutoCompleteSample extends Component
{
    usStates: string[] = []

    @transient usStateAutocomplete = 
        new AutoComplete ({
            target: this,
            prop: () => this.usStates,
            isMultiSelect: true,
            modelToLabel: mapPropertyFromTo (usStates, c => c.code, c => c.label),
            labelToModel: mapPropertyFromTo (usStates, c => c.label, c => c.code),
            suggestor: async searchText => {
                const reg = new RegExp (searchText, "i")
                return usStates.filter (l => reg.test (l.label)).map(l => l.label)
            }
        })

    view() {
        return this.usStateAutocomplete.view ()                  
    }
}

const usStates = [
    { code: "AL", label: "Alabama" },
    { code: "AK", label: "Alaska" },
    { code: "AZ", label: "Arizona" },
    ...
}

Like all the built-in inputs, AutoComplete databinds to a property. The property type is of type T|T[], where you use an array when isMultiSelect is true.

modelToLabel and labelToModel allow you to translate between logical values and the values the user sees. In the above example, the logical values are the state codes, while the label values are the full names of the states.

The suggestor is a promise that returns a list of suggestions given a searchText.

Styling

You can customize the styling to the various composite elements of the autocomplete as follows:

 this.usStateAutocomplete.view ({         
    selectionAttrs: {
        style: {
            backgroundColor: "#eee"                        
        }
    }
})            

Properties

interface IAutoCompleteProps<T>
{
    /** Can be used to prefix the input id used by the autocomplete, which is based on the property name the autocomplete is bound to. */
    prefix: string 

    /** Whether the user can select multiple items. Defaults to false. */
    isMultiSelect : boolean

    /** The maximum number of suggestions to display. Defaults to 10. */
    suggestionLimit: number

    /** Whether the databound property is updated in response to text changes. True by default. If set to false, you will need to call the autocomplete's autocomplete() method to manually trigger the autocomplete. */
    isRealtime: boolean

    /** Whether to automatically filter the results based on suggestions returned. Defaults to true, meaning that by default the suggestor doesn't need to filter the list it returns. */
    autoFilter: boolean

    /** Whether to display the autocomplete's `noResultsFoundText` courtesy message when no results are found. */
    showNoResultsFound: boolean

    /** Whether to automatically select a suggestion when there's only a single possible suggestion that matches the current input text.  */
    autoSelectSingleResult: boolean

    /** Whether the user is allowed to enter values that are not suggestions. */
    allowNonSuggestions: boolean

    /** Whether to call the suggestor with an empty value to trigger the suggestor loading and caching its values. */
    preload: boolean

    /** A promise that returns an array of suggestions given `searchText`. If `autoFilter` is `true` then filtering will be automatically applied, albeit potentially less efficiently or correctly than if you filter yourself.` */
    suggestor?: (searchText: string) => Promise<string[]>

    /** An optional function tha converts a label to your databound value. */
    labelToModel?: (label: string) => T

    /** An optional function that conversts your databound value to a label. */
    modelToLabel?: (model: T) => string

    /** A function that receives an event when the user's selection changes. */
    onSelectEvent?: (selection: string | string[]) => void    
} 

solenya-autocomplete's People

Contributors

solenya-group avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

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.