diff --git a/src/Autocomplete/CHANGELOG.md b/src/Autocomplete/CHANGELOG.md index 1237b13dddd..d7384fee5f9 100644 --- a/src/Autocomplete/CHANGELOG.md +++ b/src/Autocomplete/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 2.22.0 + +- Take `labelField` TomSelect option into account #2382 + ## 2.21.0 - Translate the `option_create` option from TomSelect with remote data setup #2279 diff --git a/src/Autocomplete/assets/dist/controller.js b/src/Autocomplete/assets/dist/controller.js index 7d9d148dd26..3b905793551 100644 --- a/src/Autocomplete/assets/dist/controller.js +++ b/src/Autocomplete/assets/dist/controller.js @@ -261,22 +261,26 @@ _default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _def }); return __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createTomSelect).call(this, config); }, _default_1_createAutocompleteWithHtmlContents = function _default_1_createAutocompleteWithHtmlContents() { - const config = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_mergeObjects).call(this, __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_getCommonConfig).call(this), { + const commonConfig = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_getCommonConfig).call(this); + const labelField = commonConfig.labelField ?? 'text'; + const config = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_mergeObjects).call(this, commonConfig, { maxOptions: this.getMaxOptions(), score: (search) => { const scoringFunction = this.tomSelect.getScoreFunction(search); return (item) => { - return scoringFunction({ ...item, text: __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_stripTags).call(this, item.text) }); + return scoringFunction({ ...item, text: __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_stripTags).call(this, item[labelField]) }); }; }, render: { - item: (item) => `
${item.text}
`, - option: (item) => `
${item.text}
`, + item: (item) => `
${item[labelField]}
`, + option: (item) => `
${item[labelField]}
`, }, }); return __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createTomSelect).call(this, config); }, _default_1_createAutocompleteWithRemoteData = function _default_1_createAutocompleteWithRemoteData(autocompleteEndpointUrl, minCharacterLength) { - const config = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_mergeObjects).call(this, __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_getCommonConfig).call(this), { + const commonConfig = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_getCommonConfig).call(this); + const labelField = commonConfig.labelField ?? 'text'; + const config = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_mergeObjects).call(this, commonConfig, { firstUrl: (query) => { const separator = autocompleteEndpointUrl.includes('?') ? '&' : '?'; return `${autocompleteEndpointUrl}${separator}query=${encodeURIComponent(query)}`; @@ -306,8 +310,8 @@ _default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _def optgroupField: 'group_by', score: (search) => (item) => 1, render: { - option: (item) => `
${item.text}
`, - item: (item) => `
${item.text}
`, + option: (item) => `
${item[labelField]}
`, + item: (item) => `
${item[labelField]}
`, loading_more: () => { return `
${this.loadingMoreTextValue}
`; }, diff --git a/src/Autocomplete/assets/src/controller.ts b/src/Autocomplete/assets/src/controller.ts index 62fe734ecb1..b9370c9919e 100644 --- a/src/Autocomplete/assets/src/controller.ts +++ b/src/Autocomplete/assets/src/controller.ts @@ -177,18 +177,21 @@ export default class extends Controller { } #createAutocompleteWithHtmlContents(): TomSelect { - const config = this.#mergeObjects(this.#getCommonConfig(), { + const commonConfig = this.#getCommonConfig(); + const labelField = commonConfig.labelField ?? 'text'; + + const config = this.#mergeObjects(commonConfig, { maxOptions: this.getMaxOptions(), score: (search: string) => { const scoringFunction = this.tomSelect.getScoreFunction(search); return (item: any) => { // strip HTML tags from each option's searchable text - return scoringFunction({ ...item, text: this.#stripTags(item.text) }); + return scoringFunction({ ...item, text: this.#stripTags(item[labelField]) }); }; }, render: { - item: (item: any) => `
${item.text}
`, - option: (item: any) => `
${item.text}
`, + item: (item: any) => `
${item[labelField]}
`, + option: (item: any) => `
${item[labelField]}
`, }, }); @@ -196,7 +199,10 @@ export default class extends Controller { } #createAutocompleteWithRemoteData(autocompleteEndpointUrl: string, minCharacterLength: number | null): TomSelect { - const config: RecursivePartial = this.#mergeObjects(this.#getCommonConfig(), { + const commonConfig = this.#getCommonConfig(); + const labelField = commonConfig.labelField ?? 'text'; + + const config: RecursivePartial = this.#mergeObjects(commonConfig, { firstUrl: (query: string) => { const separator = autocompleteEndpointUrl.includes('?') ? '&' : '?'; @@ -241,8 +247,8 @@ export default class extends Controller { // avoid extra filtering after results are returned score: (search: string) => (item: any) => 1, render: { - option: (item: any) => `
${item.text}
`, - item: (item: any) => `
${item.text}
`, + option: (item: any) => `
${item[labelField]}
`, + item: (item: any) => `
${item[labelField]}
`, loading_more: (): string => { return `
${this.loadingMoreTextValue}
`; },