Autocomplete and selection widgets

The widgets contrib puts over a select or an autocomplete, from seven modules at once, so they can be read against each other and against the three shapes core ships. Select2, Select A11y NG, Autocomplete Deluxe, Tagify, Select or Other, Client-side Hierarchical Select and one Chosen widget for the comparison: they solve the same problem and produce markup with nothing in common, so the theme has to meet each of them separately. Nothing is saved. The option lists are made up, so most of the page renders the same anywhere; the widgets that query the site for their suggestions say so, and their results are real.

Core, for comparison

The three shapes core ships, at the top of the page so the rest can be read against them. Everything below replaces one of these: a select that cannot be searched, checkboxes that do not scale past a screenful, and an autocomplete that shows nothing until it is typed into.

Select

One <select>, styled by the theme and opened by the browser. The drop is drawn by the operating system, so it cannot be themed and does not appear in the DOM.

Checkboxes

Every option is on the page at once, which is what makes it readable and what stops it scaling.

Rooms

Entity autocomplete

A textfield with a jQuery UI drop under it. The suggestions come from a selection handler of this module rather than from the site, so they are made up like everything else here; the value it submits is the label with the id in brackets after it.

Select2

Replaces the select with a rendered drop that can be searched, and is the only widget here that can both hold a full option list and fetch one over Ajax. The markup is a <span class="select2"> next to the original select, which stays in the DOM and keeps the value: everything visible is .select2-selection and .select2-results, and neither carries a class from the theme.

Single

The default. A search field appears above the results once the list is long enough for the library to think it is worth one.

Placeholder and clear

allowClear adds a cross inside the selection, which needs an empty option to clear back to. The placeholder is a rendered span rather than a placeholder attribute, so it takes its color from the library.

Multiple

Selected options become removable pills inside the control, and the control grows down as they are added. Sortable: the pills can be dragged into order, which is what the core/sortable dependency is for.

Capped at three

#cardinality closes the drop once the cap is reached and reopens it when a pill is removed. The cap is enforced in the browser and again on submission.

Grouped

An optgroup becomes a non-selectable heading in the result list. Matching searches inside a group, and a group with no match is dropped rather than shown empty.

Autocomplete

#autocomplete stops the options being rendered into the page and fetches them over Ajax instead, through the same selection handler an entity reference field would use -- here the dummy one, so the drop is a made-up list. Typing is required: minimumInputLength is 1.

Disabled

The library adds its own disabled class rather than relying on the attribute, and the whole control stops being focusable.

Select A11y NG

The same idea as Select2 without jQuery, and built to be announced properly: the control is a real button, the list is a listbox, and the count of selected options is read out through a live region. Selected options are listed as removable buttons under the control rather than as pills inside it, so the control does not change height as it fills. A fork of Select A11y, on a maintained fork of its library.

Single

A button showing the current option, and a panel with a search field and the list under it. Nothing is drawn until the panel is opened.

Multiple

The selected options come out below the control as a list of buttons, each removing its own option. That is the part a theme has to style: it is ordinary markup, not something the library paints.

Grouped

The library reads the options and not the optgroups around them, so the groups are gone: the listbox is one flat list with no headings. This is where it falls short of a native select, and of Select2 above.

Autocomplete Deluxe

An autocomplete rather than a select: it never holds the full option list, and it turns what has been picked into removable items sitting inside the field. It is built on core/drupal.autocomplete, so the drop is the jQuery UI one core already styles -- the new markup is the container and the items, not the suggestion list.

Single value

Capped at one: once an item is in the field the input stops accepting more, and removing the item lets it accept again.

Multiple values

Uncapped. The items wrap inside the container and the input follows the last of them, which is the layout most of the CSS of this module is about.

New values allowed

With new terms turned on, text that matches nothing is offered as something to add rather than rejected. The offer is a line appended to the suggestion list, so it is themed with the suggestions and not separately.

Tagify

Draws a box of its own, a custom tags element, in front of the control it is given and keeps that control in the page beneath it, absolutely positioned and transparent. The tags, the typing area and the suggestion list are all inside the box, so it is themed apart from the control it wraps.

Over an entity autocomplete

What it collects is shown as tags in the box rather than as a comma-separated string, and goes back to the server as JSON in the one text field. The suggestions come over Ajax through Tagify's own route and the dummy handler; on this page they are answered from its list and no account is loaded.

Over a select

A multiple select with the tags rendered from its selected options. The script puts a text input before the select and runs Tagify on that; adding or removing a tag selects or clears the matching option, and the select is what submits.

Select or Other

A different problem: not how to choose from a long list, but what to do when the right answer is not on it. The element is a chooser plus a textfield, and the textfield is shown only while the "Other" option is the one selected -- through #states, so the toggling is core behavior and needs no JavaScript of its own.

Select

The select carries an extra "Other" option the module appends. Choosing it reveals the textfield below; choosing anything else hides it again and its value is dropped.

Radios

The same element with a single-value button set instead of a select. The textfield sits after the last radio, which is the layout worth checking: it is inside the fieldset the radios are wrapped in.

Checkboxes

#multiple turns the buttons into checkboxes, and "Other" becomes one more thing that can be ticked alongside the rest rather than instead of them.

Chosen

Here for the comparison only. Chosen is the oldest of these and the one this project uses most, so it has a page to itself: every shape it takes, and a toolbar that pins the drop open so it can be inspected.

Multiple

The same job as the Select2 multiple above, in markup that shares none of its class names.

The Chosen widgets in full

Client-side Hierarchical Select

A different answer to a long list: split it up by its hierarchy and show one level at a time. The whole tree is rendered into a single hidden <select>, each option carrying its parent in data-parent, and the script builds one ordinary select per level from it. Choosing an option adds a select for its children, so the row grows to the right as the choice gets deeper. The value is the deepest option chosen.

Labeled levels

#labels gives each level a label of its own, rendered above its select. The default value is three levels down, so all three selects are drawn on load. The annex has a yard and no floors, so choosing it ends the row after two -- and the second select is still labeled Floor, because the labels go by depth and not by what the list holds.

Two levels, no empty first choice

#no_first_level_none drops the "- Please select -" option from the first select only, so a department is always chosen and the second select is there from the start.