Skip to content

Commit 5a31c1f

Browse files
author
jeromeZhang
committed
feat: add lazyDataSource
1 parent 4c8aab3 commit 5a31c1f

File tree

3 files changed

+76
-15
lines changed

3 files changed

+76
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,4 @@
155155
"react-router": "3.0.4",
156156
"showdown": "^1.9.0"
157157
}
158-
}
158+
}

src/components/easy-select/index.tsx

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ import debounce from 'lodash/debounce';
44

55
const { Option } = Select;
66
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+
};
1414

1515
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+
}
1822
}
1923

2024
onSearch = (str: any) => {
@@ -28,14 +32,44 @@ class EasySelect extends React.Component<any, any> {
2832
}
2933
}
3034

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+
3149
getDataSource = async (str: any) => {
3250
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+
})
3658
})
37-
})
59+
} else {
60+
const { allData } = this.state;
61+
this.lazyDataSource(allData)
62+
}
3863
}
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+
};
3973
render () {
4074
const { allowClear = true, showSearch = true, filterLocal, servise, ...others } = this.props;
4175
const { dataSource } = this.state;
@@ -44,12 +78,13 @@ class EasySelect extends React.Component<any, any> {
4478
allowClear={allowClear} // 默认支持清除
4579
showSearch={showSearch} // 默认支持查询
4680
style={{ minWidth: 120 }} // todo: 暂时样式,有待商榷
47-
onSearch={ servise && !filterLocal && this.onSearch }
81+
onSearch={ servise && !filterLocal ? this.onSearch : null }
4882
filterOption={ !filterLocal ? null : (input, option) =>
4983
// 兼容数字和字符串等模糊查询
5084
option.props.children.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0 ||
5185
option.props.value.toString().toLowerCase().indexOf(input.toLowerCase()) >= 0
5286
}
87+
onPopupScroll={this.companyScroll}
5388
{ ...others }
5489
>
5590
{

src/stories/easySelect.stories.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const servise = (str: any) =>
1515
label: item[0],
1616
value: item[1]
1717
})));
18+
const options = [];
19+
for (let i = 0; i < 100000; i++) {
20+
const value = i.toString(36);
21+
options.push(value);
22+
}
1823
const propDefinitions = [
1924
{
2025
property: 'dataSource',
@@ -106,6 +111,13 @@ stories.add('EasySelect', () => (
106111
servise={servise}
107112
onChange={(val: any, option: any) => { console.log(val, option) }}
108113
/>
114+
<p className="strory-dt_easy_select_p">6、大数据(虚拟滚动)</p>
115+
<EasySelect
116+
style={{ width: '100%' }}
117+
filterLocal
118+
dataSource={options}
119+
onChange={(val: any, option: any) => { console.log(val, option) }}
120+
/>
109121
</div>
110122
), {
111123
info: {
@@ -161,6 +173,20 @@ stories.add('EasySelect', () => (
161173
onChange={(val: any, option: any) => { console.log(val, option) }}
162174
/>
163175
~~~
176+
~~~js
177+
// 大数据(虚拟滚动)
178+
<EasySelect
179+
style={{ width: '100%' }}
180+
filterLocal
181+
dataSource={options}
182+
// const options = [];
183+
// for (let i = 0; i < 100000; i++) {
184+
// const value = i.toString(36)
185+
// options.push(value);
186+
// }
187+
onChange={(val: any, option: any) => { console.log(val, option) }}
188+
/>
189+
~~~
164190
`
165191
}
166192
})

0 commit comments

Comments
 (0)