2.12.1Typeahead is an input field with dropdown-like listbox that displays suggested results that most closely match a user's given search term.
npm install @bolt/components-typeahead
<form> fallback (via Twig)withEvents base class to allow for much deeper JavaScript customization<slot> support| Name | Type | Description | 
|---|---|---|
items | 
  array | 
  An array of objects that populates the dropdown | 
| Name | Params | Description | 
|---|---|---|
onInput | 
  event, value | 
  Called every time the input value changes | 
getSuggestions | 
  value | 
  Called by onSuggestionsFetchRequested when re-rendering suggestions. Handles highlighting keywords in the search results in a React-friendly way + handles limiting the total number of results displayed | 
onChange | 
  event, newValue, method | 
  Called when a suggestion is selected. Includes info on how the particular item was selected (click, keyboard, etc) | 
onSuggestionsFetchRequested | 
  value | 
  Called every very time you need to gather / update suggestions to display. See onSuggestionsFetchRequested for more info. | 
onSuggestionsClearRequested | 
  Called when clearing suggestions. See onSuggestionsClearRequested for more info. | |
onSelected | 
  event, suggestion | 
  Will be called every time suggestion is selected via mouse or keyboard. See onSuggestionSelected for more info. | 
onRenderInput | 
  value | 
  Fired when the input is being rendered | 
Note: when assigning component props as HTML attributes on a web component, make sure to use kebab-case.
| Prop Name | Description | Type | Default Value | Option(s) | 
|---|---|---|---|---|
| 
      
                         | 
    
           A Drupal-style attributes object with extra attributes to append to this component.  | 
    
            object
      
      
     | — |       
  | 
  
| 
      
                         | 
    
           The maximum number of typeahead results to display  | 
    
            number
      
      
     | 
              10
           | 
    
          
  | 
  
| 
      
                         | 
    
           An array of objects that's used to populate the suggestion list that appears below the input as the users type. This array of objects can be asynchronously fetched and should contain a   | 
    
            array
      
      
     | — |       
  | 
  
| 
      
                         | 
    
           Screenreader-specific text for the clear search button, intended to provide a longer, more descriptive explanation of the clear button's behavior.  | 
    
            string
      
      
     | 
              Clear search results
           | 
    
          
  | 
  
| 
      
                         | 
    
           Screenreader-specific text for the submit button, intended to provide a longer, more descriptive explanation of the submit button's behavior.  | 
    
            string
      
      
     | 
              Submit search query
           | 
    
          
  | 
  
| 
      
                         | 
    
           Screenreader-specific label text associated with the search input.  | 
    
            string
      
      
     | — |       
  | 
  
| 
      
                         | 
    
           The placeholder text to display inside the Typeahead search input.  | 
    
            string
      
      
     | 
              Enter your search query
           | 
    
          
  | 
  
| 
      
                         | 
    
           Initial value to pre-populate the input field  | 
    
            string
      
      
     | — |       
  | 
  
| 
      
                         | 
    
           Input element's name attribute used when pre-rendering the component  | 
    
            string
      
      
     | — |       
  | 
  
| 
      
                         | 
    
           Disable text highlighting in matching search results (highlighting is enabled by default)  | 
    
            boolean
      
      
     | 
              false
           | 
    
          
  | 
  
In this example, we populate the Typeahead component with JSON data that's dynamically fetched from an external source via the getSuggestions hook.
Also, this demo caps the max # of search results to display at 5.
{% include "@bolt-components-form/form.twig" with {
  children: include("@bolt-components-typeahead/typeahead.twig", {
    attributes: {
      class: [
        "js-typeahead-hook--dynamically-fetch-data"
      ]
    },
    max_results: 5,
    input_name: "q"
  }),
  attributes: {
    action: "https://www.pega.com/search",
    target: "_blank",
    method: "GET"
  }
} %}
// NOTE: make sure you're running this code through a tool like Babel before shipping for cross browser compatibility!
const dynamicTypeaheadDemo = document.querySelector(
  '.js-typeahead-hook--dynamically-fetch-data',
);
if (dynamicTypeaheadDemo) {
  dynamicTypeaheadDemo.addEventListener('ready', function(e) {
    if (e.detail.name === 'bolt-typeahead') {
      // note: make sure to let Typeahead know when the data fetched is ready
      dynamicTypeaheadDemo.on('getSuggestions', async value => {
        return await new Promise(async resolve => {
          await fetch('/build/data/typeahead.data.json')
            .then(function(response) {
              return response.json();
            })
            .then(function(data) {
              return resolve(data);
            });
        });
      });
      dynamicTypeaheadDemo.on('onSelected', (element, event, suggestion) => {
        const exactMatch = element.items.filter(
          item => item.label === suggestion.suggestionValue,
        )[0];
        function navigateTo(url) {
          if (window.location !== window.parent.location) {
            const win = window.open(url, '_blank');
            win.focus();
          } else {
            window.location = url;
          }
        }
        if (exactMatch && exactMatch.url) {
          if (exactMatch.url) {
            navigateTo(exactMatch.url);
          } else {
            navigateTo(`https://www.pega.com/search?q=${itemSelected.label}`);
          }
        } else if (suggestion.suggestionValue !== '') {
          navigateTo(
            `https://www.pega.com/search?q=${suggestion.suggestionValue}`,
          );
        }
      });
    }
  });
}