ListBody.js
5.03 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _extends from "@babel/runtime/helpers/extends";
import _typeof from "@babel/runtime/helpers/typeof";
import * as React from 'react';
import classNames from 'classnames';
import { tuple } from '../_util/type';
import Pagination from '../pagination';
import ListItem from './ListItem';
export var OmitProps = tuple('handleFilter', 'handleClear', 'checkedKeys');
function parsePagination(pagination) {
if (!pagination) {
return null;
}
var defaultPagination = {
pageSize: 10
};
if (_typeof(pagination) === 'object') {
return _extends(_extends({}, defaultPagination), pagination);
}
return defaultPagination;
}
var ListBody = /*#__PURE__*/function (_React$Component) {
_inherits(ListBody, _React$Component);
var _super = _createSuper(ListBody);
function ListBody() {
var _this;
_classCallCheck(this, ListBody);
_this = _super.apply(this, arguments);
_this.state = {
current: 1
};
_this.onItemSelect = function (item) {
var _this$props = _this.props,
onItemSelect = _this$props.onItemSelect,
selectedKeys = _this$props.selectedKeys;
var checked = selectedKeys.indexOf(item.key) >= 0;
onItemSelect(item.key, !checked);
};
_this.onItemRemove = function (item) {
var onItemRemove = _this.props.onItemRemove;
onItemRemove === null || onItemRemove === void 0 ? void 0 : onItemRemove([item.key]);
};
_this.onPageChange = function (current) {
_this.setState({
current: current
});
};
_this.getItems = function () {
var current = _this.state.current;
var _this$props2 = _this.props,
pagination = _this$props2.pagination,
filteredRenderItems = _this$props2.filteredRenderItems;
var mergedPagination = parsePagination(pagination);
var displayItems = filteredRenderItems;
if (mergedPagination) {
displayItems = filteredRenderItems.slice((current - 1) * mergedPagination.pageSize, current * mergedPagination.pageSize);
}
return displayItems;
};
return _this;
}
_createClass(ListBody, [{
key: "render",
value: function render() {
var _this2 = this;
var current = this.state.current;
var _this$props3 = this.props,
prefixCls = _this$props3.prefixCls,
onScroll = _this$props3.onScroll,
filteredRenderItems = _this$props3.filteredRenderItems,
selectedKeys = _this$props3.selectedKeys,
globalDisabled = _this$props3.disabled,
showRemove = _this$props3.showRemove,
pagination = _this$props3.pagination;
var mergedPagination = parsePagination(pagination);
var paginationNode = null;
if (mergedPagination) {
paginationNode = /*#__PURE__*/React.createElement(Pagination, {
simple: true,
size: "small",
disabled: globalDisabled,
className: "".concat(prefixCls, "-pagination"),
total: filteredRenderItems.length,
pageSize: mergedPagination.pageSize,
current: current,
onChange: this.onPageChange
});
}
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("ul", {
className: classNames("".concat(prefixCls, "-content"), _defineProperty({}, "".concat(prefixCls, "-content-show-remove"), showRemove)),
onScroll: onScroll
}, this.getItems().map(function (_ref) {
var renderedEl = _ref.renderedEl,
renderedText = _ref.renderedText,
item = _ref.item;
var disabled = item.disabled;
var checked = selectedKeys.indexOf(item.key) >= 0;
return /*#__PURE__*/React.createElement(ListItem, {
disabled: globalDisabled || disabled,
key: item.key,
item: item,
renderedText: renderedText,
renderedEl: renderedEl,
checked: checked,
prefixCls: prefixCls,
onClick: _this2.onItemSelect,
onRemove: _this2.onItemRemove,
showRemove: showRemove
});
})), paginationNode);
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(_ref2, _ref3) {
var filteredRenderItems = _ref2.filteredRenderItems,
pagination = _ref2.pagination;
var current = _ref3.current;
var mergedPagination = parsePagination(pagination);
if (mergedPagination) {
// Calculate the page number
var maxPageCount = Math.ceil(filteredRenderItems.length / mergedPagination.pageSize);
if (current > maxPageCount) {
return {
current: maxPageCount
};
}
}
return null;
}
}]);
return ListBody;
}(React.Component);
export default ListBody;