2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2019-05-07 17:10:42 +08:00
|
|
|
import omit from 'omit.js';
|
2015-12-24 17:44:54 +08:00
|
|
|
import classNames from 'classnames';
|
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';
|
2020-02-04 10:16:23 +08:00
|
|
|
import {
|
|
|
|
TransferItem,
|
|
|
|
TransferDirection,
|
|
|
|
RenderResult,
|
|
|
|
RenderResultObject,
|
|
|
|
SelectAllLabel,
|
|
|
|
} from './index';
|
2016-12-16 17:59:52 +08:00
|
|
|
import Search from './search';
|
2019-05-07 17:10:42 +08:00
|
|
|
import defaultRenderList, { TransferListBodyProps, OmitProps } from './renderListBody';
|
2015-12-24 17:44:54 +08:00
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
const defaultRender = () => null;
|
2015-11-25 23:17:06 +08:00
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
function isRenderResultPlainObject(result: RenderResult) {
|
2018-12-07 16:17:45 +08:00
|
|
|
return (
|
|
|
|
result &&
|
|
|
|
!React.isValidElement(result) &&
|
|
|
|
Object.prototype.toString.call(result) === '[object Object]'
|
|
|
|
);
|
2017-01-27 17:08:12 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
export interface RenderedItem {
|
|
|
|
renderedText: string;
|
|
|
|
renderedEl: React.ReactNode;
|
|
|
|
item: TransferItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
type RenderListFunction = (props: TransferListBodyProps) => React.ReactNode;
|
|
|
|
|
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[];
|
2019-05-07 17:10:42 +08:00
|
|
|
filterOption?: (filterText: string, item: TransferItem) => boolean;
|
2016-07-13 11:14:24 +08:00
|
|
|
style?: React.CSSProperties;
|
2017-03-17 15:23:25 +08:00
|
|
|
checkedKeys: string[];
|
2019-05-07 17:10:42 +08:00
|
|
|
handleFilter: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
|
|
onItemSelect: (key: string, check: boolean) => void;
|
|
|
|
onItemSelectAll: (dataSource: string[], checkAll: boolean) => void;
|
2016-10-24 16:30:38 +08:00
|
|
|
handleClear: () => void;
|
2019-05-07 17:10:42 +08:00
|
|
|
render?: (item: TransferItem) => RenderResult;
|
2017-03-17 15:23:25 +08:00
|
|
|
showSearch?: boolean;
|
|
|
|
searchPlaceholder: string;
|
|
|
|
notFoundContent: React.ReactNode;
|
|
|
|
itemUnit: string;
|
|
|
|
itemsUnit: string;
|
2019-05-07 17:10:42 +08:00
|
|
|
renderList?: RenderListFunction;
|
2018-09-30 16:07:01 +08:00
|
|
|
footer?: (props: TransferListProps) => React.ReactNode;
|
2019-12-23 18:33:08 +08:00
|
|
|
onScroll: (e: React.UIEvent<HTMLUListElement>) => void;
|
2018-09-15 15:18:59 +08:00
|
|
|
disabled?: boolean;
|
2019-05-07 17:10:42 +08:00
|
|
|
direction: TransferDirection;
|
|
|
|
showSelectAll?: boolean;
|
2020-02-04 10:16:23 +08:00
|
|
|
selectAllLabel?: SelectAllLabel;
|
2019-05-07 17:10:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
interface TransferListState {
|
|
|
|
/** Filter input value */
|
|
|
|
filterValue: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderListNode(renderList: RenderListFunction | undefined, props: TransferListBodyProps) {
|
|
|
|
let bodyContent: React.ReactNode = renderList ? renderList(props) : null;
|
|
|
|
const customize: boolean = !!bodyContent;
|
|
|
|
if (!customize) {
|
|
|
|
bodyContent = defaultRenderList(props);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
customize,
|
|
|
|
bodyContent,
|
|
|
|
};
|
2016-08-08 14:42:29 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
export default class TransferList extends React.Component<TransferListProps, TransferListState> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
dataSource: [],
|
|
|
|
titleText: '',
|
|
|
|
showSearch: false,
|
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;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
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 = {
|
2019-05-07 17:10:42 +08:00
|
|
|
filterValue: '',
|
2015-12-29 15:48:22 +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);
|
|
|
|
}
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
clearTimeout(this.triggerScrollTimer);
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
getCheckStatus(filteredItems: TransferItem[]) {
|
2016-07-15 15:07:31 +08:00
|
|
|
const { checkedKeys } = this.props;
|
|
|
|
if (checkedKeys.length === 0) {
|
|
|
|
return 'none';
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
if (filteredItems.every(item => checkedKeys.indexOf(item.key) >= 0 || !!item.disabled)) {
|
2016-07-15 15:07:31 +08:00
|
|
|
return 'all';
|
|
|
|
}
|
|
|
|
return 'part';
|
2015-11-25 23:17:06 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 12:27:35 +08:00
|
|
|
getFilteredItems(
|
|
|
|
dataSource: TransferItem[],
|
|
|
|
filterValue: string,
|
|
|
|
): { filteredItems: TransferItem[]; filteredRenderItems: RenderedItem[] } {
|
|
|
|
const filteredItems: TransferItem[] = [];
|
|
|
|
const filteredRenderItems: RenderedItem[] = [];
|
|
|
|
|
|
|
|
dataSource.forEach(item => {
|
|
|
|
const renderedItem = this.renderItem(item);
|
|
|
|
const { renderedText } = renderedItem;
|
|
|
|
|
|
|
|
// Filter skip
|
|
|
|
if (filterValue && filterValue.trim() && !this.matchFilter(renderedText, item)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredItems.push(item);
|
|
|
|
filteredRenderItems.push(renderedItem);
|
|
|
|
});
|
|
|
|
|
|
|
|
return { filteredItems, filteredRenderItems };
|
|
|
|
}
|
|
|
|
|
|
|
|
getListBody(
|
|
|
|
prefixCls: string,
|
|
|
|
searchPlaceholder: string,
|
|
|
|
filterValue: string,
|
|
|
|
filteredItems: TransferItem[],
|
|
|
|
notFoundContent: React.ReactNode,
|
|
|
|
filteredRenderItems: RenderedItem[],
|
|
|
|
checkedKeys: string[],
|
|
|
|
renderList?: RenderListFunction,
|
|
|
|
showSearch?: boolean,
|
|
|
|
disabled?: boolean,
|
|
|
|
): React.ReactNode {
|
|
|
|
const search = showSearch ? (
|
|
|
|
<div className={`${prefixCls}-body-search-wrapper`}>
|
|
|
|
<Search
|
|
|
|
prefixCls={`${prefixCls}-search`}
|
|
|
|
onChange={this.handleFilter}
|
|
|
|
handleClear={this.handleClear}
|
|
|
|
placeholder={searchPlaceholder}
|
|
|
|
value={filterValue}
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
|
2019-07-09 11:46:21 +08:00
|
|
|
const { bodyContent, customize } = renderListNode(renderList, {
|
|
|
|
...omit(this.props, OmitProps),
|
|
|
|
filteredItems,
|
|
|
|
filteredRenderItems,
|
|
|
|
selectedKeys: checkedKeys,
|
|
|
|
});
|
2019-05-09 12:27:35 +08:00
|
|
|
|
2019-07-09 11:46:21 +08:00
|
|
|
let bodyNode: React.ReactNode;
|
|
|
|
// We should wrap customize list body in a classNamed div to use flex layout.
|
|
|
|
if (customize) {
|
|
|
|
bodyNode = <div className={`${prefixCls}-body-customize-wrapper`}>{bodyContent}</div>;
|
|
|
|
} else {
|
|
|
|
bodyNode = filteredItems.length ? (
|
|
|
|
bodyContent
|
|
|
|
) : (
|
|
|
|
<div className={`${prefixCls}-body-not-found`}>{notFoundContent}</div>
|
2019-05-09 12:27:35 +08:00
|
|
|
);
|
|
|
|
}
|
2019-07-09 11:46:21 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`,
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{search}
|
|
|
|
{bodyNode}
|
|
|
|
</div>
|
|
|
|
);
|
2019-05-09 12:27:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getCheckBox(
|
|
|
|
filteredItems: TransferItem[],
|
|
|
|
onItemSelectAll: (dataSource: string[], checkAll: boolean) => void,
|
|
|
|
showSelectAll?: boolean,
|
|
|
|
disabled?: boolean,
|
|
|
|
): false | JSX.Element {
|
|
|
|
const checkStatus = this.getCheckStatus(filteredItems);
|
|
|
|
const checkedAll = checkStatus === 'all';
|
|
|
|
const checkAllCheckbox = showSelectAll !== false && (
|
|
|
|
<Checkbox
|
|
|
|
disabled={disabled}
|
|
|
|
checked={checkedAll}
|
|
|
|
indeterminate={checkStatus === 'part'}
|
|
|
|
onChange={() => {
|
|
|
|
// Only select enabled items
|
|
|
|
onItemSelectAll(
|
|
|
|
filteredItems.filter(item => !item.disabled).map(({ key }) => key),
|
|
|
|
!checkedAll,
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return checkAllCheckbox;
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
handleFilter = (e: React.ChangeEvent<HTMLInputElement>) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { handleFilter } = this.props;
|
2019-05-07 17:10:42 +08:00
|
|
|
const {
|
|
|
|
target: { value: filterValue },
|
|
|
|
} = e;
|
|
|
|
this.setState({ filterValue });
|
2019-08-05 18:38:10 +08:00
|
|
|
handleFilter(e);
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2015-11-26 16:07:11 +08:00
|
|
|
|
2016-03-23 19:50:44 +08:00
|
|
|
handleClear = () => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { handleClear } = this.props;
|
2019-05-07 17:10:42 +08:00
|
|
|
this.setState({ filterValue: '' });
|
2019-08-05 18:38:10 +08:00
|
|
|
handleClear();
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2015-12-17 16:08:16 +08:00
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
matchFilter = (text: string, item: TransferItem) => {
|
2019-05-07 17:10:42 +08:00
|
|
|
const { filterValue } = this.state;
|
|
|
|
const { filterOption } = this.props;
|
2017-01-27 17:08:12 +08:00
|
|
|
if (filterOption) {
|
2019-05-07 17:10:42 +08:00
|
|
|
return filterOption(filterValue, item);
|
2017-01-27 17:08:12 +08:00
|
|
|
}
|
2019-05-07 17:10:42 +08:00
|
|
|
return text.indexOf(filterValue) >= 0;
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2017-01-27 17:08:12 +08:00
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
renderItem = (item: TransferItem): RenderedItem => {
|
|
|
|
const { render = defaultRender } = this.props;
|
|
|
|
const renderResult: RenderResult = render(item);
|
2017-01-27 17:08:12 +08:00
|
|
|
const isRenderResultPlain = isRenderResultPlainObject(renderResult);
|
|
|
|
return {
|
2019-05-07 17:10:42 +08:00
|
|
|
renderedText: isRenderResultPlain
|
|
|
|
? (renderResult as RenderResultObject).value
|
|
|
|
: (renderResult as string),
|
|
|
|
renderedEl: isRenderResultPlain ? (renderResult as RenderResultObject).label : renderResult,
|
|
|
|
item,
|
2017-01-27 17:08:12 +08:00
|
|
|
};
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2017-01-27 17:08:12 +08:00
|
|
|
|
2020-02-04 10:16:23 +08:00
|
|
|
getSelectAllLabel = (selectedCount: number, totalCount: number): React.ReactNode => {
|
|
|
|
const { itemsUnit, itemUnit, selectAllLabel } = this.props;
|
|
|
|
if (selectAllLabel) {
|
|
|
|
return typeof selectAllLabel === 'function'
|
|
|
|
? selectAllLabel({ selectedCount, totalCount })
|
|
|
|
: selectAllLabel;
|
|
|
|
}
|
|
|
|
const unit = totalCount > 1 ? itemsUnit : itemUnit;
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{(selectedCount > 0 ? `${selectedCount}/` : '') + totalCount} {unit}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-11-25 23:17:06 +08:00
|
|
|
render() {
|
2019-05-07 17:10:42 +08:00
|
|
|
const { filterValue } = this.state;
|
2017-03-01 11:07:19 +08:00
|
|
|
const {
|
2018-12-07 16:17:45 +08:00
|
|
|
prefixCls,
|
|
|
|
dataSource,
|
|
|
|
titleText,
|
|
|
|
checkedKeys,
|
|
|
|
disabled,
|
|
|
|
footer,
|
|
|
|
showSearch,
|
|
|
|
style,
|
|
|
|
searchPlaceholder,
|
|
|
|
notFoundContent,
|
2019-05-07 17:10:42 +08:00
|
|
|
renderList,
|
|
|
|
onItemSelectAll,
|
|
|
|
showSelectAll,
|
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
|
2018-09-30 16:07:01 +08:00
|
|
|
const footerDom = footer && footer(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
|
|
|
});
|
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
// ====================== Get filtered, checked item list ======================
|
2017-01-27 17:08:12 +08:00
|
|
|
|
2019-05-09 12:27:35 +08:00
|
|
|
const { filteredItems, filteredRenderItems } = this.getFilteredItems(dataSource, filterValue);
|
2016-01-13 16:28:36 +08:00
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
// ================================= List Body =================================
|
2016-03-07 14:52:14 +08:00
|
|
|
|
2019-05-09 12:27:35 +08:00
|
|
|
const listBody = this.getListBody(
|
|
|
|
prefixCls,
|
|
|
|
searchPlaceholder,
|
|
|
|
filterValue,
|
|
|
|
filteredItems,
|
|
|
|
notFoundContent,
|
|
|
|
filteredRenderItems,
|
|
|
|
checkedKeys,
|
|
|
|
renderList,
|
|
|
|
showSearch,
|
|
|
|
disabled,
|
2018-12-21 11:41:11 +08:00
|
|
|
);
|
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
// ================================ List Footer ================================
|
2018-12-07 16:17:45 +08:00
|
|
|
const listFooter = footerDom ? <div className={`${prefixCls}-footer`}>{footerDom}</div> : null;
|
2016-11-25 12:03:39 +08:00
|
|
|
|
2019-05-09 12:27:35 +08:00
|
|
|
const checkAllCheckbox = this.getCheckBox(
|
|
|
|
filteredItems,
|
|
|
|
onItemSelectAll,
|
|
|
|
showSelectAll,
|
|
|
|
disabled,
|
2016-12-16 17:59:52 +08:00
|
|
|
);
|
2016-11-28 19:14:43 +08:00
|
|
|
|
2019-05-07 17:10:42 +08:00
|
|
|
// ================================== Render ===================================
|
2016-01-07 14:21:29 +08:00
|
|
|
return (
|
2016-07-08 17:17:57 +08:00
|
|
|
<div className={listCls} style={style}>
|
2019-05-07 17:10:42 +08:00
|
|
|
{/* Header */}
|
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`}>
|
2020-02-04 10:16:23 +08:00
|
|
|
<span>{this.getSelectAllLabel(checkedKeys.length, filteredItems.length)}</span>
|
2018-12-07 16:17:45 +08:00
|
|
|
<span className={`${prefixCls}-header-title`}>{titleText}</span>
|
2016-03-07 14:52:14 +08:00
|
|
|
</span>
|
2016-01-07 14:21:29 +08:00
|
|
|
</div>
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
{/* Body */}
|
2016-11-25 12:03:39 +08:00
|
|
|
{listBody}
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
{/* Footer */}
|
2016-11-25 12:03:39 +08:00
|
|
|
{listFooter}
|
2016-01-07 14:21:29 +08:00
|
|
|
</div>
|
|
|
|
);
|
2015-11-25 23:17:06 +08:00
|
|
|
}
|
|
|
|
}
|