2
2
import FieldBaseAutosuggest from ' ./FieldBaseAutosuggest.vue' ;
3
3
import debounce from ' debounce' ;
4
4
5
+ function setSuggestion (value ) {
6
+ const {name , label = null , identifier = null , ... extraItems} = value;
7
+ const suggestion = {
8
+ value: value,
9
+ label: label ?? name,
10
+ identifier: identifier,
11
+ ... (extraItems ? {extraItems: extraItems} : {}),
12
+ };
13
+ return suggestion;
14
+ }
15
+
5
16
export default {
6
17
name: ' FieldControlledVocab' ,
7
18
extends: FieldBaseAutosuggest,
@@ -15,7 +26,6 @@ export default {
15
26
data () {
16
27
return {
17
28
allSuggestions: [],
18
- suggestionsLoaded: false ,
19
29
suggestionsLoading: false ,
20
30
};
21
31
},
@@ -31,40 +41,35 @@ export default {
31
41
if (this .suggestionsLoading ) {
32
42
return ;
33
43
}
34
- if (! this .suggestionsLoaded ) {
35
- this .loadSuggestions (this .setSuggestions );
36
- }
44
+ this .loadSuggestions (this .setSuggestions );
37
45
this .setSuggestions ();
38
46
},
39
47
40
48
/**
41
49
* Load suggestions from the API
42
50
*/
43
- loadSuggestions (successCallback ) {
51
+ loadSuggestions: debounce ( function (successCallback ) {
44
52
this .suggestionsLoading = true ;
45
53
$ .ajax ({
46
54
url: this .apiUrl ,
47
55
type: ' GET' ,
48
56
context: this ,
49
- data: this .isMultilingual ? {locale: this .localeKey } : {},
57
+ data: {
58
+ term: this .inputValue ?? null ,
59
+ ... (this .isMultilingual ? {locale: this .localeKey } : {}),
60
+ },
50
61
error (r ) {
51
62
this .ajaxErrorCallback (r);
52
63
},
53
64
success (r ) {
54
- this .allSuggestions = r .map ((v ) => {
55
- return {
56
- value: v,
57
- label: v,
58
- };
59
- });
60
- this .suggestionsLoaded = true ;
65
+ this .allSuggestions = r .map ((v ) => setSuggestion (v));
61
66
this .suggestionsLoading = false ;
62
67
if (successCallback) {
63
68
successCallback .apply (this );
64
69
}
65
70
},
66
71
});
67
- },
72
+ }, 250 ),
68
73
69
74
/**
70
75
* Override the parent method to accept any typed
@@ -77,7 +82,7 @@ export default {
77
82
this .select (suggestion);
78
83
} else if (this .inputValue ) {
79
84
this .select ({
80
- value: this .inputValue ,
85
+ value: {name : this .inputValue } ,
81
86
label: this .inputValue ,
82
87
});
83
88
}
@@ -99,8 +104,8 @@ export default {
99
104
this .suggestions = this .allSuggestions .filter (
100
105
(suggestion ) =>
101
106
! this .inputValue ||
102
- (this .inputValue !== suggestion .value &&
103
- suggestion .value .match (regex)),
107
+ (this .inputValue !== suggestion .value . name &&
108
+ suggestion .value .name . match (regex)),
104
109
);
105
110
}, 250 ),
106
111
},
0 commit comments