list.js
14.7 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var React = _interopRequireWildcard(require("react"));
var _omit = _interopRequireDefault(require("omit.js"));
var _classnames = _interopRequireDefault(require("classnames"));
var _DownOutlined = _interopRequireDefault(require("@ant-design/icons/DownOutlined"));
var _checkbox = _interopRequireDefault(require("../checkbox"));
var _menu = _interopRequireDefault(require("../menu"));
var _dropdown = _interopRequireDefault(require("../dropdown"));
var _search = _interopRequireDefault(require("./search"));
var _ListBody = _interopRequireWildcard(require("./ListBody"));
var _reactNode = require("../_util/reactNode");
var defaultRender = function defaultRender() {
return null;
};
function isRenderResultPlainObject(result) {
return result && !(0, _reactNode.isValidElement)(result) && Object.prototype.toString.call(result) === '[object Object]';
}
function getEnabledItemKeys(items) {
return items.filter(function (data) {
return !data.disabled;
}).map(function (data) {
return data.key;
});
}
var TransferList = /*#__PURE__*/function (_React$PureComponent) {
(0, _inherits2["default"])(TransferList, _React$PureComponent);
var _super = (0, _createSuper2["default"])(TransferList);
function TransferList(props) {
var _this;
(0, _classCallCheck2["default"])(this, TransferList);
_this = _super.call(this, props);
_this.defaultListBodyRef = /*#__PURE__*/React.createRef(); // =============================== Filter ===============================
_this.handleFilter = function (e) {
var handleFilter = _this.props.handleFilter;
var filterValue = e.target.value;
_this.setState({
filterValue: filterValue
});
handleFilter(e);
};
_this.handleClear = function () {
var handleClear = _this.props.handleClear;
_this.setState({
filterValue: ''
});
handleClear();
};
_this.matchFilter = function (text, item) {
var filterValue = _this.state.filterValue;
var filterOption = _this.props.filterOption;
if (filterOption) {
return filterOption(filterValue, item);
}
return text.indexOf(filterValue) >= 0;
};
_this.getCurrentPageItems = function () {}; // =============================== Render ===============================
_this.renderListBody = function (renderList, props) {
var bodyContent = renderList ? renderList(props) : null;
var customize = !!bodyContent;
if (!customize) {
bodyContent = /*#__PURE__*/React.createElement(_ListBody["default"], (0, _extends2["default"])({
ref: _this.defaultListBodyRef
}, props));
}
return {
customize: customize,
bodyContent: bodyContent
};
};
_this.renderItem = function (item) {
var _this$props$render = _this.props.render,
render = _this$props$render === void 0 ? defaultRender : _this$props$render;
var renderResult = render(item);
var isRenderResultPlain = isRenderResultPlainObject(renderResult);
return {
renderedText: isRenderResultPlain ? renderResult.value : renderResult,
renderedEl: isRenderResultPlain ? renderResult.label : renderResult,
item: item
};
};
_this.getSelectAllLabel = function (selectedCount, totalCount) {
var _this$props = _this.props,
itemsUnit = _this$props.itemsUnit,
itemUnit = _this$props.itemUnit,
selectAllLabel = _this$props.selectAllLabel;
if (selectAllLabel) {
return typeof selectAllLabel === 'function' ? selectAllLabel({
selectedCount: selectedCount,
totalCount: totalCount
}) : selectAllLabel;
}
var unit = totalCount > 1 ? itemsUnit : itemUnit;
return /*#__PURE__*/React.createElement(React.Fragment, null, (selectedCount > 0 ? "".concat(selectedCount, "/") : '') + totalCount, " ", unit);
};
_this.state = {
filterValue: ''
};
return _this;
}
(0, _createClass2["default"])(TransferList, [{
key: "componentWillUnmount",
value: function componentWillUnmount() {
clearTimeout(this.triggerScrollTimer);
}
}, {
key: "getCheckStatus",
value: function getCheckStatus(filteredItems) {
var checkedKeys = this.props.checkedKeys;
if (checkedKeys.length === 0) {
return 'none';
}
if (filteredItems.every(function (item) {
return checkedKeys.indexOf(item.key) >= 0 || !!item.disabled;
})) {
return 'all';
}
return 'part';
} // ================================ Item ================================
}, {
key: "getFilteredItems",
value: function getFilteredItems(dataSource, filterValue) {
var _this2 = this;
var filteredItems = [];
var filteredRenderItems = [];
dataSource.forEach(function (item) {
var renderedItem = _this2.renderItem(item);
var renderedText = renderedItem.renderedText; // Filter skip
if (filterValue && !_this2.matchFilter(renderedText, item)) {
return null;
}
filteredItems.push(item);
filteredRenderItems.push(renderedItem);
});
return {
filteredItems: filteredItems,
filteredRenderItems: filteredRenderItems
};
}
}, {
key: "getListBody",
value: function getListBody(prefixCls, searchPlaceholder, filterValue, filteredItems, notFoundContent, filteredRenderItems, checkedKeys, renderList, showSearch, disabled) {
var search = showSearch ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-body-search-wrapper")
}, /*#__PURE__*/React.createElement(_search["default"], {
prefixCls: "".concat(prefixCls, "-search"),
onChange: this.handleFilter,
handleClear: this.handleClear,
placeholder: searchPlaceholder,
value: filterValue,
disabled: disabled
})) : null;
var _this$renderListBody = this.renderListBody(renderList, (0, _extends2["default"])((0, _extends2["default"])({}, (0, _omit["default"])(this.props, _ListBody.OmitProps)), {
filteredItems: filteredItems,
filteredRenderItems: filteredRenderItems,
selectedKeys: checkedKeys
})),
bodyContent = _this$renderListBody.bodyContent,
customize = _this$renderListBody.customize;
var bodyNode; // We should wrap customize list body in a classNamed div to use flex layout.
if (customize) {
bodyNode = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-body-customize-wrapper")
}, bodyContent);
} else {
bodyNode = filteredItems.length ? bodyContent : /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-body-not-found")
}, notFoundContent);
}
return /*#__PURE__*/React.createElement("div", {
className: (0, _classnames["default"])(showSearch ? "".concat(prefixCls, "-body ").concat(prefixCls, "-body-with-search") : "".concat(prefixCls, "-body"))
}, search, bodyNode);
}
}, {
key: "getCheckBox",
value: function getCheckBox(filteredItems, onItemSelectAll, showSelectAll, disabled) {
var checkStatus = this.getCheckStatus(filteredItems);
var checkedAll = checkStatus === 'all';
var checkAllCheckbox = showSelectAll !== false && /*#__PURE__*/React.createElement(_checkbox["default"], {
disabled: disabled,
checked: checkedAll,
indeterminate: checkStatus === 'part',
onChange: function onChange() {
// Only select enabled items
onItemSelectAll(filteredItems.filter(function (item) {
return !item.disabled;
}).map(function (_ref) {
var key = _ref.key;
return key;
}), !checkedAll);
}
});
return checkAllCheckbox;
}
}, {
key: "render",
value: function render() {
var _classNames,
_this3 = this;
var filterValue = this.state.filterValue;
var _this$props2 = this.props,
prefixCls = _this$props2.prefixCls,
dataSource = _this$props2.dataSource,
titleText = _this$props2.titleText,
checkedKeys = _this$props2.checkedKeys,
disabled = _this$props2.disabled,
footer = _this$props2.footer,
showSearch = _this$props2.showSearch,
style = _this$props2.style,
searchPlaceholder = _this$props2.searchPlaceholder,
notFoundContent = _this$props2.notFoundContent,
selectAll = _this$props2.selectAll,
selectCurrent = _this$props2.selectCurrent,
selectInvert = _this$props2.selectInvert,
removeAll = _this$props2.removeAll,
removeCurrent = _this$props2.removeCurrent,
renderList = _this$props2.renderList,
onItemSelectAll = _this$props2.onItemSelectAll,
onItemRemove = _this$props2.onItemRemove,
showSelectAll = _this$props2.showSelectAll,
showRemove = _this$props2.showRemove,
pagination = _this$props2.pagination; // Custom Layout
var footerDom = footer && footer(this.props);
var listCls = (0, _classnames["default"])(prefixCls, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-with-pagination"), pagination), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-with-footer"), footerDom), _classNames)); // ====================== Get filtered, checked item list ======================
var _this$getFilteredItem = this.getFilteredItems(dataSource, filterValue),
filteredItems = _this$getFilteredItem.filteredItems,
filteredRenderItems = _this$getFilteredItem.filteredRenderItems; // ================================= List Body =================================
var listBody = this.getListBody(prefixCls, searchPlaceholder, filterValue, filteredItems, notFoundContent, filteredRenderItems, checkedKeys, renderList, showSearch, disabled); // ================================ List Footer ================================
var listFooter = footerDom ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-footer")
}, footerDom) : null;
var checkAllCheckbox = !showRemove && !pagination && this.getCheckBox(filteredItems, onItemSelectAll, showSelectAll, disabled);
var menu = null;
if (showRemove) {
menu = /*#__PURE__*/React.createElement(_menu["default"], null, pagination && /*#__PURE__*/React.createElement(_menu["default"].Item, {
onClick: function onClick() {
var _a;
var pageKeys = getEnabledItemKeys((((_a = _this3.defaultListBodyRef.current) === null || _a === void 0 ? void 0 : _a.getItems()) || []).map(function (entity) {
return entity.item;
}));
onItemRemove === null || onItemRemove === void 0 ? void 0 : onItemRemove(pageKeys);
}
}, removeCurrent), /*#__PURE__*/React.createElement(_menu["default"].Item, {
onClick: function onClick() {
onItemRemove === null || onItemRemove === void 0 ? void 0 : onItemRemove(getEnabledItemKeys(filteredItems));
}
}, removeAll));
} else {
menu = /*#__PURE__*/React.createElement(_menu["default"], null, /*#__PURE__*/React.createElement(_menu["default"].Item, {
onClick: function onClick() {
var keys = getEnabledItemKeys(filteredItems);
onItemSelectAll(keys, keys.length !== checkedKeys.length);
}
}, selectAll), pagination && /*#__PURE__*/React.createElement(_menu["default"].Item, {
onClick: function onClick() {
var _a;
var pageItems = ((_a = _this3.defaultListBodyRef.current) === null || _a === void 0 ? void 0 : _a.getItems()) || [];
onItemSelectAll(getEnabledItemKeys(pageItems.map(function (entity) {
return entity.item;
})), true);
}
}, selectCurrent), /*#__PURE__*/React.createElement(_menu["default"].Item, {
onClick: function onClick() {
var _a;
var availableKeys;
if (pagination) {
availableKeys = getEnabledItemKeys((((_a = _this3.defaultListBodyRef.current) === null || _a === void 0 ? void 0 : _a.getItems()) || []).map(function (entity) {
return entity.item;
}));
} else {
availableKeys = getEnabledItemKeys(filteredItems);
}
var checkedKeySet = new Set(checkedKeys);
var newCheckedKeys = [];
var newUnCheckedKeys = [];
availableKeys.forEach(function (key) {
if (checkedKeySet.has(key)) {
newUnCheckedKeys.push(key);
} else {
newCheckedKeys.push(key);
}
});
onItemSelectAll(newCheckedKeys, true);
onItemSelectAll(newUnCheckedKeys, false);
}
}, selectInvert));
}
var dropdown = /*#__PURE__*/React.createElement(_dropdown["default"], {
className: "".concat(prefixCls, "-header-dropdown"),
overlay: menu,
disabled: disabled
}, /*#__PURE__*/React.createElement(_DownOutlined["default"], null)); // ================================== Render ===================================
return /*#__PURE__*/React.createElement("div", {
className: listCls,
style: style
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-header")
}, checkAllCheckbox, dropdown, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-header-selected")
}, this.getSelectAllLabel(checkedKeys.length, filteredItems.length)), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-header-title")
}, titleText)), listBody, listFooter);
}
}]);
return TransferList;
}(React.PureComponent);
exports["default"] = TransferList;
TransferList.defaultProps = {
dataSource: [],
titleText: '',
showSearch: false
};