11import type { ResponseList } from "&src/types/api" ;
2- import type { AnyObject , PagingSwrProps } from "@wanp/use-swr-data" ;
2+ import type { PagingSwrProps } from "@wanp/use-swr-data" ;
33import type { SelectProps } from "antd" ;
4-
54import useSwrData from "@wanp/use-swr-data" ;
65import { Select , Spin } from "antd" ;
76import debounce from "lodash/debounce" ;
87import { useEffect , useMemo , useState } from "react" ;
98
10- interface LazyLoadSelectProps < P extends AnyObject , R extends ResponseList < any > > {
11- value ?: SelectProps [ "value" ] ;
12- searchKey : string ;
9+ interface LazyLoadSelectProps < P extends AnyObject , R extends ResponseList < any > > extends Pick < SelectProps , "fieldNames" | "onChange" | "value" | "disabled" > {
10+ reqKey : string ;
1311 reqFunc : PagingSwrProps < R , P > [ "req" ] ;
14- fieldNames ?: SelectProps [ "fieldNames" ] ;
1512 reqParams ?: PagingSwrProps < R , P > [ "params" ] ;
16- onChange ?: SelectProps [ "onChange" ] ;
13+ searchKey ?: string ;
1714}
1815
1916export 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-
17+ const { reqKey, reqFunc, reqParams, searchKey, onChange, ...reset } = props ;
2218 const [ list , setList ] = useState < any [ ] > ( [ ] ) ;
2319
2420 const { data, pageInfo, onSearch, setPage } = useSwrData ( {
25- reqKey : reqFunc . name ,
21+ reqKey,
2622 req : reqFunc ,
2723 params : reqParams ,
2824 paging : true ,
@@ -66,6 +62,9 @@ export default function LazyLoadSelect<P extends AnyObject, R extends ResponseLi
6662 } , [ list , data , pageInfo ] ) ;
6763
6864 const onSearchData = debounce ( ( value ) => {
65+ if ( ! searchKey )
66+ return ;
67+
6968 onSearch ( { [ searchKey ] : value } as PagingSwrProps [ "defaultSearch" ] ) ;
7069 setList ( [ ] ) ;
7170 } , 500 ) ;
@@ -75,13 +74,11 @@ export default function LazyLoadSelect<P extends AnyObject, R extends ResponseLi
7574
7675 return (
7776 < Select
78- showSearch
7977 allowClear
80- value = { value }
8178 filterOption = { false }
8279 options = { displayList }
83- fieldNames = { fieldNames }
84- onSearch = { onSearchData }
80+ showSearch = { ! ! searchKey }
81+ onSearch = { searchKey ? onSearchData : undefined }
8582 onPopupScroll = { ( e ) => {
8683 const container = e . target as HTMLDivElement ;
8784 const scrollTop = container ?. scrollTop ;
@@ -91,9 +88,10 @@ export default function LazyLoadSelect<P extends AnyObject, R extends ResponseLi
9188 onloadData ( ) ;
9289 } }
9390 onChange = { ( value ) => {
94- onSearch ( { [ searchKey ] : "" } as PagingSwrProps [ "defaultSearch" ] ) ;
91+ onSearch ( { } ) ;
9592 onChange ?.( value ) ;
9693 } }
94+ { ...reset }
9795 />
9896 ) ;
9997}
0 commit comments