Autocomplete

Autocomplete provides automated assistance to fill in form field values. It allows the user to have suggestions while typing in the field.

Example

Fluid provides two types of autocomplete input fields: floating (default) and static.

For floating inputs, an empty placeholder is MANDATORY.

Our inputs are pure css components and require no additional javascript. In order to create the floating label, an empty placeholder is needed.

The label indicates the type of information required by the control.

Floating

keyboard_arrow_down
<form autocomplete="off">
  <div class="nj-form-item nj-form-item--autocomplete" 
    data-list='[{"name":"Afghanistan","value":"AF"},{"name":"Åland Islands","value":"AX"},{"name":"Albania","value":"AL"},{"name":"Algeria","value":"DZ"},{"name":"American Samoa","value":"AS"},{"name":"AndorrA","value":"AD"},{"name":"Angola","value":"AO"},{"name":"Anguilla","value":"AI"},{"name":"Antarctica","value":"AQ"},{"name":"Antigua and Barbuda","value":"AG"},{"name":"Argentina","value":"AR"},{"name":"Armenia","value":"AM"},{"name":"Aruba","value":"AW"},{"name":"Australia","value":"AU"},{"name":"Austria","value":"AT"},{"name":"Azerbaijan","value":"AZ"},{"name":"Bahamas","value":"BS"},{"name":"Bahrain","value":"BH"},{"name":"Bangladesh","value":"BD"},{"name":"Barbados","value":"BB"},{"name":"Belarus","value":"BY"},{"name":"Belgium","value":"BE"},{"name":"Belize","value":"BZ"},{"name":"Benin","value":"BJ"},{"name":"Bermuda","value":"BM"},{"name":"Bhutan","value":"BT"},{"name":"Bolivia","value":"BO"},{"name":"Bosnia and Herzegovina","value":"BA"},{"name":"Botswana","value":"BW"},{"name":"Bouvet Island","value":"BV"},{"name":"Brazil","value":"BR"},{"name":"British Indian Ocean Territory","value":"IO"},{"name":"Brunei Darussalam","value":"BN"},{"name":"Bulgaria","value":"BG"},{"name":"Burkina Faso","value":"BF"},{"name":"Burundi","value":"BI"},{"name":"Cambodia","value":"KH"},{"name":"Cameroon","value":"CM"},{"name":"Canada","value":"CA"},{"name":"Cape Verde","value":"CV"},{"name":"Cayman Islands","value":"KY"}]'>
    <div class="nj-form-item__field-wrapper">
      <input class="nj-form-item__field" id="exampleAutocompleteFloating" placeholder=" " autocomplete="off"/>
      <label for="exampleAutocompleteFloating" class="nj-form-item__label">Floating autocomplete</label>
      <div class="nj-form-item__list nj-list-group nj-list-group--no-border nj-list-group--sm" role="listbox" tabindex="-1">
        <!-- This is the template of an item!-->
        <button type="button" class="nj-list-group__item" role="option" tabindex="-1" />
      </div>
      <i class="nj-form-item__icon material-icons">keyboard_arrow_down</i>
    </div>
  </div>
</form>

Static

keyboard_arrow_down
<form autocomplete="off">
  <div class="nj-form-item nj-form-item--autocomplete nj-form-item--static"
    data-list='[{"name":"Afghanistan","value":"AF"},{"name":"Åland Islands","value":"AX"},{"name":"Albania","value":"AL"},{"name":"Algeria","value":"DZ"},{"name":"American Samoa","value":"AS"},{"name":"AndorrA","value":"AD"},{"name":"Angola","value":"AO"},{"name":"Anguilla","value":"AI"},{"name":"Antarctica","value":"AQ"},{"name":"Antigua and Barbuda","value":"AG"},{"name":"Argentina","value":"AR"},{"name":"Armenia","value":"AM"},{"name":"Aruba","value":"AW"},{"name":"Australia","value":"AU"},{"name":"Austria","value":"AT"},{"name":"Azerbaijan","value":"AZ"},{"name":"Bahamas","value":"BS"},{"name":"Bahrain","value":"BH"},{"name":"Bangladesh","value":"BD"},{"name":"Barbados","value":"BB"},{"name":"Belarus","value":"BY"},{"name":"Belgium","value":"BE"},{"name":"Belize","value":"BZ"},{"name":"Benin","value":"BJ"},{"name":"Bermuda","value":"BM"},{"name":"Bhutan","value":"BT"},{"name":"Bolivia","value":"BO"},{"name":"Bosnia and Herzegovina","value":"BA"},{"name":"Botswana","value":"BW"},{"name":"Bouvet Island","value":"BV"},{"name":"Brazil","value":"BR"},{"name":"British Indian Ocean Territory","value":"IO"},{"name":"Brunei Darussalam","value":"BN"},{"name":"Bulgaria","value":"BG"},{"name":"Burkina Faso","value":"BF"},{"name":"Burundi","value":"BI"},{"name":"Cambodia","value":"KH"},{"name":"Cameroon","value":"CM"},{"name":"Canada","value":"CA"},{"name":"Cape Verde","value":"CV"},{"name":"Cayman Islands","value":"KY"}]'>
    <div class="nj-form-item__field-wrapper">
      <input class="nj-form-item__field" id="exampleAutocompleteStatic" placeholder="Example" autocomplete="off"/>
      <label for="exampleAutocompleteStatic" class="nj-form-item__label">Static autocomplete</label>
      <div class="nj-form-item__list nj-list-group nj-list-group--no-border nj-list-group--sm" role="listbox" tabindex="-1">
        <button type="button" class="nj-list-group__item" role="option" tabindex="-1" />
      </div>
      <i class="nj-form-item__icon material-icons">keyboard_arrow_down</i>
    </div>
  </div>
</form>

Options

You can pass the following data-options

limit number | boolean : number of item displayed. If false, unlimited number of results

  • default = false

noResultMessage string : message shown if there is no match

  • default = “No results”

showResultNumber boolean : if true, shows the number of results and the resultNumberMessage

  • default = false

resultsNumberMessage - string : message shown after the number of results

  • default = “results”

Below is an example with the following options :

data-options='{"noResultMessage": "No results found", "resultsNumberMessage": "results found", "showResultNumber": true, "limit": 10}'

keyboard_arrow_down
<form autocomplete="off">
  <div class="nj-form-item nj-form-item--autocomplete"
       data-list='[{"name":"Afghanistan","value":"AF"},{"name":"Åland Islands","value":"AX"},{"name":"Albania","value":"AL"},{"name":"Algeria","value":"DZ"},{"name":"American Samoa","value":"AS"},{"name":"AndorrA","value":"AD"},{"name":"Angola","value":"AO"},{"name":"Anguilla","value":"AI"},{"name":"Antarctica","value":"AQ"},{"name":"Antigua and Barbuda","value":"AG"},{"name":"Argentina","value":"AR"},{"name":"Armenia","value":"AM"},{"name":"Aruba","value":"AW"},{"name":"Australia","value":"AU"},{"name":"Austria","value":"AT"},{"name":"Azerbaijan","value":"AZ"},{"name":"Bahamas","value":"BS"},{"name":"Bahrain","value":"BH"},{"name":"Bangladesh","value":"BD"},{"name":"Barbados","value":"BB"},{"name":"Belarus","value":"BY"},{"name":"Belgium","value":"BE"},{"name":"Belize","value":"BZ"},{"name":"Benin","value":"BJ"},{"name":"Bermuda","value":"BM"},{"name":"Bhutan","value":"BT"},{"name":"Bolivia","value":"BO"},{"name":"Bosnia and Herzegovina","value":"BA"},{"name":"Botswana","value":"BW"},{"name":"Bouvet Island","value":"BV"},{"name":"Brazil","value":"BR"},{"name":"British Indian Ocean Territory","value":"IO"},{"name":"Brunei Darussalam","value":"BN"},{"name":"Bulgaria","value":"BG"},{"name":"Burkina Faso","value":"BF"},{"name":"Burundi","value":"BI"},{"name":"Cambodia","value":"KH"},{"name":"Cameroon","value":"CM"},{"name":"Canada","value":"CA"},{"name":"Cape Verde","value":"CV"},{"name":"Cayman Islands","value":"KY"},{"name":"Central African Republic","value":"CF"},{"name":"Chad","value":"TD"},{"name":"Chile","value":"CL"},{"name":"China","value":"CN"},{"name":"Christmas Island","value":"CX"},{"name":"Cocos (Keeling) Islands","value":"CC"},{"name":"Colombia","value":"CO"},{"name":"Comoros","value":"KM"},{"name":"Congo","value":"CG"},{"name":"Congo, The Democratic Republic of the","value":"CD"},{"name":"Cook Islands","value":"CK"},{"name":"Costa Rica","value":"CR"},{"name":"Croatia","value":"HR"},{"name":"Cuba","value":"CU"},{"name":"Cyprus","value":"CY"},{"name":"Czech Republic","value":"CZ"},{"name":"Denmark","value":"DK"},{"name":"Djibouti","value":"DJ"},{"name":"Dominica","value":"DM"},{"name":"Dominican Republic","value":"DO"},{"name":"Ecuador","value":"EC"},{"name":"Egypt","value":"EG"},{"name":"El Salvador","value":"SV"},{"name":"Equatorial Guinea","value":"GQ"},{"name":"Eritrea","value":"ER"},{"name":"Estonia","value":"EE"},{"name":"Ethiopia","value":"ET"},{"name":"Falkland Islands (Malvinas)","value":"FK"},{"name":"Faroe Islands","value":"FO"},{"name":"Fiji","value":"FJ"},{"name":"Finland","value":"FI"},{"name":"France","value":"FR"},{"name":"French Guiana","value":"GF"},{"name":"French Polynesia","value":"PF"},{"name":"French Southern Territories","value":"TF"},{"name":"Gabon","value":"GA"},{"name":"Gambia","value":"GM"},{"name":"Georgia","value":"GE"},{"name":"Germany","value":"DE"},{"name":"Ghana","value":"GH"},{"name":"Gibraltar","value":"GI"},{"name":"Greece","value":"GR"},{"name":"Greenland","value":"GL"},{"name":"Grenada","value":"GD"},{"name":"Guadeloupe","value":"GP"},{"name":"Guam","value":"GU"},{"name":"Guatemala","value":"GT"},{"name":"Guernsey","value":"GG"},{"name":"Guinea","value":"GN"},{"name":"Guinea-Bissau","value":"GW"},{"name":"Guyana","value":"GY"},{"name":"Haiti","value":"HT"},{"name":"Heard Island and Mcdonald Islands","value":"HM"},{"name":"Holy See (Vatican City State)","value":"VA"},{"name":"Honduras","value":"HN"},{"name":"Hong Kong","value":"HK"},{"name":"Hungary","value":"HU"},{"name":"Iceland","value":"IS"},{"name":"India","value":"IN"},{"name":"Indonesia","value":"ID"},{"name":"Iran, Islamic Republic Of","value":"IR"},{"name":"Iraq","value":"IQ"},{"name":"Ireland","value":"IE"},{"name":"Isle of Man","value":"IM"},{"name":"Israel","value":"IL"},{"name":"Italy","value":"IT"},{"name":"Jamaica","value":"JM"},{"name":"Japan","value":"JP"},{"name":"Jersey","value":"JE"},{"name":"Jordan","value":"JO"},{"name":"Kazakhstan","value":"KZ"},{"name":"Kenya","value":"KE"},{"name":"Kiribati","value":"KI"},{"name":"Korea, Republic of","value":"KR"},{"name":"Kuwait","value":"KW"},{"name":"Kyrgyzstan","value":"KG"},{"name":"Latvia","value":"LV"},{"name":"Lebanon","value":"LB"},{"name":"Lesotho","value":"LS"}]'
       data-options='{"noResultMessage": "No results found", "resultsNumberMessage": "results found", "showResultNumber": true, "limit": 10}'>
    <div class="nj-form-item__field-wrapper">
      <input class="nj-form-item__field" id="exampleAutocompleteOptions" placeholder="Example" autocomplete="off"/>
      <label for="exampleAutocompleteOptions" class="nj-form-item__label">Autocomplete with options</label>
      <div class="nj-form-item__list nj-list-group nj-list-group--no-border nj-list-group--sm" role="listbox" tabindex="-1">
        <button type="button" class="nj-list-group__item" role="option" tabindex="-1" />
      </div>
      <i class="nj-form-item__icon material-icons">keyboard_arrow_down</i>
    </div>
  </div>
</form>

Tag attribute / property

Use tag attribute to initiate component. You can initiate via JavaScript too.

Root Tag

Attribute Description
data-list Array of json objects
Display a filtered data list while user is filling input field.

name displayed text
value reference value. Value will be copied on user interaction to input field and component root tag.

data-list='[{"name":"Afghanistan","value":"AF"},{"name":"Åland Islands","value":"AX"},{"name":"Albania","value":"AL"},...}]'
data-options Json object
Settings

limit number | false Limit the number of items displayed
noResultMessage string - Message shown if there is no match
showResultNumber boolean - If true, shows the number of results and the resultNumberMessage
resultsNumberMessage string - Message shown after the number of results

data-options='{"noResultMessage": "No results found", "resultsNumberMessage": "results", "showResultNumber": true, "limit": 20}'>
nj_form_autocomplete get Autocomplete instance

const instanceOfAutocomplete = document.querySelector('#autocomplete').nj_form_autocomplete;

Input

Attribute Description
minlength set minlength input attribute to toggle list display
required set required input to toggle list display

Methods

You can initiate and take control of autocomplete component via javascript.

Method Description
data set or get
Array of json objects
Array<{ name: string; value: string }>

Database used to populate list while user is filling input field.

instanceOfAutocomplete.data = [ { name: ‘Afghanistan’, value: ‘AF’},…];
list set or get
Array of json objects
Array<{ name: string; value: string }>

Filter given array then display items or return displayed list.

instanceOfAutocomplete.list = [ { name: ‘Afghanistan’, value: ‘AF’},…];
onKeyDown WCAG
onInputChange Populate list with data
onSelectListItem Fill input with user selection
onUserSelectItem a simple function to override to get selected name and value

instance.onUserSelectItem = ({name, value}) => {console.log(name,value);}
getInstance Static method which allows you to get the AutoComplete instance associated to a DOM element, you can use it like this:

NJ.Form.AutoComplete.getInstance( element )

Usage

Alert component usage

Best practices

Do / Don’t

alt test

Sort the autocomplete items in alphabetical order. This will make it easier for the user to find the item they are looking for.

alt test

Do not sort them by following any logic