View Source

Typeahead

Intelligently recommend entities via customizable, fuzzy recognition.

Typeahead is an enhancement upon Input which provides two * built-in matching algorithms ("fuzzy" [default] and "starts-with") and supports the use of custom matching and marking functions.

In the examples below, imagine the <> in the "marks" section is a wrapping <mark> element:

  1. "Starts-with" matching & marking

    <Typeahead
        algorithm={Typeahead.mode.STARTS_WITH}
        entities={[
            {text: 'apple'},
            {text: 'apricot'},
            {text: 'grape'},
        ]}
        inputProps={{value: 'a'}} />
    • matches: "apple", "apricot"
    • marks: "<a>pple", "<a>pricot"
  2. "Fuzzy" matching & marking

    <Typeahead
        algorithm={Typeahead.mode.FUZZY}
        entities={[
            {text: 'apple'},
            {text: 'apricot'},
            {text: 'grape'},
        ]}
        inputProps={{value: 'a'}} />
    • matches: "apple", "apricot", "grape"
    • marks: "<a>pple", "<a>pricot", "gr<a>pe"
  3. Custom matching & marking

    Optionally, you can provide your own combination of matching and marking functions. For example, loosening the matching to include * unicode variants of characters could be useful, e.g. ç c

    <Typeahead
        algorithm={{
            matcher: yourMatchFunc,
            marker: yourMarkFunc,
        }} />

Installation#

npm i --save boundless-typeahead

Typeahead can also just be directly used from the main Boundless library. This is recommended when you're getting started to avoid maintaining the package versions of several components:

npm i boundless --save

the ES6 import statement then becomes like:

import { Typeahead } from 'boundless';

Demo

Starts-with matching
Please enter your country of origin...
Fuzzy matching
Please enter your country of origin...
Show Implementation

Component Instance Methods#

When using Typeahead in your project, you may call the following methods on a rendered instance of the component. Use refs to get the instance.

  • focus() focuses the browser oon the underlying textual input for immediate text entry

  • getInputNode() returns the raw underlying textual input DOM node

  • getSelectedEntityText() returns the text property of the currently highlighted entity (from props.entities), or returns an empty string

  • getValue() retrieves the current value of the underlying textual input

  • select() programmatically creates a full selection on the underlying textual input such that a press of the Backspace key would fully clear the * input

  • setValue(value: string) sets the underlying textual input to the specified text and updates internal state; do not use this method when using Typeahead as a * "controlled input"

Props#

Required Props#

There are no required props.

Optional Props#

NameImplementationDescription
*
Expects
any

any React-supported attribute

algorithm
Expects
Typeahead.mode.STARTS_WITH or
Typeahead.mode.FUZZY or object
Default Value
Typeahead.mode.FUZZY

the mechanism used to identify and mark matching substrings; a custom set can be provided as an object (see the properties below)

clearOnSelection
Expects
bool
Default Value
false

if true, clears the input text when a (partial) match is selected

component
Expects
string
Default Value
'div'

overrides the HTML container tag

entities
Expects
arrayOf(object)
Default Value
[]

an array of objects that user input is filtered against; at a minimum, each object must have a text property and any other supplied property is passed through to the resulting DOM element

text
Expects
string

the text to be used to do string comparison and match against

hidePlaceholderOnFocus
Expects
bool
Default Value
true

triggers the placeholder to disappear when the input field is focused, reappears when the user has tabbed away or focus is moved

hint
Expects
bool
Default Value
null

renders a disabled textfield with the full text of the currently selected input hint; will remain blank if the matched substring is not at the beginning of the user input

hintProps
Expects
object
Default Value
{}
*
Expects
any

any React-supported attribute

inputProps
Expects
object
Default Value
{
    type: 'text',
}
*
Expects
any

any React-supported attribute

defaultValue
Expects
string
onBlur
Expects
function
onFocus
Expects
function
onChange
Expects
function
placeholder
Expects
string
type
Expects
string
value
Expects
string
matchWrapperProps
Expects
object
Default Value
{}
*
Expects
any

any React-supported attribute

offscreenClass
Expects
string
Default Value
'b-offscreen'

the "offscreen" class used by your application; specifically to retain ARIA navigability as display: none excludes the element from consideration

onComplete
Expects
function
Default Value
() => {}

called when the user presses Enter with no autosuggest hint available, indicating that input is complete

onEntityHighlighted
Expects
function
Default Value
() => {}

called with the index of the highlighted entity due to keyboard selection

onEntitySelected
Expects
function
Default Value
() => {}

called with the index of the entity selected by the user