|
| 1 | +import type { ResponseList } from "&src/types/api"; |
| 2 | +import type { SelectProps } from "antd"; |
| 3 | +import type { AnyObject, PagingSwrProps } from "use-swr-data"; |
| 4 | + |
| 5 | +import { Select, Spin } from "antd"; |
| 6 | +import debounce from "lodash/debounce"; |
| 7 | +import { useEffect, useMemo, useState } from "react"; |
| 8 | +import useSwrData from "use-swr-data"; |
| 9 | + |
| 10 | +interface LazyLoadSelectProps<P extends AnyObject, R extends ResponseList<any>> { |
| 11 | + value?: SelectProps["value"]; |
| 12 | + searchKey: string; |
| 13 | + reqFunc: PagingSwrProps<R, P>["req"]; |
| 14 | + fieldNames?: SelectProps["fieldNames"]; |
| 15 | + reqParams?: PagingSwrProps<R, P>["params"]; |
| 16 | + onChange?: SelectProps["onChange"]; |
| 17 | +} |
| 18 | + |
| 19 | +export default function LazyLoadSelect<P extends AnyObject, R extends ResponseList<any>>(props: LazyLoadSelectProps<P, R>) { |
| 20 | + const { value, fieldNames, searchKey = "name", reqParams, reqFunc, onChange } = props; |
| 21 | + |
| 22 | + const [list, setList] = useState<any[]>([]); |
| 23 | + |
| 24 | + const { data, pageInfo, onSearch, setPage } = useSwrData({ |
| 25 | + reqKey: reqFunc.name, |
| 26 | + req: reqFunc, |
| 27 | + params: reqParams, |
| 28 | + paging: true, |
| 29 | + defaultPage: { pageNum: 1, pageSize: 100 }, |
| 30 | + swrConfig: { |
| 31 | + revalidateOnFocus: false, |
| 32 | + revalidateIfStale: true, |
| 33 | + revalidateOnMount: true, |
| 34 | + dedupingInterval: 0, |
| 35 | + }, |
| 36 | + }); |
| 37 | + useEffect(() => { |
| 38 | + if (!data) |
| 39 | + return; |
| 40 | + |
| 41 | + if (data?.pageNum > 1) { |
| 42 | + setList(draft => [...draft, ...data.list]); |
| 43 | + } |
| 44 | + else { |
| 45 | + setList(data.list); |
| 46 | + } |
| 47 | + }, [data]); |
| 48 | + |
| 49 | + const displayList = useMemo(() => { |
| 50 | + const newList = [...list]; |
| 51 | + |
| 52 | + if (pageInfo.pageNum === data?.totalPage) |
| 53 | + return newList; |
| 54 | + |
| 55 | + newList.push({ |
| 56 | + title: "loading", |
| 57 | + label: ( |
| 58 | + <div className="flex justify-center"> |
| 59 | + <Spin /> |
| 60 | + </div> |
| 61 | + ), |
| 62 | + value: "loading", |
| 63 | + }); |
| 64 | + |
| 65 | + return newList; |
| 66 | + }, [list, data, pageInfo]); |
| 67 | + |
| 68 | + const onSearchData = debounce((value) => { |
| 69 | + onSearch({ [searchKey]: value } as PagingSwrProps["defaultSearch"]); |
| 70 | + setList([]); |
| 71 | + }, 500); |
| 72 | + const onloadData = debounce(() => { |
| 73 | + setPage(draft => ({ ...draft, pageNum: pageInfo.pageNum + 1 })); |
| 74 | + }, 200); |
| 75 | + |
| 76 | + return ( |
| 77 | + <Select |
| 78 | + showSearch |
| 79 | + allowClear |
| 80 | + value={value} |
| 81 | + filterOption={false} |
| 82 | + options={displayList} |
| 83 | + fieldNames={fieldNames} |
| 84 | + onSearch={onSearchData} |
| 85 | + onPopupScroll={(e) => { |
| 86 | + const container = e.target as HTMLDivElement; |
| 87 | + const scrollTop = container?.scrollTop; |
| 88 | + const containerHeight = (container.firstChild as HTMLDivElement)?.clientHeight; |
| 89 | + |
| 90 | + if (data && containerHeight - scrollTop < 300 && data.totalPage > pageInfo.pageNum) |
| 91 | + onloadData(); |
| 92 | + }} |
| 93 | + onChange={(value) => { |
| 94 | + onSearch({ [searchKey]: "" } as PagingSwrProps["defaultSearch"]); |
| 95 | + onChange?.(value); |
| 96 | + }} |
| 97 | + /> |
| 98 | + ); |
| 99 | +} |
0 commit comments