Skip to content

Commit f060a28

Browse files
committed
Merge bitzesty:select-behavior from alphagov#467
2 parents a79d8f7 + 38eedaf commit f060a28

14 files changed

+671
-107
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ This will:
367367

368368
- Place an autocomplete input field after the specified `<select>`
369369
- Default the autocomplete `autoselect` to `true`
370+
- Default the autocomplete `preserveNullOptions` to `true`
371+
- Default the autocomplete `showAllValues` to `true`
372+
- Default the autocomplete `confirmOnBlur` to `true`
370373
- Default the autocomplete `defaultValue` to the select's `option[selected]`
371374
- Default the autocomplete `id` to the `<select>`'s `id`
372375
- Default the autocomplete `name` attribute to `''` to prevent it being included in form submissions
@@ -377,7 +380,7 @@ This will:
377380

378381
This function takes the same options as `accessibleAutocomplete`, with the only difference being that it uses `selectElement` instead of `element`, which needs to be an instance of `HTMLSelectElement`.
379382

380-
> **Note**: The `accessibleAutocomplete.enhanceSelectElement` function is fairly light and wraps the public API for `accessibleAutocomplete`. If your use case doesn't fit the above defaults, try [reading the source](src/wrapper.js) and seeing if you can write your own.
383+
> **Note**: The `accessibleAutocomplete.enhanceSelectElement` function wraps the public API for `accessibleAutocomplete` to provide sensible defaults that mimic the behaviour expected from select dropdowns. It will also internally adjust the autocomplete to behave more like a select than an autocomplete. If your use case doesn't fit the above defaults, try [reading the source](src/wrapper.js) and seeing if you can write your own.
381384
382385
### Null options
383386

@@ -405,6 +408,32 @@ accessibleAutocomplete.enhanceSelectElement({
405408

406409
Any null options will also be filtered out of the options used to populate the `source` of the autocomplete element. To preserve options with no value in the autocomplete, then pass a `preserveNullOptions` flag of `true` to `enhanceSelectElement`.
407410

411+
412+
### Programmatic API
413+
414+
> **Note**: the programmatic API is still a work in progress. If a function you need programmatically is not available, feel free to raise an issue or send a Pull Request for it.
415+
416+
Autoselects normally react to the selection of the user automatically. However, when enhancing a select, it is often desirable to have some programmatic control over the selection.
417+
418+
The programmatic API is only exposed for enhanced selects, and contains the following functions:
419+
420+
##### `clearSelection()`
421+
422+
This function will clear the selection and trigger the `change` event on the select element being enhanced.
423+
424+
Example usage:
425+
426+
```js
427+
var select = document.querySelector(".to-be-enhanced");
428+
accessibleAutocomplete.enhanceSelectElement({
429+
selectElement: select
430+
});
431+
432+
select.accessibleAutocomplete.clearSelection();
433+
434+
```
435+
436+
408437
## Analytics and tracking
409438

410439
The following events get triggered on the input element during the life cycle of the autocomplete:

dist/accessible-autocomplete.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/accessible-autocomplete.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/accessible-autocomplete.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/accessible-autocomplete.preact.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/accessible-autocomplete.preact.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/accessible-autocomplete.react.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/accessible-autocomplete.react.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/index.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,38 @@ <h3>Progressive enhancement</h3>
128128
This example demonstrates how to turn a server-rendered <code>&lt;select&gt;</code> element
129129
into a autocomplete. Turn off JavaScript to see the <code>&lt;select&gt;</code> element.
130130
</p>
131+
<p>
132+
It should have sensible defaults and keyboard interactions that mimic the browser native select.
133+
</p>
131134
<p>Uses <code>accessibleAutocomplete.enhanceSelectElement</code>.</p>
132135
<label for="autocomplete-progressiveEnhancement">Country</label>
133136
<div class="autocomplete-wrapper">
134137
<select id="autocomplete-progressiveEnhancement">
138+
<option value="">Please select a country</option>
135139
<option value="fr">France</option>
136140
<option value="de">Germany</option>
137141
<option value="gb" selected>United Kingdom</option>
138142
</select>
139143
</div>
140144

145+
<h3>Programmatic API for enhanced select (Work in progress)</h3>
146+
<p>
147+
This example demonstrates how you can use the programmatic API to perform actions from code.
148+
</p>
149+
<p>Uses <code>accessibleAutocomplete.enhanceSelectElement</code>.</p>
150+
151+
<h5>clearSelection()</h5>
152+
<label for="autocomplete-progressiveEnhancement-api-clearselection">Select your country</label>
153+
<div class="autocomplete-wrapper">
154+
<select id="autocomplete-progressiveEnhancement-api-clearselection">
155+
<option value="">Please select a country</option>
156+
<option value="fr">France</option>
157+
<option value="de" selected>Germany</option>
158+
<option value="gb">United Kingdom</option>
159+
</select>
160+
<button class="clear-selection">Clear</button>
161+
</div>
162+
141163
<h3>Custom results</h3>
142164
<p>
143165
<p>This example demonstrates how to allow users to search for a country by either:</p>
@@ -486,6 +508,21 @@ <h3>Translating texts</h3>
486508
})
487509
</script>
488510

511+
<script type="text/javascript">
512+
let clearableSelect = element = document.querySelector('#autocomplete-progressiveEnhancement-api-clearselection')
513+
accessibleAutocomplete.enhanceSelectElement({
514+
selectElement: clearableSelect,
515+
placeholder: "Select a country"
516+
})
517+
518+
document.querySelector(".clear-selection").addEventListener("click", function(e) {
519+
e.preventDefault();
520+
e.stopPropagation();
521+
522+
clearableSelect.accessibleAutocomplete.clearSelection();
523+
})
524+
</script>
525+
489526
<script type="text/javascript">
490527
function customSuggest (query, syncResults) {
491528
var results = [

src/autocomplete.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
}
4040

4141
.autocomplete__dropdown-arrow-down{
42-
z-index: -1;
4342
display: inline-block;
4443
position: absolute;
4544
right: 8px;
4645
width: 24px;
4746
height: 24px;
4847
top: 10px;
48+
pointer-events: none;
4949
}
5050

5151
.autocomplete__menu {

0 commit comments

Comments
 (0)