From f2f5d2a96c62db636cc56000672222cf70e8de07 Mon Sep 17 00:00:00 2001 From: Melloware Date: Mon, 6 Jan 2025 07:12:43 -0500 Subject: [PATCH] Fix #7522: Autocomplete allow selectedItemTemplate in both modes (#7564) --- components/doc/autocomplete/templatedoc.js | 31 ++++++++++++++++--- components/lib/autocomplete/AutoComplete.js | 6 ++-- components/lib/autocomplete/autocomplete.d.ts | 4 +-- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/components/doc/autocomplete/templatedoc.js b/components/doc/autocomplete/templatedoc.js index 2925e87826..dccb8307e0 100644 --- a/components/doc/autocomplete/templatedoc.js +++ b/components/doc/autocomplete/templatedoc.js @@ -35,6 +35,10 @@ export function TemplateDoc(props) { ); }; + const selectedItemTemplate = (item) => { + return `${item.name} (${item.code.toUpperCase()})`; + }; + const panelFooterTemplate = () => { const isCountrySelected = (filteredCountries || []).some((country) => country.name === selectedCountry); @@ -58,7 +62,7 @@ export function TemplateDoc(props) { const code = { basic: ` setSelectedCountry(e.value)} itemTemplate={itemTemplate} panelFooterTemplate={panelFooterTemplate} /> + completeMethod={search} onChange={(e) => setSelectedCountry(e.value)} itemTemplate={itemTemplate} panelFooterTemplate={panelFooterTemplate} selectedItemTemplate={selectedItemTemplate} /> `, javascript: ` import React, { useEffect, useState } from 'react'; @@ -117,6 +121,10 @@ export default function TemplateDemo() { ); }; + const selectedItemTemplate = (item) => { + return item.name + ' (' + item.code.toUpperCase() + ')'; + }; + useEffect(() => { CountryService.getCountries().then((data) => setCountries(data)); }, []); @@ -124,7 +132,7 @@ export default function TemplateDemo() { return (
setSelectedCountry(e.value)} itemTemplate={itemTemplate} panelFooterTemplate={panelFooterTemplate} /> + completeMethod={search} onChange={(e) => setSelectedCountry(e.value)} itemTemplate={itemTemplate} panelFooterTemplate={panelFooterTemplate} selectedItemTemplate={selectedItemTemplate} />
) } @@ -175,6 +183,10 @@ export default function TemplateDemo() { ); }; + + const selectedItemTemplate = (item: Country) => { + return item.name + ' (' + item.code.toUpperCase() + ')'; + }; const panelFooterTemplate = () => { const isCountrySelected = (filteredCountries || []).some( country => country['name'] === selectedCountry ); @@ -198,7 +210,7 @@ export default function TemplateDemo() { return (
setSelectedCountry(e.value)} itemTemplate={itemTemplate} panelFooterTemplate={panelFooterTemplate} /> + completeMethod={search} onChange={(e: AutoCompleteChangeEvent) => setSelectedCountry(e.value)} itemTemplate={itemTemplate} panelFooterTemplate={panelFooterTemplate} selectedItemTemplate={selectedItemTemplate} />
) } @@ -217,11 +229,20 @@ export default function TemplateDemo() {

Custom content can be displayed as an option using itemTemplate property that references a function with a suggestion option as a parameter and returns an element. Similarly selectedItemTemplate property is available - to customize the chips in multiple mode using the same approach. Note that selectedItemTemplate is only available in multiple mode at the moment. + to customize the chips in multiple mode and the text in single mode using the same approach.

- setSelectedCountry(e.value)} itemTemplate={itemTemplate} panelFooterTemplate={panelFooterTemplate} /> + setSelectedCountry(e.value)} + itemTemplate={itemTemplate} + panelFooterTemplate={panelFooterTemplate} + selectedItemTemplate={selectedItemTemplate} + />
diff --git a/components/lib/autocomplete/AutoComplete.js b/components/lib/autocomplete/AutoComplete.js index 63176353b1..82e4078a70 100644 --- a/components/lib/autocomplete/AutoComplete.js +++ b/components/lib/autocomplete/AutoComplete.js @@ -157,10 +157,10 @@ export const AutoComplete = React.memo( if (typeof value === 'string') return value; - const valueFromTemplate = ObjectUtils.getJSXElement(props.selectedItemTemplate, value); + if (props.selectedItemTemplate) { + const valueFromTemplate = ObjectUtils.getJSXElement(props.selectedItemTemplate, value); - if (typeof valueFromTemplate === 'string') { - return valueFromTemplate; + return props.multiple || typeof valueFromTemplate === 'string' ? valueFromTemplate : value; } if (props.field) { diff --git a/components/lib/autocomplete/autocomplete.d.ts b/components/lib/autocomplete/autocomplete.d.ts index c7981c5c59..03e970db20 100755 --- a/components/lib/autocomplete/autocomplete.d.ts +++ b/components/lib/autocomplete/autocomplete.d.ts @@ -404,9 +404,9 @@ export interface AutoCompleteProps extends Omit string | undefined | null); + selectedItemTemplate?: React.ReactNode | string | undefined | null | ((value: any) => React.ReactNode | string | undefined | null); /** * Whether to show the empty message or not. * @defaultValue false