From fba3f3ff714b245f8995881043765a90a09354bc Mon Sep 17 00:00:00 2001 From: Justin White Date: Thu, 16 Nov 2023 11:40:19 -0600 Subject: [PATCH] Fix autocomplete to support callback as described in docs --- src/app/components/autocomplete/autocomplete.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/app/components/autocomplete/autocomplete.ts b/src/app/components/autocomplete/autocomplete.ts index d981d4cb3e4..90d084d4327 100755 --- a/src/app/components/autocomplete/autocomplete.ts +++ b/src/app/components/autocomplete/autocomplete.ts @@ -47,6 +47,9 @@ export const AUTOCOMPLETE_VALUE_ACCESSOR: any = { useExisting: forwardRef(() => AutoComplete), multi: true }; + +declare type OptionLabelCallback = (option: any) => string; + /** * AutoComplete is an input component that provides real-time suggestions when being typed. * @group Components @@ -555,7 +558,7 @@ export class AutoComplete implements AfterViewChecked, AfterContentInit, OnDestr * Property name or getter function to use as the label of an option. * @group Props */ - @Input() optionLabel: string | undefined; + @Input() optionLabel: string | OptionLabelCallback | undefined; /** * Unique identifier of the component. * @group Props @@ -1533,7 +1536,12 @@ export class AutoComplete implements AfterViewChecked, AfterContentInit, OnDestr } getOptionLabel(option: any) { - return this.field || this.optionLabel ? ObjectUtils.resolveFieldData(option, this.field || this.optionLabel) : option && option.label != undefined ? option.label : option; + if (this.field || typeof this.optionLabel === 'string' || this.optionLabel instanceof String) { + return ObjectUtils.resolveFieldData(option, this.field || this.optionLabel); + } else if (this.optionLabel) { + return this.optionLabel(option); + } + return option && option.label != undefined ? option.label : option; } getOptionValue(option) {