feat: value field function
This commit is contained in:
@@ -27,6 +27,7 @@ type FormEnterLocationsProps = {
|
|||||||
valueTemplateProps?: Array<{ [key: string]: "string" | "number" }>;
|
valueTemplateProps?: Array<{ [key: string]: "string" | "number" }>;
|
||||||
groupBy?: string | string[];
|
groupBy?: string | string[];
|
||||||
groupFunction?: (item: any) => string;
|
groupFunction?: (item: any) => string;
|
||||||
|
valueFieldFunction?: (item: any) => any;
|
||||||
selectField?: boolean;
|
selectField?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -53,6 +54,7 @@ export const FormApiBasedAutoComplete = ({
|
|||||||
valueTemplateProps,
|
valueTemplateProps,
|
||||||
groupBy,
|
groupBy,
|
||||||
groupFunction,
|
groupFunction,
|
||||||
|
valueFieldFunction,
|
||||||
selectField = false,
|
selectField = false,
|
||||||
}: FormEnterLocationsProps) => {
|
}: FormEnterLocationsProps) => {
|
||||||
const [data, setData] = useState<any>([]);
|
const [data, setData] = useState<any>([]);
|
||||||
@@ -83,6 +85,18 @@ export const FormApiBasedAutoComplete = ({
|
|||||||
return value;
|
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({
|
const { data: apiData } = useApiRequest({
|
||||||
api: api,
|
api: api,
|
||||||
method: "get",
|
method: "get",
|
||||||
@@ -122,17 +136,7 @@ export const FormApiBasedAutoComplete = ({
|
|||||||
: undefined,
|
: undefined,
|
||||||
value: valueTemplate
|
value: valueTemplate
|
||||||
? valueTemplate
|
? valueTemplate
|
||||||
.replace(
|
.replace(/v1/g, formatValue(getPrimaryValue(option), "v1"))
|
||||||
/v1/g,
|
|
||||||
formatValue(
|
|
||||||
valueField === "page"
|
|
||||||
? getFaPermissions(option[valueField])
|
|
||||||
: typeof valueField === "string"
|
|
||||||
? option[valueField]
|
|
||||||
: getNestedValue(option, valueField),
|
|
||||||
"v1",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.replace(
|
.replace(
|
||||||
/v2/g,
|
/v2/g,
|
||||||
formatValue(
|
formatValue(
|
||||||
@@ -155,13 +159,7 @@ export const FormApiBasedAutoComplete = ({
|
|||||||
"v3",
|
"v3",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
: `${
|
: `${getPrimaryValue(option)} ${
|
||||||
valueField === "page"
|
|
||||||
? getFaPermissions(option[valueField])
|
|
||||||
: typeof valueField === "string"
|
|
||||||
? option[valueField]
|
|
||||||
: getNestedValue(option, valueField)
|
|
||||||
} ${
|
|
||||||
valueField2
|
valueField2
|
||||||
? " - " +
|
? " - " +
|
||||||
(typeof valueField2 === "string"
|
(typeof valueField2 === "string"
|
||||||
|
|||||||
Reference in New Issue
Block a user