2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2015-12-24 17:44:54 +08:00
|
|
|
import classNames from 'classnames';
|
2015-12-29 15:48:22 +08:00
|
|
|
import Animate from 'rc-animate';
|
2016-10-28 13:56:23 +08:00
|
|
|
import PureRenderMixin from 'rc-util/lib/PureRenderMixin';
|
2017-03-17 15:23:25 +08:00
|
|
|
import Checkbox from '../checkbox';
|
2016-07-01 20:52:17 +08:00
|
|
|
import { TransferItem } from './index';
|
2016-12-16 17:59:52 +08:00
|
|
|
import Search from './search';
|
2016-11-04 16:52:57 +08:00
|
|
|
import Item from './item';
|
2017-05-02 15:47:51 +08:00
|
|
|
import triggerEvent from '../_util/triggerEvent';
|
2015-12-24 17:44:54 +08:00
|
|
|
|
2015-11-25 23:17:06 +08:00
|
|
|
function noop() {
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
function isRenderResultPlainObject(result: any) {
|
2017-01-27 17:08:12 +08:00
|
|
|
return result && !React.isValidElement(result) &&
|
|
|
|
Object.prototype.toString.call(result) === '[object Object]';
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:25:03 +08:00
|
|
|
export interface TransferListProps {
|
2016-10-24 16:30:38 +08:00
|
|
|
prefixCls: string;
|
2017-03-17 15:23:25 +08:00
|
|
|
titleText: string;
|
2016-09-19 17:52:20 +08:00
|
|
|
dataSource: TransferItem[];
|
2017-03-17 15:23:25 +08:00
|
|
|
filter: string;
|
2017-07-17 16:01:18 +08:00
|
|
|
filterOption?: (filterText: any, item: any) => boolean;
|
2016-07-13 11:14:24 +08:00
|
|
|
style?: React.CSSProperties;
|
2017-03-17 15:23:25 +08:00
|
|
|
checkedKeys: string[];
|
2016-10-24 16:30:38 +08:00
|
|
|
handleFilter: (e: any) => void;
|
|
|
|
handleSelect: (selectedItem: any, checked: boolean) => void;
|
|
|
|
handleSelectAll: (dataSource: any[], checkAll: boolean) => void;
|
|
|
|
handleClear: () => void;
|
2016-08-08 15:00:35 +08:00
|
|
|
render?: (item: any) => any;
|
2017-03-17 15:23:25 +08:00
|
|
|
showSearch?: boolean;
|
|
|
|
searchPlaceholder: string;
|
|
|
|
notFoundContent: React.ReactNode;
|
|
|
|
itemUnit: string;
|
|
|
|
itemsUnit: string;
|
2016-08-08 14:42:29 +08:00
|
|
|
body?: (props: any) => any;
|
|
|
|
footer?: (props: any) => void;
|
2016-12-16 17:59:52 +08:00
|
|
|
lazy?: boolean | {};
|
2017-03-01 11:07:19 +08:00
|
|
|
onScroll: Function;
|
2016-08-08 14:42:29 +08:00
|
|
|
}
|
|
|
|
|
2016-07-01 20:52:17 +08:00
|
|
|
export default class TransferList extends React.Component<TransferListProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
dataSource: [],
|
|
|
|
titleText: '',
|
|
|
|
showSearch: false,
|
|
|
|
render: noop,
|
2016-12-16 17:59:52 +08:00
|
|
|
lazy: {},
|
2016-05-12 11:09:27 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2016-10-08 10:42:13 +08:00
|
|
|
timer: number;
|
2017-05-02 15:47:51 +08:00
|
|
|
triggerScrollTimer: number;
|
2016-08-08 14:42:29 +08:00
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
constructor(props: TransferListProps) {
|
2015-11-25 23:17:06 +08:00
|
|
|
super(props);
|
2015-12-29 15:48:22 +08:00
|
|
|
this.state = {
|
|
|
|
mounted: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-10-09 21:03:00 +08:00
|
|
|
this.timer = window.setTimeout(() => {
|
2015-12-29 15:48:22 +08:00
|
|
|
this.setState({
|
|
|
|
mounted: true,
|
|
|
|
});
|
|
|
|
}, 0);
|
2015-11-25 23:17:06 +08:00
|
|
|
}
|
|
|
|
|
2016-06-28 20:01:02 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
clearTimeout(this.timer);
|
2017-05-02 15:47:51 +08:00
|
|
|
clearTimeout(this.triggerScrollTimer);
|
2016-06-28 20:01:02 +08:00
|
|
|
}
|
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
shouldComponentUpdate(...args: any[]) {
|
2016-06-21 17:41:39 +08:00
|
|
|
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
getCheckStatus(filteredDataSource: TransferItem[]) {
|
2016-07-15 15:07:31 +08:00
|
|
|
const { checkedKeys } = this.props;
|
|
|
|
if (checkedKeys.length === 0) {
|
|
|
|
return 'none';
|
|
|
|
} else if (filteredDataSource.every(item => checkedKeys.indexOf(item.key) >= 0)) {
|
|
|
|
return 'all';
|
|
|
|
}
|
|
|
|
return 'part';
|
2015-11-25 23:17:06 +08:00
|
|
|
}
|
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
handleSelect = (selectedItem: TransferItem) => {
|
2015-12-21 15:29:02 +08:00
|
|
|
const { checkedKeys } = this.props;
|
|
|
|
const result = checkedKeys.some((key) => key === selectedItem.key);
|
|
|
|
this.props.handleSelect(selectedItem, !result);
|
2015-11-25 23:17:06 +08:00
|
|
|
}
|
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
handleFilter = (e: React.ChangeEvent<HTMLInputElement>) => {
|
2015-11-26 16:07:11 +08:00
|
|
|
this.props.handleFilter(e);
|
2017-05-02 15:47:51 +08:00
|
|
|
if (!e.target.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Manually trigger scroll event for lazy search bug
|
|
|
|
// https://github.com/ant-design/ant-design/issues/5631
|
2017-10-09 21:03:00 +08:00
|
|
|
this.triggerScrollTimer = window.setTimeout(() => {
|
2017-11-17 14:38:54 +08:00
|
|
|
const listNode = ReactDOM.findDOMNode(this).querySelectorAll('.ant-transfer-list-content')[0];
|
2017-05-02 15:47:51 +08:00
|
|
|
if (listNode) {
|
|
|
|
triggerEvent(listNode, 'scroll');
|
|
|
|
}
|
|
|
|
}, 0);
|
2015-11-26 16:07:11 +08:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:50:44 +08:00
|
|
|
handleClear = () => {
|
2015-12-17 16:08:16 +08:00
|
|
|
this.props.handleClear();
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
matchFilter = (text: string, item: TransferItem) => {
|
2017-01-27 17:08:12 +08:00
|
|
|
const { filter, filterOption } = this.props;
|
|
|
|
if (filterOption) {
|
|
|
|
return filterOption(filter, item);
|
|
|
|
}
|
|
|
|
return text.indexOf(filter) >= 0;
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
renderItem = (item: TransferItem) => {
|
2017-01-27 17:08:12 +08:00
|
|
|
const { render = noop } = this.props;
|
|
|
|
const renderResult = render(item);
|
|
|
|
const isRenderResultPlain = isRenderResultPlainObject(renderResult);
|
|
|
|
return {
|
|
|
|
renderedText: isRenderResultPlain ? renderResult.value : renderResult,
|
|
|
|
renderedEl: isRenderResultPlain ? renderResult.label : renderResult,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-11-25 23:17:06 +08:00
|
|
|
render() {
|
2017-03-01 11:07:19 +08:00
|
|
|
const {
|
|
|
|
prefixCls, dataSource, titleText, checkedKeys, lazy,
|
|
|
|
body = noop, footer = noop, showSearch, style, filter,
|
2017-03-17 15:23:25 +08:00
|
|
|
searchPlaceholder, notFoundContent, itemUnit, itemsUnit, onScroll,
|
2017-03-01 11:07:19 +08:00
|
|
|
} = this.props;
|
2016-03-07 14:52:14 +08:00
|
|
|
|
2015-12-16 23:02:49 +08:00
|
|
|
// Custom Layout
|
2017-07-03 16:57:11 +08:00
|
|
|
const footerDom = footer({ ...this.props });
|
|
|
|
const bodyDom = body({ ...this.props });
|
2015-12-15 17:08:34 +08:00
|
|
|
|
2016-11-30 10:20:23 +08:00
|
|
|
const listCls = classNames(prefixCls, {
|
2016-02-17 18:04:42 +08:00
|
|
|
[`${prefixCls}-with-footer`]: !!footerDom,
|
2015-12-24 17:44:54 +08:00
|
|
|
});
|
|
|
|
|
2016-10-24 16:30:38 +08:00
|
|
|
const filteredDataSource: TransferItem[] = [];
|
2017-01-27 17:08:12 +08:00
|
|
|
const totalDataSource: TransferItem[] = [];
|
2016-07-15 15:07:31 +08:00
|
|
|
|
2016-09-19 17:52:20 +08:00
|
|
|
const showItems = dataSource.map((item) => {
|
2017-01-27 17:08:12 +08:00
|
|
|
const { renderedText, renderedEl } = this.renderItem(item);
|
|
|
|
if (filter && filter.trim() && !this.matchFilter(renderedText, item)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// all show items
|
|
|
|
totalDataSource.push(item);
|
2016-09-19 17:52:20 +08:00
|
|
|
if (!item.disabled) {
|
2017-01-27 17:08:12 +08:00
|
|
|
// response to checkAll items
|
2016-09-19 17:52:20 +08:00
|
|
|
filteredDataSource.push(item);
|
2016-07-07 18:42:59 +08:00
|
|
|
}
|
2017-01-27 17:08:12 +08:00
|
|
|
|
2016-11-04 16:52:57 +08:00
|
|
|
const checked = checkedKeys.indexOf(item.key) >= 0;
|
2016-02-22 10:52:30 +08:00
|
|
|
return (
|
2016-11-04 16:52:57 +08:00
|
|
|
<Item
|
|
|
|
key={item.key}
|
|
|
|
item={item}
|
|
|
|
lazy={lazy}
|
2017-01-27 17:08:12 +08:00
|
|
|
renderedText={renderedText}
|
|
|
|
renderedEl={renderedEl}
|
2016-11-04 16:52:57 +08:00
|
|
|
checked={checked}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
onClick={this.handleSelect}
|
|
|
|
/>
|
2016-02-22 10:52:30 +08:00
|
|
|
);
|
2016-09-19 17:52:20 +08:00
|
|
|
});
|
2016-01-13 16:28:36 +08:00
|
|
|
|
2017-03-17 15:23:25 +08:00
|
|
|
const unit = dataSource.length > 1 ? itemsUnit : itemUnit;
|
2016-03-07 14:52:14 +08:00
|
|
|
|
2016-11-28 19:14:43 +08:00
|
|
|
const search = showSearch ? (
|
|
|
|
<div className={`${prefixCls}-body-search-wrapper`}>
|
|
|
|
<Search
|
|
|
|
prefixCls={`${prefixCls}-search`}
|
|
|
|
onChange={this.handleFilter}
|
|
|
|
handleClear={this.handleClear}
|
2017-03-17 15:23:25 +08:00
|
|
|
placeholder={searchPlaceholder}
|
2016-11-28 19:14:43 +08:00
|
|
|
value={filter}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null;
|
2016-07-15 15:07:31 +08:00
|
|
|
|
2016-11-25 12:03:39 +08:00
|
|
|
const listBody = bodyDom || (
|
|
|
|
<div className={showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`}>
|
2016-11-28 19:14:43 +08:00
|
|
|
{search}
|
2016-11-25 12:03:39 +08:00
|
|
|
<Animate
|
|
|
|
component="ul"
|
2017-03-01 11:07:19 +08:00
|
|
|
componentProps={{ onScroll }}
|
2016-11-25 12:03:39 +08:00
|
|
|
className={`${prefixCls}-content`}
|
|
|
|
transitionName={this.state.mounted ? `${prefixCls}-content-item-highlight` : ''}
|
|
|
|
transitionLeave={false}
|
|
|
|
>
|
|
|
|
{showItems}
|
|
|
|
</Animate>
|
|
|
|
<div className={`${prefixCls}-body-not-found`}>
|
2017-03-17 15:23:25 +08:00
|
|
|
{notFoundContent}
|
2016-11-25 12:03:39 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
const listFooter = footerDom ? (
|
|
|
|
<div className={`${prefixCls}-footer`}>
|
|
|
|
{footerDom}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
|
2016-12-16 17:59:52 +08:00
|
|
|
const checkStatus = this.getCheckStatus(filteredDataSource);
|
|
|
|
const checkedAll = checkStatus === 'all';
|
|
|
|
const checkAllCheckbox = (
|
|
|
|
<Checkbox
|
|
|
|
ref="checkbox"
|
|
|
|
checked={checkedAll}
|
|
|
|
indeterminate={checkStatus === 'part'}
|
|
|
|
onChange={() => this.props.handleSelectAll(filteredDataSource, checkedAll)}
|
|
|
|
/>
|
|
|
|
);
|
2016-11-28 19:14:43 +08:00
|
|
|
|
2016-01-07 14:21:29 +08:00
|
|
|
return (
|
2016-07-08 17:17:57 +08:00
|
|
|
<div className={listCls} style={style}>
|
2016-01-07 14:21:29 +08:00
|
|
|
<div className={`${prefixCls}-header`}>
|
2016-12-16 17:59:52 +08:00
|
|
|
{checkAllCheckbox}
|
2016-03-07 14:52:14 +08:00
|
|
|
<span className={`${prefixCls}-header-selected`}>
|
|
|
|
<span>
|
2017-01-27 17:08:12 +08:00
|
|
|
{(checkedKeys.length > 0 ? `${checkedKeys.length}/` : '') + totalDataSource.length} {unit}
|
2016-03-07 14:52:14 +08:00
|
|
|
</span>
|
|
|
|
<span className={`${prefixCls}-header-title`}>
|
|
|
|
{titleText}
|
|
|
|
</span>
|
|
|
|
</span>
|
2016-01-07 14:21:29 +08:00
|
|
|
</div>
|
2016-11-25 12:03:39 +08:00
|
|
|
{listBody}
|
|
|
|
{listFooter}
|
2016-01-07 14:21:29 +08:00
|
|
|
</div>
|
|
|
|
);
|
2015-11-25 23:17:06 +08:00
|
|
|
}
|
|
|
|
}
|