Giter VIP home page Giter VIP logo

jquery-accessible-autocomplete-list-aria's Introduction

jQuery accessible autocomplete list

This jQuery plugin will transform and enhance a simple text input with a datalist into a wonderful and shiny input with autocomplete, and accessible.

The demo is here: https://a11y.nicolas-hoffmann.net/autocomplete-list/

How it works

This plugin:

  • starts from a classical input completed by a datalist;
  • then takes the elements in your datalist, reorder them and store them into Javascript arrays;
  • then wraps the input into a container, adds status/help text zones and a “clear” button;
  • then it listens when you type something in the field, and proposes you some suggestions.

Why it is cool

As it is made using progressive enhancement, if the JavaScript fails to load or is blocked by your visitor, your website will still be ok.

It is accessible: it works with VoiceOver, NVDA, Jaws, and it loves keyboard.

It is easy to customize there is a default configuration that can be changed/adapted, and you can override it using data-* attributes if you need it.

For the design, you can also customize the CSS to fullfil your needs.

How to use it

First, add a class js-combobox on an input with a working datalist:

<input type="text" name="mysuper_field" id="id_mysuper_field" class="js-combobox" list="beers" data-combobox-prefix-class="combobox" />
 <datalist id="beers">
  <option value="Meteor">
  <option value="Pils">
  <option value="Kronenbourg">
  <option value="Kronenbourg 2 (for test)">
  <option value="Kronenbourg 3 (for test)">
  <option value="Grimbergen">
 </datalist>

Then download the plugin and use jQuery of course. This plugin is also available on NPMjs.com and can be installed using npm i jquery-accessible-autocomplete-list-aria: https://www.npmjs.com/package/jquery-accessible-autocomplete-list-aria

You may also get it from Bower: bower install jquery-accessible-autocomplete-list-aria.

You may use some options, so have a look at plugin options.

Options

At the beginning of the plugin, a global configuration is available:

var $js_combobox = $('.js-combobox'),
 $body = $('body'),
 default_text_help = 'Use tabulation (or down) key to access and browse suggestions after input. Confirm your choice with enter key, or esc key to close suggestions box.',
 default_class_for_invisible_text = 'invisible',
 suggestion_single = 'There is ',
 suggestion_plural = 'There are ',
 suggestion_word = 'suggestion',
 suggestion_word_plural = 'suggestions',
 button_clear_title = 'clear this field',
 button_clear_text = 'X',
 case_sensitive = 'yes',
 min_length = 0, 
 limit_number_suggestions = 666,
 search_option = 'beginning', // or 'containing'
 see_more_text = 'See more results…',
 tablo_suggestions = [];

You may adapt texts to your need with it. However, if you need to customize one autocomplete field, you may use data-* attributes to override global configuration. Here is the list:

  • data-combobox-prefix-class="foo": prefix all generated classes by foo.
  • data-combobox-case-sensitive="yes/no": plugin is case-sensitive by default, which means “KROW” will be before “Kron”. If you set it up to ="no", the plugin will order all elements without caring of the case (see first example).
  • data-combobox-help-text: to override default_text_help in the config.
  • data-combobox-button-title: to override button_clear_title in the config, will be the title of the clear button.
  • data-suggestion-single: to override suggestion_single in the config, will be vocalized when there is 1 suggestion.
  • data-suggestion-plural: to override suggestion_plural in the config, will be vocalized when there are more several suggestions.
  • data-suggestion-word: to override suggestion_word in the config.
  • data-suggestion-word-plural: to override suggestion_word_plural in the config.
  • data-combobox-min-length="xx": to override min_length in the config, a minimal length of text from which will be displayed suggestions.
  • data-combobox-limit-number-suggestions="3": to override limit_number_suggestions, the maximum number of suggestions displayed.
  • data-combobox-search-option="beginning/containing": when plugin is searching a match with foo in the suggestions, beginning value will search suggestions beginning with foo, containing will search suggestions containing foo.
  • data-combobox-see-more-text="<text>": if the number limit of suggestions is reached, the plugin will put this text as last suggestion.
  • data-combobox-notab-options="yes": if you don’t want to enable the tab key to go into/through suggestions.

Have a look at the second example of the demo if you want to see data attributes in action.

How to style it

In these examples, I’ve used data-combobox-prefix-class="combobox", which means all classes will be prefixed by combobox-. Here are minimal styles needed:

.invisible {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

.combobox-container {
  position: relative;
  max-width: 250px;
}

.combobox-suggestions {
  position: absolute;
}
.combobox-suggestion {
  cursor: pointer;
}

.combobox-clear-button {
  position: absolute;
  right: 1px;
  font-family: inherit;
  font-size: 1em;
  padding: .5em;
  background: transparent
}

Here is a full example really nicer and more pleasant to use:

.invisible {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

.combobox-container {
  position: relative;
  max-width: 250px;
  margin: 0 auto;
}

.combobox-suggestions {
  position: absolute;
  left: 0;
  width: 250px;
  background: #fff;
}
.combobox-suggestion {
  color: #666;
  border-bottom: 1px solid #000;
  border-left: 1px solid #000;
  border-right: 1px solid #000;
  cursor: pointer;
  text-align: left;
}
.combobox-suggestion:first-child {
  border-top: 1px solid #000;
}
.combobox-suggestion:hover,
.combobox-suggestion:focus {
  color: #000;
}

.combobox-clear-button {
  position: absolute;
  right: 1px;
  font-family: inherit;
  font-size: 1em;
  padding: .5em;
  background: transparent
}

/* http://geektnt.com/how-to-remove-x-from-search-input-field-on-chrome-and-ie.html */
.js-combobox[type=text]::-ms-clear { display: none; width: 0; height: 0; }
.js-combobox[type=text]::-ms-reveal { display: none; width: 0; height: 0; }

With these styles, the focus/hover looks far better, and a border is added aroung suggestion list.

Keys

When focus is on the text input

  • You may use tab to reach suggestions and continue through them (activated by default, can be removed using data-combobox-notab-options="yes");
  • Esc will close suggestion list;
  • You can also use Down to reach the suggestion list.

When focus is on the suggestions

  • Tab/Shift+Tab allow to navigate through options (activated by default, can be removed using data-combobox-notab-options="yes");
  • Enter or Space update the value of the input with the currently highlighted option, close the drop-down list and move the focus back to the input;
  • Esc will close suggestion list, give focus back to the text input and remove suggestion in the input (go back to what was typed in);
  • You can also use Down/Up arrows to navigate between suggestions.

Other informations

License

No license problem, it uses MIT license, so it’s free, open-source and you can do whatever you want with it, including commercial use. This permission notice shall be included in all copies or substantial portions of it.

However, it is not prohibited to tell me that you’ve used it, or send me a little “thank you”. ;)

Weight

It is lightweight: 19kb(development), 6.8kb (minified), only 2.1kb minified and gzipped.

Tribute

The idea of this script is inspired by Heydon’s basic autocomplete with listbox demo. Aurélien Levy enhanced it with a working prototype, and I’ve finished the work (packaging, reutilisability, progressive enhancement, etc.) with Aurélien.

Chuck Norris approved

jquery-accessible-autocomplete-list-aria's People

Contributors

nico3333fr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jquery-accessible-autocomplete-list-aria's Issues

JSFiddle example

Are there any JSFiddle examples of this working? I can't get this to work. The demo looks great, but I cannot figure out how to get the code running correctly.

Regular autocomplete works when using:
<input type="text" list="beers" />

When I add the attributes from the demo, it no longer works:
<input type="text" name="mysuper_field" id="id_mysuper_field" class="js-combobox" list="beers" data-combobox-prefix-class="combobox" />

Here is my JsFiddle:
https://jsfiddle.net/edasxuh0/12/

I would like to get this working and figure out how to dynamically add about 5000 options to the suggestion list. I already have it working in regular autocomplete but I would much rather make it JAWS accessible and compliant.

Any ideas what I am doing wrong? Help?

Searching from beginning with case sensitivity off populates incorrect suggestions

I used the example code from the README, and added data-combobox-case-sensitive="no" and data-combobox-search-option="beginning":

<input type="text" name="mysuper_field" id="id_mysuper_field" class="js-combobox" list="beers" data-combobox-prefix-class="combobox" data-combobox-case-sensitive="no" data-combobox-search-option="beginning"/>
  <datalist id="beers">
    <option value="Meteor">
    <option value="Pils">
    <option value="Kronenbourg">
    <option value="Kronenbourg 2 (for test)">
    <option value="Kronenbourg 3 (for test)">
    <option value="Grimbergen">
  </datalist>

Searching for "b" should provide no suggestions but gives Grimbergen and the 3 Kronenbourg options as suggestions. This works fine with case sensitivity left as the default (on), but for my use case I need to have it off.

It looks like the issue is in the big conditional that splits for containing/beginning - since the containing section evaluates to true (it winds up as false && (false) || (true)), it doesn't matter that the correct end of the conditional found no results.

ampersand in list element converted to html entity

First, great script - thank you!

I've noticed that when adding an item to the <datalist> that contains an ampersand, upon selection the ampersand becomes &amp; in the input field and subsequent submitted data. This can be replicated on your demo site using the browser dev tool to change the contents of your beer list, for example. Add "Corona & Lime" and you'll see what I mean.

Screenreader speaks only on first-time typing the same char how many suggestions found.

Hi,
I have tested this autocomplete plugin with FF52 - JAWS 17 and NVDA. Great job. But one thing:

Test cases:

  • TAB to input "Choose a beer" typing k -> screenreader speaks "... 4 suggestions ..." -> delete k -> typing k agian screenreader still silient
  • TAB to input "Choose a beer" typing k -> screenreader speaks "... 4 suggestions ..." -> delete k -> switch to another tabable -> come back to input -> typing k screenreader still also silient

And I am not sure if it a desired behavior. I think it will be good the screenreader speak the suggestion count agian?

Thanks in advance.

Lars

Multiple input with same single datalist

Is it possibile to have multiple inputs that referes to a single datalist.

For example:

<label for="departure">From</label>
<input type="text" id="departure" list="cities" />

<label for="arrival">To</label>
<input type="text" id="arrival" list="cities" />

<datalist id="beers">
  <option value="Rome">
  <option value="Madrid">
  <option value="London">
</datalist>

Case-Insensitive keyword matching

In case anybody cares, I was able to easily implement "case insensitive" search feature by simply changing one line of code.

I changed this:
( $combobox_case_sensitive==='no' && tablo_suggestions[index_table][i].substring(0,value_to_search.length).toUpperCase() === value_to_search.toUpperCase() )

to instead make use of "contains", like this:
if (tablo_suggestions[index_table][i].toLowerCase().indexOf(value_to_search.toLowerCase()) >= 0)

I also went a step further and limited my output results to only 6 suggestions max, like so:
if ( tablo_suggestions[index_table][i].toLowerCase().indexOf(value_to_search.toLowerCase()) >= 0 && counter < 6)

So now, I am able to apply my keyword to search for a pattern within the entire string, not just from the beginning of the string. This replicates the standard jQuery Autocomplete behaviour, in case you are migrating to this accessible version from that instead.

Here it is in action, in full glory:
image

Plural for data-suggestion-word

Hi, is there a way to set the plural of data-suggestion-word?
There are some languages (eg: Italian) that use "Suggerimento" for the singular, and "Suggerimenti" for the plural.
Can I set the plural suggestion word via data-*?

Actually the plugin adds the "s" word at the end of the suggestion word, this works for English language but not for other languages.

Thanks

MinLength attribute for search

Hello Nicolas
First of all thank you for your awesome work.

How can I customize the plugin to enable a minLength attribute?

Thanks

Npm module is not properly configured

Hi,

I just installed your plugin through npm, but I couldn't import it correctly.

The problem seems to arise because there is no main field in package.json, pointing to the dist file.

Also, there is a little typo in the dist file : jquery-accessible-autocomplet-list-aria.js instead of jquery-accessible-autocomplet[e]-list-aria.js ;)

In 'no tab' mode, selected option disappears after tabbing to 'clear' button

In the second example using the 'no tab' mode, there seems to be an issue selecting an option with the keyboard (In Firefox, IE11 and Chrome). When selecting an option with arrow down, I loose the selection after tabbing away to the Clear button.
1st scenario : I type 'Cr', then I select 'Croissant au chocolat' with the arrow keys, then I hit TAB. I end up on the 'clear' button and the input only contains 'Cr'. I expect that tabbing away from the list confirms my selection as it would in a select.
2nd scenario : I type 'Cr', then I select 'Croissant au chocolat' with the arrow keys, I hit ENTER. The focus moves to the input which contains 'Croissant au chocolat'. Then I hit TAB. I end up on the 'clear' button and the input only contains 'Cr'. I don't expect my input to disappear when tabbing away.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.