Skip to content

Commit 9c95782

Browse files
author
Chris Nelson
committed
better details for events and docs
1 parent 8440183 commit 9c95782

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ The `items` attribute should contain a json string of option project. The `label
2222

2323
## Events
2424

25-
- `autocomplete-search` sent when the value of the input changes and is greater than the `minlength`, debounced by the specified interval.
26-
- `autocomplete-commit` sent when an item is selected either by pressing Enter or clicking an option
27-
- `autocomplete-close` sent when the element is open and loses focus, on by user pressing Escape
25+
- `autocomplete-search` sent when the value of the input changes and is greater than the `minlength`, debounced by the specified interval. It will contain a detail with the following properties:
26+
- `name` the value of the name attribute for the element. Useful if you have multiple autocompletes sending events to the same back end handler
27+
- `query` the value the user has typed into the search input
28+
- `autocomplete-commit` sent when an item is selected either by pressing Enter or clicking an option. It will contain a
29+
detail with the following properties:
30+
- `name` the value of the name attribute for the element. Useful if you have multiple autocompletes sending events to the same back end handler
31+
- `value` the value for the selected option
32+
- `autocomplete-close` sent when the element is open and loses focus, or by user pressing Escape
2833

2934
## Attributes
3035

3136
- `name` This is a required attribute for setting the correct FormData value. It works exactly the same way as the `name` attribute of any other form input.
32-
- `clear-list-on-select` If true, will cause the options list to have it's children removed when an `autocomplete-commit` event is about to be dispatched.
3337
- `debounce` The time in milliseconds to debounce before sending an `autocomplete-search` event when the user enters text into the input
3438
- `value` The value which will be initially used to populate the `FormData` of the associated form.
39+
- `display-value` this will appear when the element is in the closed state, with an icon next to it indicating
40+
the user can click to search for options
41+
- `min-length` the number of characters the user needs to type to trigger a search.
3542
- `searchValue` The value which will initially be used to populate the search input.
3643
- `open` The element will start in the Open mode display the text input
3744
- `label-property` The property of each item that will be used as the displayed label, defaults to `name`

src/autocomplete-input.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class AutocompleteInputElement extends LitElement {
207207
if (this.searchInput.value.length >= this.minLength) {
208208
this.elementInternals.states.add('searching');
209209
this.dispatchEvent(
210-
new CustomEvent('autocomplete-search', { detail: { query: this.searchInput.value } }));
210+
new CustomEvent('autocomplete-search', { detail: { query: this.searchInput.value, name: this.name } }));
211211
}
212212
}
213213

@@ -221,7 +221,7 @@ export class AutocompleteInputElement extends LitElement {
221221
if (this.clearListOnSelect) {
222222
this.items = [];
223223
}
224-
this.dispatchEvent(new CustomEvent('autocomplete-commit', { detail: target.dataset, bubbles: true }));
224+
this.dispatchEvent(new CustomEvent('autocomplete-commit', { detail: { name: this.name, ...target.dataset }, bubbles: true }));
225225
this.dispatchEvent(new Event('input', { bubbles: true }));
226226
}
227227

test/autocomplete-input.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ it('emits an autocomplete-search event', async () => {
2424
const el = await fixture(`
2525
<autocomplete-input name="foo" open debounce="10"></autocomplete-input>
2626
`);
27-
// el.click();
28-
// await el.updated;
2927
const searchInput = el.shadowRoot.querySelector('input');
30-
searchInput.value = 'foo';
28+
searchInput.value = 'bar';
3129
searchInput.dispatchEvent(new Event('input', { bubbles: true }));
3230
const { detail } = await oneEvent(el, 'autocomplete-search');
33-
expect(detail.query).to.equal('foo');
31+
expect(detail.query).to.equal('bar');
32+
expect(detail.name).to.equal('foo');
3433
});
3534

3635
it('only dispatches search event when the mininum length is met', async () => {
@@ -49,13 +48,14 @@ it('only dispatches search event when the mininum length is met', async () => {
4948
describe('the combobox', () => {
5049
it('builds a combobox and sends autocomplete-commit for a slotted list', async () => {
5150
const el = await fixture(`
52-
<autocomplete-input name="foo" open items='[{"value": "foo", "label": "Foo"}]'>
51+
<autocomplete-input name="bar" open items='[{"value": "foo", "label": "Foo"}]'>
5352
</autocomplete-input>
5453
`);
5554
const option = el.shadowRoot.querySelector('li[data-value="foo"]');
5655
el.addEventListener('autocomplete-commit', (e) => {
5756
console.debug(e.detail)
5857
expect(e.detail.value).to.equal('foo');
58+
expect(e.detail.name).to.equal('bar');
5959
});
6060
option.click();
6161
});

0 commit comments

Comments
 (0)