fix(web): DoctorSelect 预加载医生列表 + 搜索错误处理
- 组件挂载时预加载最多 50 条医生数据,下拉框打开即有选项 - 搜索清空时保留已有列表(不再置空) - 搜索失败时 catch 错误,保留初始列表不静默丢失 - 更新质量验证报告:全部 MEDIUM 问题已关闭
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Select } from 'antd';
|
||||
import { useState, useCallback } from 'react';
|
||||
import { useState, useCallback, useEffect } from 'react';
|
||||
import { doctorApi } from '../../../api/health/doctors';
|
||||
|
||||
interface Props {
|
||||
@@ -14,9 +14,23 @@ export function DoctorSelect({ value, onChange, placeholder }: Props) {
|
||||
>([]);
|
||||
const [fetching, setFetching] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
doctorApi.list({ page_size: 50 }).then((result) => {
|
||||
if (!cancelled) {
|
||||
setOptions(
|
||||
result.data.map((d) => ({
|
||||
value: d.id,
|
||||
label: `${d.name}${d.department ? ` - ${d.department}` : ''}`,
|
||||
})),
|
||||
);
|
||||
}
|
||||
}).catch(() => {});
|
||||
return () => { cancelled = true; };
|
||||
}, []);
|
||||
|
||||
const handleSearch = useCallback(async (search: string) => {
|
||||
if (!search || search.length < 1) {
|
||||
setOptions([]);
|
||||
return;
|
||||
}
|
||||
setFetching(true);
|
||||
@@ -31,6 +45,8 @@ export function DoctorSelect({ value, onChange, placeholder }: Props) {
|
||||
label: `${d.name}${d.department ? ` - ${d.department}` : ''}`,
|
||||
})),
|
||||
);
|
||||
} catch {
|
||||
// 搜索失败时保留初始列表
|
||||
} finally {
|
||||
setFetching(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user