import React, { useState, useEffect } from 'react';
import { Select, Spin, message } from 'antd';
import debounce from 'lodash/debounce';
import { getCustomerListByPage } from 'service';
const { Option } = Select;
interface OptionType {
value: string;
customerName: string;
id: string;
}
interface ApiResponse {
items: OptionType[];
hasMore: boolean;
}
const SelectByPage: React.FC = () => {
const [data, setData] = useState<OptionType[]>([]);
const [value, setValue] = useState<string | undefined>(undefined);
const [loading, setLoading] = useState<boolean>(false);
const [page, setPage] = useState<number>(1);
const [hasMore, setHasMore] = useState<boolean>(true);
const fetchOptions = async (
searchValue: string,
pageNum: number
): Promise<void> => {
console.log(556546, pageNum);
setLoading(true);
try {
const {
data: { records },
} = await getCustomerListByPage({
size: 10,
current: pageNum,
businessRelations: '02',
warehouseIds: '',
customerId: '',
});
if (pageNum === 1) {
setData(records);
} else {
setData((prev) => [...prev, ...records]);
}
setHasMore(records.length === 10);
} catch (error) {
message.error(`Failed to fetch options: ${(error as Error).message}`);
} finally {
setLoading(false);
}
};
const handleSearch = debounce((value: string) => {
setPage(1);
fetchOptions(value, 1);
}, 300);
const handleScroll = (e: React.UIEvent<HTMLDivElement, UIEvent>): void => {
const { target } = e;
if (
(target as HTMLDivElement).scrollTop +
(target as HTMLDivElement).clientHeight >=
(target as HTMLDivElement).scrollHeight - 8
) {
if (hasMore && !loading) {
setPage((prev) => {
const nextPage = prev + 1;
fetchOptions(value || '', nextPage);
return nextPage;
});
}
}
};
useEffect(() => {
fetchOptions('', 1);
}, []);
return (
<Select
showSearch
value={value}
placeholder="选择"
onSearch={handleSearch}
onChange={setValue}
onPopupScroll={handleScroll}
notFoundContent={loading ? <Spin size="small" /> : null}
loading={loading}
filterOption={false} // 关闭内置的过滤功能
>
{data.map((item: OptionType) => (
<Option key={item.id} value={item.id}>
{item.customerName}
</Option>
))}
</Select>
);
};
export default SelectByPage;