Skip to content

Commit

Permalink
CDxSelectAutocomplete component will cancel the request and correct p…
Browse files Browse the repository at this point in the history
…roperly onBlur
  • Loading branch information
leandroradusky committed Jul 27, 2023
1 parent 50cdc80 commit 5da106f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions app/assets/javascripts/components/cdx_select_autocomplete.js.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var CdxSelectAutocomplete = React.createClass({
value={this.props.value || ""}
onChange={this.onChange}
onInputChange={this.props.combobox && this.copyValue}
onBlur= {this.onBlur}
/>);
},

Expand All @@ -50,11 +51,17 @@ var CdxSelectAutocomplete = React.createClass({
var url = this.props.url;
url += (url.includes("?") ? "&query=" : "?query=") + encodeURIComponent(query);

$.ajax({
this.cancelAjax();

this.xhr = $.ajax({
type: this.props.method || "GET",
url: url,

success: function (options) {
if (!this.selectRef.state.isFocused) {
this.selectRef.setState({ isLoading: false });
return;
}
if (prepareOptions) {
options = prepareOptions.call(null, options);
}
Expand All @@ -68,11 +75,18 @@ var CdxSelectAutocomplete = React.createClass({
}.bind(this),

error: function (_, error) {
callback(error);
if (error !== "abort") {
callback(error);
}
},
})
},

onBlur: function () {
this.selectRef.setState({ isLoading: false });
this.cancelAjax()
},

onChange: function (value, options) {
if (this.props.onSelect) {
this.props.onSelect.call(null, value, options);
Expand All @@ -81,5 +95,9 @@ var CdxSelectAutocomplete = React.createClass({

setSelectRef: function (ref) {
this.selectRef = ref;
},

cancelAjax: function () {
if (this.xhr) this.xhr.abort();
}
});

0 comments on commit 5da106f

Please sign in to comment.