-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
80 lines (62 loc) · 1.95 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React, { Component } from 'react';
import infinityScrollLoader from "../infinity-scroll-loader";
export default class Parent extends React.Component {
constructor(props) {
super(props);
let partObj = this.partitionArray(this.props.fields)
this.partitionSugestions = partObj.newArray
this.maxParts = partObj.maxParts
this.lastLoadedPart = 0
}
partitionArray = (arr) => {
let partSize = 500
let currPartSize = partSize
let newArray = []
let tempArray = []
let i, j
for (i = 0, j = 0; i < arr.length; i++) {
if (i > currPartSize) {
newArray[j] = tempArray
tempArray = []
j++
currPartSize = currPartSize + partSize
}
tempArray.push(arr[i])
}
newArray[j] = tempArray
return { newArray, maxParts: j }
}
loadMoreSuggestions = () => {
if (this.state.fieldValue !== '*') { return false }
if (this.lastLoadedPart < this.maxParts) {
this.lastLoadedPart++
this.setState((prevState) => {
let combinedArr = prevState.suggestions.concat(this.partitionSugestions[this.lastLoadedPart])
return { suggestions: combinedArr }
})
}
}
render() {
return (
<FieldSelector loadMoreSuggestions={this.loadMoreSuggestions} />
)
}
}
class FieldSelector extends React.Component {
constructor(props) {
super(props)
this.fieldSelectorRef = React.CreateRef()
}
componentDidMount() {
let dropDownContainer = this.fieldSelectorRef
this.iLoader = new infinityScrollLoader(dropDownContainer, this.props.loadMoreSuggestions)
}
componentWillUnmount() {
this.iLoader.removeListener()
}
render() {
return (
<Field ref={this.fieldSelectorRef} />
)
}
}