Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lost input focus after remote search #827

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions public/assets/scripts/choices.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! choices.js v9.0.1 | © 2019 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/*! choices.js v9.0.1 | © 2020 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
Expand Down Expand Up @@ -1603,6 +1603,7 @@ function () {
this._addEventListeners();

this.input.enable();
this.input.element.focus();
this.containerOuter.enable();
}

Expand Down Expand Up @@ -3577,7 +3578,7 @@ function () {
};

Choices.prototype._generatePlaceholderValue = function () {
if (this._isSelectElement) {
if (this._isSelectElement && this.passedElement.placeholderOption) {
var placeholderOption = this.passedElement.placeholderOption;
return placeholderOption ? placeholderOption.text : null;
}
Expand Down
4 changes: 2 additions & 2 deletions public/assets/scripts/choices.min.js

Large diffs are not rendered by default.

46 changes: 18 additions & 28 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,6 @@ <h2>Multiple select input</h2>
</optgroup>
</select>

<p>
<small
>If the following example do not load, the Discogs rate limit has
probably been reached. Try again later!</small
>
</p>

<label for="choices-multiple-remote-fetch"
>Options from remote source (Fetch API) &amp; limited to 5</label
>
<select
class="form-control"
name="choices-multiple-remote-fetch"
id="choices-multiple-remote-fetch"
multiple
></select>

<label for="choices-multiple-rtl">Right-to-left</label>
<select
class="form-control"
Expand Down Expand Up @@ -618,18 +601,25 @@ <h2>Form interaction</h2>
placeholder: true,
placeholderValue: 'Pick an Strokes record',
maxItemCount: 5,
}).setChoices(function() {
return fetch(
'https://api.discogs.com/artists/55980/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW'
)
.then(function(response) {
return response.json();
})
.then(function(data) {
return data.releases.map(function(release) {
return { value: release.title, label: release.title };
});

var remoteSearchElement = document.querySelector(
'#choices-multiple-remote-fetch'
);
remoteSearchElement.addEventListener('search', function(event) {
multipleFetch.setChoices(function() {
return fetch(
'https://api.discogs.com/artists/55980/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW'
)
.then(function(response) {
return response.json();
})
.then(function(data) {
return data.releases.map(function(release) {
return { value: release.title, label: release.title };
});
});
});
});
});

var multipleCancelButton = new Choices(
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ class Choices {
if (this.containerOuter.isDisabled) {
this._addEventListeners();
this.input.enable();
this.input.element.focus();
this.containerOuter.enable();
}

Expand Down Expand Up @@ -627,7 +628,6 @@ class Choices {
if (typeof choicesArrayOrFetcher === 'function') {
// it's a choices fetcher function
const fetcher = choicesArrayOrFetcher(this);

if (typeof Promise === 'function' && fetcher instanceof Promise) {
// that's a promise
// eslint-disable-next-line compat/compat
Expand Down