-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
开发了一个新的数据模型选择组件(ProjectSelect),它允许用户从个人和团队项目中选择一个数据模型。此组件已集成到数据查询页面,为用户提供了在不同数据模型之间切换的选项,增强了用户体验和数据查询效率。
- Loading branch information
www.zerocode.net.cn
committed
Sep 17, 2024
1 parent
e43286d
commit da35466
Showing
2 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { Select, message } from 'antd'; | ||
import { pageProject, recentProject } from "@/services/project"; | ||
|
||
const { Option, OptGroup } = Select; | ||
|
||
interface ProjectSelectProps { | ||
value?: string; | ||
onChange?: (value: string) => void; | ||
style?: React.CSSProperties; | ||
size?: 'small' | 'middle' | 'large'; | ||
} | ||
|
||
interface DataModel { | ||
id: string; | ||
projectName: string; | ||
type: string; | ||
} | ||
|
||
const ProjectSelect: React.FC<ProjectSelectProps> = ({ value, onChange, style, size = 'middle' }) => { | ||
const [dataModels, setDataModels] = useState<DataModel[]>([]); | ||
const [loading, setLoading] = useState(false); | ||
|
||
useEffect(() => { | ||
fetchDataModels(); | ||
}, []); | ||
|
||
const fetchDataModels = async (searchValue?: string) => { | ||
setLoading(true); | ||
try { | ||
const response = await recentProject({ | ||
page: 1, | ||
limit: 10, | ||
projectName: searchValue, | ||
order: "updateTime", | ||
}); | ||
if (response && response.data && response.data.records) { | ||
setDataModels(response.data.records); | ||
} | ||
} catch (error) { | ||
console.error("Error fetching data models:", error); | ||
message.error("Failed to fetch data models"); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
const handleSearch = (value: string) => { | ||
fetchDataModels(value); | ||
}; | ||
|
||
const groupedDataModels = dataModels.reduce((acc, model) => { | ||
const key = model.type === '1' ? 'personal' : 'team'; | ||
if (!acc[key]) { | ||
acc[key] = []; | ||
} | ||
acc[key].push(model); | ||
return acc; | ||
}, {} as { personal: DataModel[], team: DataModel[] }); | ||
|
||
return ( | ||
<Select | ||
value={value} | ||
onChange={onChange} | ||
style={style} | ||
size={size} | ||
placeholder="选择数据模型" | ||
showSearch | ||
filterOption={false} | ||
onSearch={handleSearch} | ||
loading={loading} | ||
> | ||
<OptGroup label="个人项目"> | ||
{groupedDataModels.personal?.map(model => ( | ||
<Option key={model.id} value={model.id}>{model.projectName}</Option> | ||
))} | ||
</OptGroup> | ||
<OptGroup label="团队项目"> | ||
{groupedDataModels.team?.map(model => ( | ||
<Option key={model.id} value={model.id}>{model.projectName}</Option> | ||
))} | ||
</OptGroup> | ||
</Select> | ||
); | ||
}; | ||
|
||
export default ProjectSelect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters