1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
3 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
4 | import _inherits from 'babel-runtime/helpers/inherits';
|
5 |
|
6 | var _class, _temp;
|
7 |
|
8 | import React, { Component } from 'react';
|
9 | import PropTypes from 'prop-types';
|
10 | import cx from 'classnames';
|
11 | import Checkbox from '../../checkbox';
|
12 | import Search from '../../search';
|
13 | import Menu from '../../menu';
|
14 | import { func, htmlId } from '../../util';
|
15 | import TransferItem from './transfer-item';
|
16 | import VirtualList from '../../virtual-list';
|
17 |
|
18 | var bindCtx = func.bindCtx;
|
19 | var TransferPanel = (_temp = _class = function (_Component) {
|
20 | _inherits(TransferPanel, _Component);
|
21 |
|
22 | function TransferPanel(props, context) {
|
23 | _classCallCheck(this, TransferPanel);
|
24 |
|
25 | var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));
|
26 |
|
27 | _this.state = {
|
28 | searchedValue: '',
|
29 | dragValue: null,
|
30 | dragOverValue: null
|
31 | };
|
32 | _this.footerId = props.baseId ? htmlId.escapeForId(props.baseId + '-panel-footer-' + props.position) : '';
|
33 | _this.headerId = props.baseId ? htmlId.escapeForId(props.baseId + '-panel-header-' + props.position) : '';
|
34 |
|
35 | bindCtx(_this, ['handleCheck', 'handleAllCheck', 'handleSearch', 'handleItemDragStart', 'handleItemDragOver', 'handleItemDragEnd', 'handleItemDrop', 'getListDOM']);
|
36 | _this.firstRender = true;
|
37 | return _this;
|
38 | }
|
39 |
|
40 | TransferPanel.prototype.componentDidMount = function componentDidMount() {
|
41 | this.firstRender = false;
|
42 | };
|
43 |
|
44 | TransferPanel.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
|
45 | if (prevProps.dataSource.length !== this.props.dataSource.length && this.list) {
|
46 | if (this.list.scrollTop > 0) {
|
47 | this.list.scrollTop = 0;
|
48 | }
|
49 | }
|
50 |
|
51 | this.searched = false;
|
52 | };
|
53 |
|
54 | TransferPanel.prototype.getListDOM = function getListDOM(ref) {
|
55 | this.list = ref;
|
56 | };
|
57 |
|
58 | TransferPanel.prototype.getListData = function getListData(dataSource, disableHighlight) {
|
59 | var _this2 = this;
|
60 |
|
61 | var _props = this.props,
|
62 | prefix = _props.prefix,
|
63 | position = _props.position,
|
64 | mode = _props.mode,
|
65 | value = _props.value,
|
66 | onMove = _props.onMove,
|
67 | disabled = _props.disabled,
|
68 | itemRender = _props.itemRender,
|
69 | sortable = _props.sortable;
|
70 | var _state = this.state,
|
71 | dragPosition = _state.dragPosition,
|
72 | dragValue = _state.dragValue,
|
73 | dragOverValue = _state.dragOverValue;
|
74 |
|
75 | return dataSource.map(function (item) {
|
76 | var others = 'title' in item ? {
|
77 | title: item.title
|
78 | } : {};
|
79 |
|
80 | return React.createElement(TransferItem, _extends({
|
81 | key: item.value,
|
82 | prefix: prefix,
|
83 | mode: mode,
|
84 | checked: value.indexOf(item.value) > -1,
|
85 | disabled: disabled || item.disabled,
|
86 | item: item,
|
87 | onCheck: _this2.handleCheck,
|
88 | onClick: onMove,
|
89 | needHighlight: !_this2.firstRender && !_this2.searched && !disableHighlight,
|
90 | itemRender: itemRender,
|
91 | draggable: sortable,
|
92 | onDragStart: _this2.handleItemDragStart,
|
93 | onDragOver: _this2.handleItemDragOver,
|
94 | onDragEnd: _this2.handleItemDragEnd,
|
95 | onDrop: _this2.handleItemDrop,
|
96 | dragPosition: dragPosition,
|
97 | dragValue: dragValue,
|
98 | dragOverValue: dragOverValue,
|
99 | panelPosition: position
|
100 | }, others));
|
101 | });
|
102 | };
|
103 |
|
104 | TransferPanel.prototype.handleAllCheck = function handleAllCheck(allChecked) {
|
105 | var _props2 = this.props,
|
106 | position = _props2.position,
|
107 | onChange = _props2.onChange,
|
108 | filter = _props2.filter;
|
109 | var searchedValue = this.state.searchedValue;
|
110 |
|
111 |
|
112 | var newValue = void 0;
|
113 | if (allChecked) {
|
114 | if (searchedValue) {
|
115 | newValue = this.enabledDatasource.filter(function (item) {
|
116 | return filter(searchedValue, item);
|
117 | }).map(function (item) {
|
118 | return item.value;
|
119 | });
|
120 | } else {
|
121 | newValue = this.enabledDatasource.map(function (item) {
|
122 | return item.value;
|
123 | });
|
124 | }
|
125 | } else {
|
126 | newValue = [];
|
127 | }
|
128 |
|
129 | onChange && onChange(position, newValue);
|
130 | };
|
131 |
|
132 | TransferPanel.prototype.handleCheck = function handleCheck(itemValue, checked) {
|
133 | var _props3 = this.props,
|
134 | position = _props3.position,
|
135 | value = _props3.value,
|
136 | onChange = _props3.onChange;
|
137 |
|
138 |
|
139 | var newValue = [].concat(value);
|
140 | var index = value.indexOf(itemValue);
|
141 | if (checked && index === -1) {
|
142 | newValue.push(itemValue);
|
143 | } else if (!checked && index > -1) {
|
144 | newValue.splice(index, 1);
|
145 | }
|
146 |
|
147 | onChange && onChange(position, newValue);
|
148 | };
|
149 |
|
150 | TransferPanel.prototype.handleSearch = function handleSearch(searchedValue) {
|
151 | this.setState({
|
152 | searchedValue: searchedValue
|
153 | });
|
154 | this.searched = true;
|
155 |
|
156 | var _props4 = this.props,
|
157 | onSearch = _props4.onSearch,
|
158 | position = _props4.position;
|
159 |
|
160 | onSearch(searchedValue, position);
|
161 | };
|
162 |
|
163 | TransferPanel.prototype.handleItemDragStart = function handleItemDragStart(position, value) {
|
164 | this.setState({
|
165 | dragPosition: position,
|
166 | dragValue: value
|
167 | });
|
168 | };
|
169 |
|
170 | TransferPanel.prototype.handleItemDragOver = function handleItemDragOver(value) {
|
171 | this.setState({
|
172 | dragOverValue: value
|
173 | });
|
174 | };
|
175 |
|
176 | TransferPanel.prototype.handleItemDragEnd = function handleItemDragEnd() {
|
177 | this.setState({
|
178 | dragOverValue: null
|
179 | });
|
180 | };
|
181 |
|
182 | TransferPanel.prototype.handleItemDrop = function handleItemDrop() {
|
183 | var _props5;
|
184 |
|
185 | this.setState({
|
186 | dragOverValue: null
|
187 | });
|
188 |
|
189 | (_props5 = this.props).onSort.apply(_props5, arguments);
|
190 | };
|
191 |
|
192 | TransferPanel.prototype.renderHeader = function renderHeader() {
|
193 | var _props6 = this.props,
|
194 | title = _props6.title,
|
195 | prefix = _props6.prefix;
|
196 |
|
197 |
|
198 | return React.createElement(
|
199 | 'div',
|
200 | { id: this.headerId, className: prefix + 'transfer-panel-header' },
|
201 | title
|
202 | );
|
203 | };
|
204 |
|
205 | TransferPanel.prototype.renderSearch = function renderSearch() {
|
206 | var _props7 = this.props,
|
207 | prefix = _props7.prefix,
|
208 | searchPlaceholder = _props7.searchPlaceholder,
|
209 | locale = _props7.locale,
|
210 | _props7$searchProps = _props7.searchProps,
|
211 | searchProps = _props7$searchProps === undefined ? {} : _props7$searchProps;
|
212 |
|
213 | return React.createElement(Search, _extends({
|
214 | 'aria-labelledby': this.headerId,
|
215 | shape: 'simple'
|
216 | }, searchProps, {
|
217 | className: prefix + 'transfer-panel-search',
|
218 | placeholder: searchPlaceholder || locale.searchPlaceholder,
|
219 | onChange: this.handleSearch
|
220 | }));
|
221 | };
|
222 |
|
223 | TransferPanel.prototype.renderList = function renderList(dataSource) {
|
224 | var _cx;
|
225 |
|
226 | var _props8 = this.props,
|
227 | prefix = _props8.prefix,
|
228 | listClassName = _props8.listClassName,
|
229 | listStyle = _props8.listStyle,
|
230 | customerList = _props8.customerList,
|
231 | useVirtual = _props8.useVirtual;
|
232 |
|
233 | var newClassName = cx((_cx = {}, _cx[prefix + 'transfer-panel-list'] = true, _cx[listClassName] = !!listClassName, _cx));
|
234 |
|
235 | var customerPanel = customerList && customerList(this.props);
|
236 | if (customerPanel) {
|
237 | return React.createElement(
|
238 | 'div',
|
239 | { className: newClassName, style: listStyle, ref: this.getListDOM },
|
240 | customerPanel
|
241 | );
|
242 | }
|
243 |
|
244 | if (!dataSource.length) {
|
245 | return React.createElement(
|
246 | 'div',
|
247 | { className: newClassName, style: listStyle },
|
248 | this.renderNotFoundContent()
|
249 | );
|
250 | }
|
251 |
|
252 | if (useVirtual) {
|
253 | return React.createElement(
|
254 | 'div',
|
255 | { className: newClassName, style: _extends({ position: 'relative' }, listStyle) },
|
256 | React.createElement(
|
257 | VirtualList,
|
258 | {
|
259 | itemsRenderer: function itemsRenderer(items, ref) {
|
260 | return React.createElement(
|
261 | Menu,
|
262 | { style: { border: 'none' }, ref: ref },
|
263 | items
|
264 | );
|
265 | }
|
266 | },
|
267 | this.getListData(dataSource, true)
|
268 | )
|
269 | );
|
270 | }
|
271 |
|
272 | return React.createElement(
|
273 | Menu,
|
274 | { className: newClassName, style: listStyle, ref: this.getListDOM },
|
275 | this.getListData(dataSource)
|
276 | );
|
277 | };
|
278 |
|
279 | TransferPanel.prototype.renderNotFoundContent = function renderNotFoundContent() {
|
280 | var _props9 = this.props,
|
281 | prefix = _props9.prefix,
|
282 | notFoundContent = _props9.notFoundContent;
|
283 |
|
284 |
|
285 | return React.createElement(
|
286 | 'div',
|
287 | { className: prefix + 'transfer-panel-not-found-container' },
|
288 | React.createElement(
|
289 | 'div',
|
290 | { className: prefix + 'transfer-panel-not-found' },
|
291 | notFoundContent
|
292 | )
|
293 | );
|
294 | };
|
295 |
|
296 | TransferPanel.prototype.renderFooter = function renderFooter() {
|
297 | var _props10 = this.props,
|
298 | prefix = _props10.prefix,
|
299 | position = _props10.position,
|
300 | mode = _props10.mode,
|
301 | disabled = _props10.disabled,
|
302 | locale = _props10.locale,
|
303 | showCheckAll = _props10.showCheckAll;
|
304 |
|
305 | if (mode === 'simple') {
|
306 | var _cx2;
|
307 |
|
308 | var onMoveAll = this.props.onMoveAll;
|
309 |
|
310 | var classNames = cx((_cx2 = {}, _cx2[prefix + 'transfer-panel-move-all'] = true, _cx2[prefix + 'disabled'] = disabled, _cx2));
|
311 | return React.createElement(
|
312 | 'div',
|
313 | { className: prefix + 'transfer-panel-footer' },
|
314 | React.createElement(
|
315 | 'a',
|
316 | { className: classNames, onClick: onMoveAll.bind(this, position === 'left' ? 'right' : 'left') },
|
317 | locale.moveAll
|
318 | )
|
319 | );
|
320 | }
|
321 |
|
322 | var _props11 = this.props,
|
323 | value = _props11.value,
|
324 | showSearch = _props11.showSearch,
|
325 | filter = _props11.filter,
|
326 | dataSource = _props11.dataSource;
|
327 | var searchedValue = this.state.searchedValue;
|
328 |
|
329 | var totalCount = dataSource.length;
|
330 | var _dataSource = dataSource;
|
331 | var checkedCount = value.length;
|
332 | var _checkedCount = checkedCount;
|
333 | if (showSearch && searchedValue) {
|
334 | _dataSource = dataSource.filter(function (item) {
|
335 | return filter(searchedValue, item);
|
336 | });
|
337 | totalCount = _dataSource.length;
|
338 | _checkedCount = _dataSource.filter(function (item) {
|
339 | return value.includes(item.value);
|
340 | }).length;
|
341 | }
|
342 | var totalEnabledCount = Math.min(totalCount, this.enabledDatasource.length);
|
343 | var checked = checkedCount > 0 && checkedCount >= totalEnabledCount;
|
344 | var indeterminate = checkedCount > 0 && _checkedCount >= 0 && _checkedCount < totalEnabledCount;
|
345 | var items = totalCount > 1 ? locale.items : locale.item;
|
346 | var countLabel = checkedCount === 0 ? totalCount + ' ' + items : checkedCount + '/' + totalCount + ' ' + items;
|
347 |
|
348 | return React.createElement(
|
349 | 'div',
|
350 | { className: prefix + 'transfer-panel-footer' },
|
351 | showCheckAll && React.createElement(Checkbox, {
|
352 | disabled: disabled,
|
353 | checked: checked,
|
354 | indeterminate: indeterminate,
|
355 | onChange: this.handleAllCheck,
|
356 | 'aria-labelledby': this.footerId
|
357 | }),
|
358 | React.createElement(
|
359 | 'span',
|
360 | { className: prefix + 'transfer-panel-count', id: this.footerId },
|
361 | countLabel
|
362 | )
|
363 | );
|
364 | };
|
365 |
|
366 | TransferPanel.prototype.render = function render() {
|
367 | var _props12 = this.props,
|
368 | prefix = _props12.prefix,
|
369 | title = _props12.title,
|
370 | showSearch = _props12.showSearch,
|
371 | filter = _props12.filter,
|
372 | dataSource = _props12.dataSource;
|
373 | var searchedValue = this.state.searchedValue;
|
374 |
|
375 | var _dataSource = this.props.dataSource;
|
376 | this.enabledDatasource = dataSource.filter(function (item) {
|
377 | return !item.disabled;
|
378 | });
|
379 | if (showSearch && searchedValue) {
|
380 | _dataSource = dataSource.filter(function (item) {
|
381 | return filter(searchedValue, item);
|
382 | });
|
383 | }
|
384 |
|
385 | return React.createElement(
|
386 | 'div',
|
387 | { className: prefix + 'transfer-panel' },
|
388 | title ? this.renderHeader() : null,
|
389 | showSearch ? this.renderSearch() : null,
|
390 | this.renderList(_dataSource),
|
391 | this.renderFooter()
|
392 | );
|
393 | };
|
394 |
|
395 | return TransferPanel;
|
396 | }(Component), _class.propTypes = {
|
397 | prefix: PropTypes.string,
|
398 | position: PropTypes.oneOf(['left', 'right']),
|
399 | mode: PropTypes.oneOf(['normal', 'simple']),
|
400 | dataSource: PropTypes.array,
|
401 | value: PropTypes.array,
|
402 | onChange: PropTypes.func,
|
403 | onMove: PropTypes.func,
|
404 | onMoveAll: PropTypes.func,
|
405 | disabled: PropTypes.bool,
|
406 | locale: PropTypes.object,
|
407 | title: PropTypes.node,
|
408 | showSearch: PropTypes.bool,
|
409 | searchProps: PropTypes.object,
|
410 | filter: PropTypes.func,
|
411 | onSearch: PropTypes.func,
|
412 | searchPlaceholder: PropTypes.string,
|
413 | notFoundContent: PropTypes.node,
|
414 | listClassName: PropTypes.string,
|
415 | listStyle: PropTypes.object,
|
416 | itemRender: PropTypes.func,
|
417 | sortable: PropTypes.bool,
|
418 | onSort: PropTypes.func,
|
419 | baseId: PropTypes.string,
|
420 | customerList: PropTypes.func,
|
421 | useVirtual: PropTypes.bool,
|
422 | showCheckAll: PropTypes.bool
|
423 | }, _temp);
|
424 | TransferPanel.displayName = 'TransferPanel';
|
425 | export { TransferPanel as default }; |
\ | No newline at end of file |