Skip to content

Commit

Permalink
Fix autocomplete to support callback as described in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin White committed Nov 16, 2023
1 parent 4c57057 commit fba3f3f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/app/components/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit fba3f3f

Please sign in to comment.