diff --git a/src/components/FormItems/FormApiBasedAutoComplete.tsx b/src/components/FormItems/FormApiBasedAutoComplete.tsx index 8ec645c..9b2dd79 100644 --- a/src/components/FormItems/FormApiBasedAutoComplete.tsx +++ b/src/components/FormItems/FormApiBasedAutoComplete.tsx @@ -27,6 +27,7 @@ type FormEnterLocationsProps = { valueTemplateProps?: Array<{ [key: string]: "string" | "number" }>; groupBy?: string | string[]; groupFunction?: (item: any) => string; + valueFieldFunction?: (item: any) => any; selectField?: boolean; }; @@ -53,6 +54,7 @@ export const FormApiBasedAutoComplete = ({ valueTemplateProps, groupBy, groupFunction, + valueFieldFunction, selectField = false, }: FormEnterLocationsProps) => { const [data, setData] = useState([]); @@ -83,6 +85,18 @@ export const FormApiBasedAutoComplete = ({ return value; }; + const getPrimaryValue = (option: any) => { + if (valueFieldFunction) { + return valueFieldFunction(option); + } + + return valueField === "page" + ? getFaPermissions(option[valueField]) + : typeof valueField === "string" + ? option[valueField] + : getNestedValue(option, valueField); + }; + const { data: apiData } = useApiRequest({ api: api, method: "get", @@ -122,17 +136,7 @@ export const FormApiBasedAutoComplete = ({ : undefined, value: valueTemplate ? valueTemplate - .replace( - /v1/g, - formatValue( - valueField === "page" - ? getFaPermissions(option[valueField]) - : typeof valueField === "string" - ? option[valueField] - : getNestedValue(option, valueField), - "v1", - ), - ) + .replace(/v1/g, formatValue(getPrimaryValue(option), "v1")) .replace( /v2/g, formatValue( @@ -155,13 +159,7 @@ export const FormApiBasedAutoComplete = ({ "v3", ), ) - : `${ - valueField === "page" - ? getFaPermissions(option[valueField]) - : typeof valueField === "string" - ? option[valueField] - : getNestedValue(option, valueField) - } ${ + : `${getPrimaryValue(option)} ${ valueField2 ? " - " + (typeof valueField2 === "string"