forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemListInput.tsx
82 lines (66 loc) · 2 KB
/
ItemListInput.tsx
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
81
82
/* eslint no-console: "off" */
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { ItemList } from '@deephaven/components';
interface ItemListInput {
selectedItems: number[];
}
interface ItemListInputProps {
isMultiSelect: boolean;
}
interface ItemListInputState {
itemCount: number;
items: { value: string; isSelected: boolean }[];
offset: number;
}
class ItemListInput extends PureComponent<
ItemListInputProps,
ItemListInputState
> {
static defaultProps: { isMultiSelect: boolean };
static propTypes: { isMultiSelect: PropTypes.Requireable<boolean> };
constructor(props: ItemListInputProps) {
super(props);
this.handleSelect = this.handleSelect.bind(this);
this.handleViewportChange = this.handleViewportChange.bind(this);
this.selectedItems = [];
this.state = {
items: [],
offset: 0,
itemCount: 500000,
};
}
handleSelect(itemIndex: number): void {
const { itemCount } = this.state;
console.log('Item selected at index', itemIndex, '/', itemCount);
}
handleViewportChange(top: number, bottom: number): void {
const { itemCount } = this.state;
const viewportSize = bottom - top + 1;
const topRow = Math.max(0, top - viewportSize);
const bottomRow = Math.min(bottom + viewportSize, itemCount);
const items = [];
for (let i: number = topRow; i <= bottomRow; i += 1) {
const value = `Item ${i}`;
const isSelected = this.selectedItems.indexOf(i) >= 0;
items.push({ value, isSelected });
}
const offset = topRow;
this.setState({ offset, items });
}
render(): React.ReactElement {
const { isMultiSelect } = this.props;
const { offset, items, itemCount } = this.state;
return (
<ItemList
isMultiSelect={isMultiSelect}
itemCount={itemCount}
items={items}
offset={offset}
onSelect={this.handleSelect}
onViewportChange={this.handleViewportChange}
/>
);
}
}
export default ItemListInput;