@@ -4,17 +4,21 @@ import debounce from 'lodash/debounce';
4
4
5
5
const { Option } = Select ;
6
6
class EasySelect extends React . Component < any , any > {
7
- constructor ( props : any ) {
8
- super ( props ) ;
9
- const { dataSource = [ ] } = this . props ;
10
- this . state = {
11
- dataSource
12
- } ;
13
- }
7
+
8
+ state = {
9
+ dataSource : [ ] ,
10
+ str : '' ,
11
+ scrollPage : 1 ,
12
+ allData : [ ]
13
+ } ;
14
14
15
15
componentDidMount = ( ) => {
16
- const { autoValue = '' } = this . props ;
17
- this . getDataSource ( autoValue ) ;
16
+ const { autoValue = '' , dataSource = [ ] } = this . props ;
17
+ if ( dataSource . length > 0 ) {
18
+ this . lazyDataSource ( dataSource )
19
+ } else {
20
+ this . getDataSource ( autoValue ) ;
21
+ }
18
22
}
19
23
20
24
onSearch = ( str : any ) => {
@@ -28,14 +32,44 @@ class EasySelect extends React.Component<any, any> {
28
32
}
29
33
}
30
34
35
+ lazyDataSource = ( data : any ) => {
36
+ const { scrollPage = 1 } = this . state ;
37
+ if ( data . length > ( scrollPage * 100 ) ) {
38
+ this . setState ( {
39
+ dataSource : data . slice ( 0 , scrollPage * 100 ) || [ ] ,
40
+ allData : data
41
+ } )
42
+ } else {
43
+ this . setState ( {
44
+ dataSource : data || [ ]
45
+ } )
46
+ }
47
+ }
48
+
31
49
getDataSource = async ( str : any ) => {
32
50
const { servise } = this . props ;
33
- servise && await servise ( str ) . then ( ( res : any ) => {
34
- this . setState ( {
35
- dataSource : res || [ ]
51
+ if ( servise ) {
52
+ await servise ( str ) . then ( ( res : any ) => {
53
+ this . setState ( {
54
+ str
55
+ } , ( ) => {
56
+ this . lazyDataSource ( res )
57
+ } )
36
58
} )
37
- } )
59
+ } else {
60
+ const { allData } = this . state ;
61
+ this . lazyDataSource ( allData )
62
+ }
38
63
}
64
+ companyScroll = ( e : { persist ?: any ; target ?: any ; } ) => {
65
+ e . persist ( ) ;
66
+ const { target } = e ;
67
+ const { str, scrollPage, allData } = this . state ;
68
+ if ( target . scrollTop + target . offsetHeight === target . scrollHeight && allData . length > 0 ) {
69
+ const nextScrollPage = scrollPage + 1 ;
70
+ this . setState ( { scrollPage : nextScrollPage } , ( ) => this . getDataSource ( str ) ) ;
71
+ }
72
+ } ;
39
73
render ( ) {
40
74
const { allowClear = true , showSearch = true , filterLocal, servise, ...others } = this . props ;
41
75
const { dataSource } = this . state ;
@@ -44,12 +78,13 @@ class EasySelect extends React.Component<any, any> {
44
78
allowClear = { allowClear } // 默认支持清除
45
79
showSearch = { showSearch } // 默认支持查询
46
80
style = { { minWidth : 120 } } // todo: 暂时样式,有待商榷
47
- onSearch = { servise && ! filterLocal && this . onSearch }
81
+ onSearch = { servise && ! filterLocal ? this . onSearch : null }
48
82
filterOption = { ! filterLocal ? null : ( input , option ) =>
49
83
// 兼容数字和字符串等模糊查询
50
84
option . props . children . toString ( ) . toLowerCase ( ) . indexOf ( input . toLowerCase ( ) ) >= 0 ||
51
85
option . props . value . toString ( ) . toLowerCase ( ) . indexOf ( input . toLowerCase ( ) ) >= 0
52
86
}
87
+ onPopupScroll = { this . companyScroll }
53
88
{ ...others }
54
89
>
55
90
{
0 commit comments