Predictive Text

Predictive Text

The predictive text pattern allows users to type in values or to select values from a list of possible matches that appears after the user starts typing. Screen readers announce the availability of predictive text options, and users can select the options with keyboard, touch, or mouse.



Turn on a screen reader to experience this example in action.

    <div class="deque-predictive-text">
      <form id="chooseState">
         <label for="acInput" id="acInputLb">What state do you live in?</label>
        <div id="states-predictive-text"></div>
        <input type="submit" class="deque-button" value="Submit">
      </form>
    </div>

    Required: The complete JavaScript file (for all patterns in the library): deque-patterns.min.js

    Required: Initialization JavaScript (with functionality specific to individual pattern instances):

    var data = ['Alabama', 'Alaska', 'American Samoa',
      'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',
      'Delaware', 'District of Columbia', 'Federated States of Micronesia',
      'Florida', 'Georgia', 'Guam', 'Hawaii', 'Idaho', 'Illinois', 'Indiana',
      'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Marshall Islands',
      'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
      'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
      'New Mexico', 'New York', 'North Carolina', 'North Dakota',
      'Northern Mariana Islands', 'Ohio', 'Oklahoma', 'Oregon', 'Palau',
      'Pennsylvania', 'Puerto Rico', 'Rhode Island', 'South Carolina',
      'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virgin Island',
      'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
    var config = {};
    
    var ac = deque.createPredictiveText(data, config);
    var input = ac.getInputElement();
    input.id = 'acInput';
    // input.setAttribute('aria-label', 'Predictive text input - type slowly to get the results');
    
    document
      .getElementById('states-predictive-text')
      .appendChild(ac);
    
    /* The following is for optional form validation purposes */
    input.setAttribute('data-validate', 'required');
    var form1 = document.getElementById('chooseState');
    deque.configureFormValidation(form1, {});
    

    Required: The complete CSS file (for all patterns in the library): deque-patterns.min.css

    Key styles within the CSS file (other styles may also be necessary):

    
    .deque-predictive-text label {
      float: left;
      width: 100%;
    }
    .deque-predictive-text #states-predictive-text {
      float: left;
    }
    .deque-predictive-text .deque-predictive-text-combobox {
      display: inline-block;
      position: relative;
      /*input[type='text'][aria-expanded='true'] + */
    }
    .deque-predictive-text .deque-predictive-text-combobox input[type='text'] {
      margin-bottom: -1px;
      height: 27px;
      margin-right: 10px;
      width: 200px;
    }
    .deque-predictive-text .deque-predictive-text-combobox .deque-predictive-text-highlight {
      background-color: #ffdc7c;
      outline: 1px solid transparent;
    }
    .deque-predictive-text .deque-predictive-text-combobox ul li.deque-predictive-text-hint {
      list-style-type: none;
      cursor: pointer;
      padding: 5px 10px;
    }
    .deque-predictive-text .deque-predictive-text-combobox .deque-predictive-text-hints {
      padding-left: 0;
      position: absolute;
      top: 13px;
      left: -1px;
      width: 100%;
      background: #ffffff;
      z-index: 1;
    }
    .deque-predictive-text .deque-predictive-text-combobox .deque-predictive-text-hints:empty {
      display: none;
    }
    .deque-predictive-text .deque-predictive-text-combobox .deque-predictive-text-hints {
      border: 1px solid #000000;
    }
    .deque-predictive-text input[type='submit'] {
      font-size: 15px;
      max-width: 374px;
      min-width: 120px;
      display: inline-block;
      margin-top: 12px;
      padding: 9px 12px 10px;
      border: solid 1px transparent;
      overflow: hidden;
      line-height: 1;
      text-align: center;
      white-space: nowrap;
      vertical-align: bottom;
      background: #006cc1;
      outline: none;
      color: #ffffff;
      appearance: none;
    }
    .deque-predictive-text input[type='submit']:hover {
      cursor: pointer;
      background-color: #006cc2;
      border-color: rgba(0, 0, 0, 0.4);
    }
    .deque-predictive-text input[type='submit']:focus {
      outline: 1px dashed #000000;
    }