Skip to content

Commit 52e9351

Browse files
sakshamarora1ntarocco
authored andcommitted
RemoteSelectField: Show default search results after selection/addition
1 parent 489827c commit 52e9351

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/lib/forms/RemoteSelectField.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import axios from "axios";
99
import _debounce from "lodash/debounce";
1010
import _uniqBy from "lodash/uniqBy";
11+
import _isEqual from "lodash/isEqual";
1112
import PropTypes from "prop-types";
1213
import queryString from "query-string";
1314
import React, { Component } from "react";
@@ -44,7 +45,7 @@ export class RemoteSelectField extends Component {
4445
this.cancellableAction && this.cancellableAction.cancel();
4546
}
4647

47-
onSelectValue = (event, { options, value, ...otherData }, callbackFunc) => {
48+
onSelectValue = async (event, { options, value, ...otherData }, callbackFunc) => {
4849
const { multiple } = this.props;
4950
const newSelectedSuggestions = options.filter((item) => {
5051
if (multiple) {
@@ -65,9 +66,10 @@ export class RemoteSelectField extends Component {
6566
},
6667
() => callbackFunc(newSelectedSuggestions)
6768
);
69+
await this.searchIfNoSuggestions(newSelectedSuggestions); // Reset search query to empty string after selection
6870
};
6971

70-
handleAddition = (e, { value }, callbackFunc) => {
72+
handleAddition = async (e, { value }, callbackFunc) => {
7173
const { serializeAddedValue } = this.props;
7274
const { selectedSuggestions } = this.state;
7375
const selectedSuggestion = serializeAddedValue
@@ -83,9 +85,11 @@ export class RemoteSelectField extends Component {
8385
[...prevState.suggestions, ...newSelectedSuggestions],
8486
"value"
8587
),
88+
searchQuery: null,
8689
}),
8790
() => callbackFunc(newSelectedSuggestions)
8891
);
92+
await this.searchIfNoSuggestions(newSelectedSuggestions); // Reset search query to empty string after addition
8993
};
9094

9195
onSearchChange = _debounce(async (e, { searchQuery }) => {
@@ -99,7 +103,7 @@ export class RemoteSelectField extends Component {
99103
const query = preSearchChange(searchQuery);
100104
// If there is no query change, then display prevState suggestions
101105
const { searchQuery: prevSearchQuery } = this.state;
102-
if (prevSearchQuery === searchQuery) {
106+
if (prevSearchQuery === query) {
103107
return;
104108
}
105109
this.setState({ isFetching: true, searchQuery: query });
@@ -125,6 +129,14 @@ export class RemoteSelectField extends Component {
125129
}
126130
};
127131

132+
searchIfNoSuggestions = async (newSelectedSuggestions) => {
133+
// If all the suggestions from the search query are selected, fetch all suggestions via API
134+
const { suggestions } = this.state;
135+
if (_isEqual(newSelectedSuggestions, suggestions)) {
136+
await this.executeSearch("");
137+
}
138+
};
139+
128140
fetchSuggestions = async (searchQuery) => {
129141
const {
130142
suggestionAPIUrl,

0 commit comments

Comments
 (0)