Ver Fonte

支持默认值列表

visuddhinanda há 1 ano atrás
pai
commit
9bc64451ba

+ 25 - 7
dashboard-v4/dashboard/src/components/template/UserSelect.tsx

@@ -1,4 +1,4 @@
-import { ProFormSelect } from "@ant-design/pro-components";
+import { ProFormSelect, RequestOptionsType } from "@ant-design/pro-components";
 import { useIntl } from "react-intl";
 import { useIntl } from "react-intl";
 
 
 import { get } from "../../request";
 import { get } from "../../request";
@@ -9,24 +9,38 @@ interface IWidget {
   width?: number | "md" | "sm" | "xl" | "xs" | "lg";
   width?: number | "md" | "sm" | "xl" | "xs" | "lg";
   multiple?: boolean;
   multiple?: boolean;
   hidden?: boolean;
   hidden?: boolean;
+  hiddenTitle?: boolean;
+  required?: boolean;
+  initialValue?: string | string[] | null;
+  options?: RequestOptionsType[];
 }
 }
 const UserSelectWidget = ({
 const UserSelectWidget = ({
   name = "user",
   name = "user",
   multiple = false,
   multiple = false,
   width = "md",
   width = "md",
   hidden = false,
   hidden = false,
+  hiddenTitle = false,
+  required = true,
+  options = [],
+  initialValue,
 }: IWidget) => {
 }: IWidget) => {
   const intl = useIntl();
   const intl = useIntl();
+  console.log("UserSelect options", options);
   return (
   return (
     <ProFormSelect
     <ProFormSelect
       name={name}
       name={name}
-      label={intl.formatMessage({ id: "forms.fields.user.label" })}
+      label={
+        hiddenTitle
+          ? undefined
+          : intl.formatMessage({ id: "forms.fields.user.label" })
+      }
       hidden={hidden}
       hidden={hidden}
       width={width}
       width={width}
+      initialValue={initialValue}
       showSearch
       showSearch
       debounceTime={300}
       debounceTime={300}
       fieldProps={{
       fieldProps={{
-        mode: multiple ? "multiple" : undefined,
+        mode: multiple ? "tags" : undefined,
       }}
       }}
       request={async ({ keyWords }) => {
       request={async ({ keyWords }) => {
         console.log("keyWord", keyWords);
         console.log("keyWord", keyWords);
@@ -35,21 +49,25 @@ const UserSelectWidget = ({
           const json = await get<IUserListResponse>(
           const json = await get<IUserListResponse>(
             `/v2/user?view=key&key=${keyWords}`
             `/v2/user?view=key&key=${keyWords}`
           );
           );
-          const userList = json.data.rows.map((item) => {
+          console.info("api response user select", json);
+          const userList: RequestOptionsType[] = json.data.rows.map((item) => {
             return {
             return {
               value: item.id,
               value: item.id,
-              label: `${item.userName}-${item.nickName}`,
+              label: `${item.nickName}`,
             };
             };
           });
           });
           console.log("json", userList);
           console.log("json", userList);
           return userList;
           return userList;
         } else {
         } else {
-          return [];
+          const defaultOptions: RequestOptionsType[] = options.map((item) => {
+            return { label: item.label, value: item.value?.toString() };
+          });
+          return defaultOptions;
         }
         }
       }}
       }}
       rules={[
       rules={[
         {
         {
-          required: true,
+          required: required,
         },
         },
       ]}
       ]}
     />
     />