Skip to content

Commit 9d3ae62

Browse files
committed
Clear search input when clicking "X" icon
Keep the same "close only" behaviour when pressing the Esc key and when clicking outside the listbox. But, when pressing the "X" icon within the search field, I think it better meets expectations if this also clears the input field. Comparison: * DocSearch.js v2 ([email protected]) does not do this. * DocSearch.js v3 ([email protected]) does do this. == npm run size == * Before: JS 2196 * After: JS 2209 (+13 B, +0.5%)
1 parent 5debc5c commit 9d3ae62

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div align="center">
44

55
[![Continuous integration](https://github.com/jquery/typesense-minibar/actions/workflows/CI.yaml/badge.svg)](https://github.com/jquery/typesense-minibar/actions/workflows/CI.yaml?query=event%3Apush+branch%3Amain)
6-
[![Test coverage](https://img.shields.io/badge/coverage-92%25-brightgreen.svg)](https://jquery.github.io/typesense-minibar/coverage/)
6+
[![Test coverage](https://img.shields.io/badge/coverage-93%25-brightgreen.svg)](https://jquery.github.io/typesense-minibar/coverage/)
77
[![Tested with QUnit](https://img.shields.io/badge/tested_with-qunit-9c3493.svg)](https://qunitjs.com/)
88

99
</div>

test/test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,27 @@ QUnit.module('typesense-minibar', hooks => {
302302
assert.true(listbox.hidden, 'listbox hidden');
303303
});
304304

305-
QUnit.test('listbox [close on click]', async assert => {
305+
QUnit.test('listbox [close button]', async assert => {
306+
const form = parseHTML('<form><input type="search"></form>');
307+
const input = form.firstChild;
308+
bar = tsminibar(form);
309+
const listbox = form.querySelector('[role=listbox]');
310+
311+
mockFetchResponse = API_RESP_FULL_MATCH_SOMETHING;
312+
input.value = 'something';
313+
await expectRender(form, () => {
314+
simulate(input, 'input');
315+
});
316+
assert.false(listbox.hidden, 'listbox not hidden');
317+
318+
mockFetchResponse = null;
319+
simulate(form.querySelector('svg.tsmb-icon-close'), 'click', { bubbles: true });
320+
321+
assert.true(listbox.hidden, 'listbox hidden');
322+
assert.equal(input.value, '', 'clear input value');
323+
});
324+
325+
QUnit.test('listbox [close on outside click]', async assert => {
306326
const form = parseHTML('<form><input type="search"></form>');
307327
const input = form.firstChild;
308328
bar = tsminibar(form);

typesense-minibar.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ globalThis.tsminibar = function tsminibar (form) {
7575
e.preventDefault(); // disable fallback
7676
});
7777
form.insertAdjacentHTML('beforeend', '<svg viewBox="0 0 12 12" width="20" height="20" aria-hidden="true" class="tsmb-icon-close" style="display: none;"><path d="M9 3L3 9M3 3L9 9"/></svg>');
78-
form.querySelector('.tsmb-icon-close').addEventListener('click', close);
78+
form.querySelector('.tsmb-icon-close').addEventListener('click', function () {
79+
input.value = '';
80+
input.focus();
81+
close();
82+
});
7983
connect();
8084

8185
function close () {

0 commit comments

Comments
 (0)