From 5ef6c105aca5bd7c0c63ce3240fbe82c2e07e8f7 Mon Sep 17 00:00:00 2001
From: 1 <1@123>
Date: 星期三, 18 三月 2026 13:46:05 +0800
Subject: [PATCH] lsh#// rsf-server本机调用直通,不需要Token

---
 rsf-admin/src/page/components/TreeSelectInput.jsx |   78 +++++++++++++++++++--------------------
 1 files changed, 38 insertions(+), 40 deletions(-)

diff --git a/rsf-admin/src/page/components/TreeSelectInput.jsx b/rsf-admin/src/page/components/TreeSelectInput.jsx
index 4140b94..5e1bebb 100644
--- a/rsf-admin/src/page/components/TreeSelectInput.jsx
+++ b/rsf-admin/src/page/components/TreeSelectInput.jsx
@@ -1,20 +1,20 @@
 import React, { useState, useEffect } from 'react';
 import { useCreateContext, useTranslate, useRecordContext } from 'react-admin'
-import { MenuItem, Select, FormControl, InputLabel, Typography } from '@mui/material';
+import { Autocomplete, TextField, FormControl } from '@mui/material';
 import request from '@/utils/request';
 import * as Common from '@/utils/common';
 import { useFormContext } from 'react-hook-form';
 
-const TreeSelectInput = ({ resource, label, source = 'parentId', value, isTranslate = false, ...rest }) => {
+const TreeSelectInput = ({ resource, required, onChange, label, source = 'parentId', value, isTranslate = false, ...rest }) => {
     const translate = useTranslate();
-    const { setValue } = useFormContext();
+    const form = useFormContext();
     const [filter, setFilter] = React.useState("");
     const [treeData, setTreeData] = React.useState([]);
 
     const [proxyVal, setProxyVal] = React.useState('');
 
     const record = useRecordContext()
-    const val = value || record[source];
+    const val = value || record?.[source];
 
     useEffect(() => {
         const http = async (resource) => {
@@ -22,7 +22,9 @@
                 condition: filter
             });
             if (res?.data?.code === 200) {
-                setTreeData(Common.flattenTree(res.data.data));
+                // Add a null option at the beginning
+                const flatTree = Common.flattenTree(res.data.data);
+                setTreeData([{ id: 0, name: ' ' }, ...flatTree]);
                 setProxyVal(val);
             } else {
                 notify(res.data.msg);
@@ -31,49 +33,45 @@
         http(resource);
     }, [filter, value]);
 
-    const handleChange = (event) => {
-        const val = event.target.value;
-        setProxyVal(val);
-        setValue(source, val, {
+    const handleChange = (event, newValue) => {
+        const selectedVal = newValue ? newValue.id : 0;
+        setProxyVal(selectedVal);
+        form?.setValue(source, selectedVal, {
             shouldValidate: true,
             shouldDirty: true,
         });
+        if (onChange) {
+            onChange({ target: { value: selectedVal } })
+        }
     };
 
+    const validValueObj = treeData.find(item => item.id === (proxyVal || 0)) || null;
+
     return (
-        <FormControl fullWidth>
-            <InputLabel>{translate(label)}</InputLabel>
-            <Select
-                value={proxyVal || ''}
+        <FormControl fullWidth required={required} size="small">
+            <Autocomplete
+                value={validValueObj}
                 onChange={handleChange}
-                label={label}
-                MenuProps={{
-                    PaperProps: {
-                        style: {
-                            maxHeight: 300,
-                        },
-                    },
-                }}
+                options={treeData}
+                getOptionLabel={(option) => isTranslate && option.type === 0 ? translate(option.name) : option.name}
+                isOptionEqualToValue={(option, val) => option.id === val.id}
+                size="small"
+                ListboxProps={{ style: { maxHeight: '300px' } }}
+                renderOption={(props, option) => (
+                    <li {...props} style={{ paddingLeft: (option.depth || 0) * 16 + 16 }}>
+                        {isTranslate && option.type === 0 ? translate(option.name) : option.name}
+                    </li>
+                )}
+                renderInput={(params) => (
+                    <TextField
+                        {...params}
+                        label={translate(label)}
+                        variant="filled"
+                        required={required}
+                    />
+                )}
                 {...rest}
-            >
-                <MenuItem value={0}>
-                    &nbsp;
-                </MenuItem>
-                {treeData.map((node) => {
-                    return (
-                        <MenuItem
-                            key={node.id}
-                            value={node.id}
-                            style={{ paddingLeft: node.depth * 16 + 16 }}
-                        >
-                            {isTranslate && node.type === 0
-                                ? translate(node.name)
-                                : node.name
-                            }
-                        </MenuItem>
-                    )
-                })}
-            </Select>
+            />
         </FormControl>
     );
 };

--
Gitblit v1.9.1