@@ -177,26 +177,32 @@ export default class extends Controller {
177
177
}
178
178
179
179
#createAutocompleteWithHtmlContents( ) : TomSelect {
180
- const config = this . #mergeObjects( this . #getCommonConfig( ) , {
180
+ const commonConfig = this . #getCommonConfig( ) ;
181
+ const labelField = commonConfig . labelField ?? 'text' ;
182
+
183
+ const config = this . #mergeObjects( commonConfig , {
181
184
maxOptions : this . getMaxOptions ( ) ,
182
185
score : ( search : string ) => {
183
186
const scoringFunction = this . tomSelect . getScoreFunction ( search ) ;
184
187
return ( item : any ) => {
185
188
// strip HTML tags from each option's searchable text
186
- return scoringFunction ( { ...item , text : this . #stripTags( item . text ) } ) ;
189
+ return scoringFunction ( { ...item , text : this . #stripTags( item [ labelField ] ) } ) ;
187
190
} ;
188
191
} ,
189
192
render : {
190
- item : ( item : any ) => `<div>${ item . text } </div>` ,
191
- option : ( item : any ) => `<div>${ item . text } </div>` ,
193
+ item : ( item : any ) => `<div>${ item [ labelField ] } </div>` ,
194
+ option : ( item : any ) => `<div>${ item [ labelField ] } </div>` ,
192
195
} ,
193
196
} ) ;
194
197
195
198
return this . #createTomSelect( config ) ;
196
199
}
197
200
198
201
#createAutocompleteWithRemoteData( autocompleteEndpointUrl : string , minCharacterLength : number | null ) : TomSelect {
199
- const config : RecursivePartial < TomSettings > = this . #mergeObjects( this . #getCommonConfig( ) , {
202
+ const commonConfig = this . #getCommonConfig( ) ;
203
+ const labelField = commonConfig . labelField ?? 'text' ;
204
+
205
+ const config : RecursivePartial < TomSettings > = this . #mergeObjects( commonConfig , {
200
206
firstUrl : ( query : string ) => {
201
207
const separator = autocompleteEndpointUrl . includes ( '?' ) ? '&' : '?' ;
202
208
@@ -241,8 +247,8 @@ export default class extends Controller {
241
247
// avoid extra filtering after results are returned
242
248
score : ( search : string ) => ( item : any ) => 1 ,
243
249
render : {
244
- option : ( item : any ) => `<div>${ item . text } </div>` ,
245
- item : ( item : any ) => `<div>${ item . text } </div>` ,
250
+ option : ( item : any ) => `<div>${ item [ labelField ] } </div>` ,
251
+ item : ( item : any ) => `<div>${ item [ labelField ] } </div>` ,
246
252
loading_more : ( ) : string => {
247
253
return `<div class="loading-more-results">${ this . loadingMoreTextValue } </div>` ;
248
254
} ,
0 commit comments