8
8
import axios from "axios" ;
9
9
import _debounce from "lodash/debounce" ;
10
10
import _uniqBy from "lodash/uniqBy" ;
11
+ import _isEqual from "lodash/isEqual" ;
11
12
import PropTypes from "prop-types" ;
12
13
import queryString from "query-string" ;
13
14
import React , { Component } from "react" ;
@@ -44,7 +45,7 @@ export class RemoteSelectField extends Component {
44
45
this . cancellableAction && this . cancellableAction . cancel ( ) ;
45
46
}
46
47
47
- onSelectValue = ( event , { options, value, ...otherData } , callbackFunc ) => {
48
+ onSelectValue = async ( event , { options, value, ...otherData } , callbackFunc ) => {
48
49
const { multiple } = this . props ;
49
50
const newSelectedSuggestions = options . filter ( ( item ) => {
50
51
if ( multiple ) {
@@ -65,9 +66,10 @@ export class RemoteSelectField extends Component {
65
66
} ,
66
67
( ) => callbackFunc ( newSelectedSuggestions )
67
68
) ;
69
+ await this . searchIfNoSuggestions ( newSelectedSuggestions ) ; // Reset search query to empty string after selection
68
70
} ;
69
71
70
- handleAddition = ( e , { value } , callbackFunc ) => {
72
+ handleAddition = async ( e , { value } , callbackFunc ) => {
71
73
const { serializeAddedValue } = this . props ;
72
74
const { selectedSuggestions } = this . state ;
73
75
const selectedSuggestion = serializeAddedValue
@@ -83,9 +85,11 @@ export class RemoteSelectField extends Component {
83
85
[ ...prevState . suggestions , ...newSelectedSuggestions ] ,
84
86
"value"
85
87
) ,
88
+ searchQuery : null ,
86
89
} ) ,
87
90
( ) => callbackFunc ( newSelectedSuggestions )
88
91
) ;
92
+ await this . searchIfNoSuggestions ( newSelectedSuggestions ) ; // Reset search query to empty string after addition
89
93
} ;
90
94
91
95
onSearchChange = _debounce ( async ( e , { searchQuery } ) => {
@@ -99,7 +103,7 @@ export class RemoteSelectField extends Component {
99
103
const query = preSearchChange ( searchQuery ) ;
100
104
// If there is no query change, then display prevState suggestions
101
105
const { searchQuery : prevSearchQuery } = this . state ;
102
- if ( prevSearchQuery === searchQuery ) {
106
+ if ( prevSearchQuery === query ) {
103
107
return ;
104
108
}
105
109
this . setState ( { isFetching : true , searchQuery : query } ) ;
@@ -125,6 +129,14 @@ export class RemoteSelectField extends Component {
125
129
}
126
130
} ;
127
131
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
+
128
140
fetchSuggestions = async ( searchQuery ) => {
129
141
const {
130
142
suggestionAPIUrl,
0 commit comments