Overview

Smart.ComboBox represents an input with a dropdown list of items.

Files

  • CSS files
    • smart.default.css - the CSS file containing the styles for the element.
  • Javascript module
    • smart.combobox.js - the JS module which is in the source/modules/ folder and loads all script dependencies.
  • Javascript files
    • smart.element.js - the base class
    • smart.button.js - the JS file containing the element definition
    • smart.scrollbar.js - the JS file containing the definition for the Scroll bars of the listBox popup.
    • smart.listbox.js - the JS file containing the definition for the listBox in the drop down list popup.
    • smart.dropdownlist.js - the JS file containing the definition for drop down list popup.
    • smart.combobox.js - the JS file containing the definition for element.

Usage

  • Import a module

    With this approach, we import a module and create the web component by using the Smart function. The #combobox is a smart-combo-box tag.

    import {smartComboBox} from "../../../source/modules/smart.combobox.js";
    
    Smart('#combobox', class {
    get properties() {
    	return {
    		dataSource: [
                "Affogato",
                "Americano",
                "Bicerin",
                "Breve",
                "Café Bombón",
                "Café au lait",
                "Caffé Corretto",
                "Café Crema",
                "Caffé Latte",
                "Caffé macchiato",
                "Café mélange",
                "Coffee milk",
                "Cafe mocha",
                "Cappuccino",
                "Carajillo",
                "Cortado",
                "Cuban espresso",
                "Espresso",
                "Eiskaffee",
                "The Flat White",
                "Frappuccino",
                "Galao",
                "Greek frappé coffee",
                "Indian filter coffee",
                "Instant coffee",
                "Irish coffee",
                "Liqueur coffee"
    		]
    	}
    }
    });
    
    document.readyState === 'complete' ? init() : window.onload = init;
    
    function init() { 
    
    }
    
    Using the Smart function is optional. You can use const combobox = document.querySelector("#combobox"); and set the properties like that:
    const combobox = document.querySelector("#combobox");
    combobox.dataSource = [
                "Affogato",
                "Americano",
                "Bicerin",
                "Breve",
                "Café Bombón",
                "Café au lait",
                "Caffé Corretto",
                "Café Crema",
                "Caffé Latte",
                "Caffé macchiato",
                "Café mélange",
                "Coffee milk",
                "Cafe mocha",
                "Cappuccino",
                "Carajillo",
                "Cortado",
                "Cuban espresso",
                "Espresso",
                "Eiskaffee",
                "The Flat White",
                "Frappuccino",
                "Galao",
                "Greek frappé coffee",
                "Indian filter coffee",
                "Instant coffee",
                "Irish coffee",
                "Liqueur coffee"
    		];
    

  • Import a module, Init on Demand

    The following imports the web component's module and creates it on demand, when the document is ready. The #combobox is a DIV tag.

    import {smartComboBox} from "../../../source/modules/smart.combobox.js";
    
    document.readyState === 'complete' ? init() : window.onload = init;
    
    function init() { 
    const combobox = new smartComboBox('#combobox', {
      
    });
    }
    
  • Load scripts

    The following code adds the custom element to the page.

    <!DOCTYPE html>
    <html>
    <head>
     <link rel="stylesheet" href="./../../source/styles/smart.default.css" type="text/css" />
     <script type="text/javascript" src="../../source/smart.element.js"></script>
     <script type="text/javascript" src="../../source/smart.button.js"></script>
     <script type="text/javascript" src="../../source/smart.scrollbar.js"></script>
     <script type="text/javascript" src="../../source/smart.listbox.js"></script>
     <script type="text/javascript" src="../../source/smart.dropdownlist.js"></script>
     <script type="text/javascript" src="../../source/smart.combobox.js"></script>
    </head>
    <body>
    	 <smart-combo-box>
    		 <smart-list-item selected>Affogato</smart-list-item>
    		 <smart-list-item>Affogato</smart-list-item>
    		 <smart-list-item>Americano</smart-list-item>
    		 <smart-list-item>Bicerin</smart-list-item>
    		 <smart-list-item>Breve</smart-list-item>
    		 <smart-list-item>Café Bombón</smart-list-item>
    		 <smart-list-item>Café au lait</smart-list-item>
    		 <smart-list-item>Caffé Latte"</smart-list-item>
    		 <smart-list-item>Caffé macchiato</smart-list-item>
    		 <smart-list-item>Café mélange</smart-list-item>
    		 <smart-list-item>Coffee milk</smart-list-item>
    		 <smart-list-item>Cafe mocha</smart-list-item>
    		 <smart-list-item>Cappuccino</smart-list-item>
    		 <smart-list-item>Carajillo</smart-list-item>
    		 <smart-list-item>Cortado</smart-list-item>
    		 <smart-list-item>Cuban espresso</smart-list-item>
    		 <smart-list-item>Espresso</smart-list-item>
    		 <smart-list-item>Eiskaffee</smart-list-item>
    		 <smart-list-item>The Flat White</smart-list-item>
    		 <smart-list-item>Frappuccino</smart-list-item>
    		 <smart-list-item>Galao</smart-list-item>
    		 <smart-list-item>Greek frappé coffee</smart-list-item>
    		 <smart-list-item>Indian filter coffee</smart-list-item>
    		 <smart-list-item>Instant coffee</smart-list-item>
    		 <smart-list-item>Irish coffee</smart-list-item>
    		 <smart-list-item>Liqueur coffee</smart-list-item>
    	 </smart-combo-box>
     </body>
    </html>
    

    Note how smart.element.js is declared before everything else. This is mandatory for all custom elements.

    Demo

Create, Append, Remove, Get/Set Property, Invoke Method, Bind to Event


Create a new element:
const combobox = document.createElement('smart-combo-box');

Append it to the DOM:
document.body.appendChild(combobox);

Remove it from the DOM:
combobox.parentNode.removeChild(combobox);

Set a property:
combobox.propertyName = propertyValue;

Get a property value:
const propertyValue = combobox.propertyName;

Invoke a method:
combobox.methodName(argument1, argument2);

Add Event Listener:
const eventHandler = (event) => {
// your code here.
};

combobox.addEventListener(eventName, eventHandler);

Remove Event Listener:
combobox.removeEventListener(eventName, eventHandler, true);

Appearance

Smart.ComboBox allows to completely customize the content of the list items.

In order to change the content of an item, itemTemplate property has to be applied by assigning the id of an HTML Template element that's located inside the DOM. Here's an example:

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.elements.js"></script>
</head>
<body>
 <template id="template">
     <span class="glyphicon glyphicon-ok"></span>
     <span> {{label}}</span>
 </template>

 <smart-combo-box item-template="template">
     <smart-list-item selected value="1">Item 1</smart-list-item>
     <smart-list-item value="2">Item 2</smart-list-item>
     <smart-list-item value="3">Item 3</smart-list-item>
 </smart-combo-box>
</body>
</html>

The template contains a binding field surrounded by curly braces {{ label }}. The text between the breckets can be random.

Demo

Smart.ComboBox has the abbility to behave like a simple input field or an input field with a separate drop down button. The dropDownOpenMode property determines how the drop down will open. The element can have two separate buttons: an action and a dropDown button if the property is set to 'dropDownButton'.

Available modes for dropDownOpenMode:

  • none - the popup doesn't open.
  • default - opens if the user clicks on the dropDownButton
  • dropDownButton - the input and the button behave like two separate elements. The drop down button becomes focusable using keyboard interaction.
  • auto - the drop down popup opens when the mouse is over the element and closes when not.
<smart-combo-box drop-down-open-mode="dropDownButton">
     <smart-list-item >Item 1</smart-list-item>
     <smart-list-item selected>Item 2</smart-list-item>
     <smart-list-item>Item 3</smart-list-item>
     <smart-list-item>Item 4</smart-list-item>
     <smart-list-item>Item 5</smart-list-item>
 </smart-combo-box>

Demo

Data Biding

Adding data to the Combo box can be accomplished in a few ways:

  • Nesting Smart.ListItem elements inside the Combo box's body in the HTML:
     <smart-combo-box>
         <smart-list-item value="value1">Item1</smart-list-item>
         <smart-list-item selected value="value2">Item2</smart-list-item>
         <smart-list-item value="value3">Item3</smart-list-item>
     </smart-combo-box>

    Demo

    Smart.ListItem has a label and a value property. The value property can have a separate value from the label or no value at all. The user can prefer to apply a value only to the label and it will be automatically assigned to value as well.

    The text inside the list item represents the value of the label property. It can also be set in the HTML code like shown in the example above.


  • Nesting Smart.ListItemGroup elements inside the ComboBox's body in the HTML:
    <smart-combo-box>
        <smart-list-items-group label="Group 1">
            <smart-list-item>Item 1</smart-list-item>
            <smart-list-item>Item 2</smart-list-item>
            <smart-list-item>Item 3</smart-list-item>
        </smart-list-items-group>
        <smart-list-items-group label="Group 2">
            <smart-list-item>Item 4</smart-list-item>
            <smart-list-item>Item 5</smart-list-item>
            <smart-list-item>Item 6</smart-list-item>
        </smart-list-items-group>
        <smart-list-items-group label="Group 3">
            <smart-list-item>Item 7</smart-list-item>
            <smart-list-item>Item 8</smart-list-item>
            <smart-list-item>Item 9</smart-list-item>
        </smart-list-items-group>
    </smart-combo-box>
    

    Smart.ListItemGroup represents a group of items.

    Grouping can also be enabled later via javascript by applying the grouped property.

    Demo

  • Nesting HTML list elements inside the Combo box's body in the HTML:
    <smart-combo-box>
         <ul label="Group 1">
             <li>Item 1</li>
             <li>Item 2</li>
             <li>Item 3</li>
             <li>Item 4</li>
             <li>Item 5</li>
         </ul>
         <ul label="Group 2">
             <li>Item 6</li>
             <li>Item 7</li>
             <li>Item 8</li>
             <li>Item 9</li>
             <li>Item 10</li>
         </ul>
     </smart-combo-box>
    

    List items can be added without the ul tag. The grouping can be added at a later stage by setting the grouped property to the element.

    Demo

  • Nesting optgroup elements inside the Combo box's body in the HTML:
    <smart-combo-box>
         <optgroup label="Group 1">
             <option value="1">Item 1</option>
             <option value="2">Item 2</option>
         </optgroup>
         <optgroup label="Group 2">
             <option value="3">Item 3</option>
             <option value="4">Item 4</option>
         </optgroup>
     </smart-combo-box>
    

    optgroups are the items of an HTML select element. As usual the value attribute can be applied to each list item to assign a specific value that can differ from the label.

    Demo

  • Setting the dataSource property of the element to a new value of type Array.

    The property can be changed dynamically via javascript after the element has been initialized:

    <!DOCTYPE html>
    <html>
    <head>
     <link rel="stylesheet" href="./../../source/styles/smart.default.css" type="text/css" />
     <script type="text/javascript" src="../../source/smart.elements.js"></script>
     <script>
     window.onload = function () {
         document.querySelector('smart-combo-box').dataSource =
             [
                 "Item 1",
                 "Item 2",
                 "Item 3"
             ];
         }
     </script>
    </head>
    <body>
     <smart-combo-box></smart-combo-box>
    </body>
    </html>

    Demo

    dataSource accepts Arrays of strings or Objects. An object can contain more specific data for the items. For example:

    <!DOCTYPE html>
    <html>
    <head>
     <link rel="stylesheet" href="./../../source/styles/smart.default.css" type="text/css" />
     <script type="text/javascript" src="../../source/smart.elements.js"></script>
     <script>
     window.onload = function () {
         var comboBox = document.querySelector('smart-combo-box');
    
         comboBox.dataSource = [
             {
                 label: 'Item 1',
                 value: 1
             },
             {
                 label: 'Item 2',
                 value: 2
             },
             {
                 label: 'Item 3',
                 value: 3,
                 disabled: true
             },
             {
                 label: 'Item 4',
                 value: 4
             },
             {
                 label: 'Item 5',
                 value: 5
             },
         ];
     } 
     </script>
    </head>
    <body>
     <smart-combo-box></smart-combo-box>
    </body>
    </html>
    

    Demo

    Like every other custom element property, dataSource can also be set from the HTML in the tag of the element, like so:

    <smart-combo-box data-source='["Item1", "Item2", "Item3"]'></smart-combo-box>

    Demo


Manipulating the content

The element offers the following methods:

  • Insert method can create and insert new list items into the element.

    The insert method can be called at any time after the element has been initialized.

    The method accepts two arguments:

    1. The index at which the item will be inserted.
    2. An array of strings or a single string representing the labels of the items to be inserted.

    <!DOCTYPE html>
    <html>
    <head>
     <link rel="stylesheet" href="./../../source/styles/smart.default.css" type="text/css" />
     <script type="text/javascript" src="../../source/smart.elements.js"></script>
     <script>
         window.onload = function () {
             var comboBox = document.querySelector('smart-combo-box');
    
             comboBox.insert(0, ["Item1", "Item2", "Item3"]);
             comboBox.insert(2, 'Item4');
         }
     </script>
    </head>
    <body>
     <smart-combo-box></smart-combo-box>
    </body>
    </html>

    Demo

  • Update method allow List items to be modified after creation.

    Update method accepts two arguments:
    1. The index of the item to be updated.
    2. Details for the item. This argument can be an object containing properties of smartListItem, e.g. label, value, disabled. It can also be a simple string that represents the new label of the item that will be updated.

    <!DOCTYPE html>
    <html>
    <head>
     <link rel="stylesheet" href="./../../source/styles/smart.default.css" type="text/css" />
     <script type="text/javascript" src="../../source/smart.elements.js"></script>
     <script>
         window.onload = function () {
             var comboBox = document.querySelector('smart-combo-box');
    
             //Create items
             comboBox.insert(0, ["Item1", "Item2", "Item3"]);
             comboBox.insert(2, 'Item4');
    
             //Update items
             comboBox.update(1, 'Item12');
             comboBox.update(2, { label: 'Item13', value: '13', disabled: true });
         }
     </script>
    </head>
    <body>
     <smart-combo-box></smart-combo-box>
    </body>
    </html>

    Demo

  • Remove deletes an item from the element. The method accepts a single argument - the index of the item to be removed.

    Here's an example:

    <!DOCTYPE html>
    <html>
    <head>
     <link rel="stylesheet" href="./../../source/styles/smart.default.css" type="text/css" />
     <script type="text/javascript" src="../../source/smart.elements.js"></script>
     <script>
         window.onload = function () {
             var comboBox = document.querySelector('smart-combo-box');
    
             //Create items
             comboBox.insert(0, ["Item1", "Item2", "Item3"]);
             comboBox.insert(2, 'Item4');
    
             //Update items
             comboBox.update(1, 'Item12');
             comboBox.update(2, { label: 'Item13', value: '13', disabled: true });
    
             //Remove items
             comboBox.remove(2);
             comboBox.remove(0);
         }
     </script>
    </head>
    <body>
     <smart-combo-box></smart-combo-box>
    </body>
    </html>

    Demo

  • clearItems removes all items from the element. Takes no arguments:
    window.onload = function () {
        var comboBox = document.querySelector('smart-combo-box');
    
        comboBox.clearItems();
    }
    

Behavior

Smart.ComboBox offers different drop down positions: bottom, top, overlay-center, overlay-bottom, overlay-top, center-top, center-bottom and auto. The position of the listBox drop down can be modified via the dropDownPosition property, like so:

<!DOCTYPE html>
<html>
<head>
 <link rel="stylesheet" href="./../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.elements.js"></script>
 <script>
     window.onload = function () {
         var comboBox = document.querySelector('smart-combo-box');

         comboBox.dropDownPosition = 'top';
     }
 </script>
</head>
<body>
 <smart-combo-box>
    <smart-list-item>Item 1</smart-list-item>
    <smart-list-item>Item 2</smart-list-item>
    <smart-list-item>Item 3</smart-list-item>
 </smart-combo-box>
</body>
</html>

Demo

Similar to any other property dropDownPosition can be set in the HTML with an attribute:

<smart-combo-box drop-down-position="bottom">
    <smart-list-item>Item 1</smart-list-item>
    <smart-list-item>Item 2</smart-list-item>
    <smart-list-item>Item 3</smart-list-item>
</smart-combo-box>

Demo

The default value of the property is 'auto'. This means that the element will automatically determine the position of the drop down depending on the space available. The algorithm always starts by trying to place the drop down below the element. If there's no space available it moves it above the element.

Keyboard Support

The following keys apply to the Combo box element:

Key Action
Alt + Arrow Up/Down Open/Close the drop down popup.
Enter Select/Unselect an item form the list or open/close the drop down popup if the drop down button is focused when dropDownOpenMode is set to dropDownButton.
Arrow Up Navigate to the previous item from the list if the drop down popup is opened.
Arrow Down Navigate to the next item in the list if the drop down popup is opened.
Escape Close the drop down popup.