Skip to content

Commit

Permalink
fix(gv-autocomplete): remove overlap when has several autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
gcusnieux committed Feb 26, 2021
1 parent f234882 commit d8b9849
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/atoms/gv-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const ESCAPE_KEY_CODE = 27;
*
* @fires gv-autocomplete:search - Custom event when user search value
* @fires gv-autocomplete:select - Custom event when user select value
* @fires gv-autocomplete:opened - Custom event after opened autocomplete
*
* @slot style - The options style
* @slot input - The input to wrap
Expand Down Expand Up @@ -62,7 +63,7 @@ export class GvAutocomplete extends LitElement {
filter: { type: Boolean | Function },
size: { type: Number },
_options: { type: Array, attribute: false },
_forceOpen: { type: Boolean },
_forceOpen: { type: Boolean, attribute: false },
};
}

Expand Down Expand Up @@ -156,6 +157,7 @@ export class GvAutocomplete extends LitElement {
this.style = '';
this.filter = false;
this.size = 5;
this._handleOpened = this._onOpened.bind(this);
}

set options(options) {
Expand Down Expand Up @@ -192,7 +194,7 @@ export class GvAutocomplete extends LitElement {
this.value = event.target.value;
if (this.value != null && this.value.trim().length >= this.minChars) {
this._cancellableTimeout = setTimeout(() => {
this._forceOpen = true;
this._open();
dispatchCustomEvent(this, 'search', this.value);
}, 200);
} else {
Expand All @@ -207,7 +209,7 @@ export class GvAutocomplete extends LitElement {
});
}
this.value = this._getInput().value = value;
this._forceOpen = false;
this._close();
dispatchCustomEvent(this, 'select', option);
}

Expand Down Expand Up @@ -324,7 +326,7 @@ export class GvAutocomplete extends LitElement {
this._onSelect(candidate.getAttribute('data-value'));
this._updateHover();
}
this._forceOpen = false;
this._close();
break;
}

Expand Down Expand Up @@ -362,13 +364,23 @@ export class GvAutocomplete extends LitElement {
}
}

_onFocus() {
_open() {
this._forceOpen = true;
dispatchCustomEvent(this, 'opened', { target: this });
}

_close() {
this._forceOpen = false;
}

_onFocus() {
this._open();
}

_onBlur() {
// Important when using custom HTML in options
if (this._shouldSelect === false) {
this._forceOpen = false;
this._close();
}
}

Expand Down Expand Up @@ -416,6 +428,17 @@ export class GvAutocomplete extends LitElement {
}
}

_onOpened({ detail }) {
if (detail.target !== this) {
this._close();
}
}

connectedCallback() {
super.connectedCallback();
window.addEventListener('gv-autocomplete:opened', this._handleOpened);
}

disconnectedCallback() {
if (this._handlers) {
this.shadowRoot.removeEventListener('input', this._handlers.input);
Expand All @@ -425,6 +448,7 @@ export class GvAutocomplete extends LitElement {
input.removeEventListener('blur', this._handlers.blur);
input.removeEventListener('gv-input:clear', this._handlers.clear);
}
window.removeEventListener('gv-autocomplete:open', this._handleOpened);
super.disconnectedCallback();
}
}
Expand Down
2 changes: 2 additions & 0 deletions stories/atoms/gv-autocomplete.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const conf = {
css: `
:host {
height: 175px;
display: flex;
flex-direction: column;
}
gv-autocomplete {
width: 350px;
Expand Down
2 changes: 2 additions & 0 deletions stories/organisms/gv-schema-form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('S C H E M A F O R M', () => {
'body-with-el',
'path-operator',
'resources',
'another-list-resources',
'attributes',
'timeToLiveSeconds',
'useResponseCacheHeaders',
Expand Down Expand Up @@ -116,6 +117,7 @@ describe('S C H E M A F O R M', () => {
'body-with-el',
'path-operator',
'resources',
'another-list-resources',
'attributes',
'timeToLiveSeconds',
'useResponseCacheHeaders',
Expand Down
10 changes: 10 additions & 0 deletions stories/resources/schemas/mixed.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@
}
}
},
"another-list-resources": {
"title": "Async list of resource",
"description": "Special field that's dispatch an event after add in DOM, usefull for async load",
"type": "string",
"x-schema-form": {
"event": {
"name": "fetch-data"
}
}
},
"attributes": {
"type": "array",
"title": "Assign context attributes",
Expand Down

0 comments on commit d8b9849

Please sign in to comment.