ant-design/components/transfer/list.tsx

344 lines
9.2 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import omit from 'omit.js';
2015-12-24 17:44:54 +08:00
import classNames from 'classnames';
import Checkbox from '../checkbox';
import {
TransferItem,
TransferDirection,
RenderResult,
RenderResultObject,
SelectAllLabel,
} from './index';
import Search from './search';
import defaultRenderList, { TransferListBodyProps, OmitProps } from './renderListBody';
2015-12-24 17:44:54 +08:00
const defaultRender = () => null;
2015-11-25 23:17:06 +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]'
);
}
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;
titleText: string;
dataSource: TransferItem[];
filterOption?: (filterText: string, item: TransferItem) => boolean;
2016-07-13 11:14:24 +08:00
style?: React.CSSProperties;
checkedKeys: string[];
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;
render?: (item: TransferItem) => RenderResult;
showSearch?: boolean;
searchPlaceholder: string;
notFoundContent: React.ReactNode;
itemUnit: string;
itemsUnit: string;
renderList?: RenderListFunction;
2018-09-30 16:07:01 +08:00
footer?: (props: TransferListProps) => React.ReactNode;
onScroll: (e: React.UIEvent<HTMLUListElement>) => void;
2018-09-15 15:18:59 +08:00
disabled?: boolean;
direction: TransferDirection;
showSelectAll?: boolean;
selectAllLabel?: SelectAllLabel;
}
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
}
export default class TransferList extends React.PureComponent<
TransferListProps,
TransferListState
> {
static defaultProps = {
dataSource: [],
titleText: '',
showSearch: false,
};
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 = {
filterValue: '',
2015-12-29 15:48:22 +08:00
};
}
2019-08-05 18:38:10 +08:00
componentWillUnmount() {
clearTimeout(this.triggerScrollTimer);
}
getCheckStatus(filteredItems: TransferItem[]) {
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)) {
return 'all';
}
return 'part';
2015-11-25 23:17:06 +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;
const { bodyContent, customize } = renderListNode(renderList, {
...omit(this.props, OmitProps),
filteredItems,
filteredRenderItems,
selectedKeys: checkedKeys,
});
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>
);
}
return (
<div
className={classNames(
showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`,
)}
>
{search}
{bodyNode}
</div>
);
}
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;
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
handleClear = () => {
2019-08-05 18:38:10 +08:00
const { handleClear } = this.props;
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) => {
const { filterValue } = this.state;
const { filterOption } = this.props;
if (filterOption) {
return filterOption(filterValue, item);
}
return text.indexOf(filterValue) >= 0;
2018-12-07 16:17:45 +08:00
};
renderItem = (item: TransferItem): RenderedItem => {
const { render = defaultRender } = this.props;
const renderResult: RenderResult = render(item);
const isRenderResultPlain = isRenderResultPlainObject(renderResult);
return {
renderedText: isRenderResultPlain
? (renderResult as RenderResultObject).value
: (renderResult as string),
renderedEl: isRenderResultPlain ? (renderResult as RenderResultObject).label : renderResult,
item,
};
2018-12-07 16:17:45 +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() {
const { filterValue } = this.state;
const {
2018-12-07 16:17:45 +08:00
prefixCls,
dataSource,
titleText,
checkedKeys,
disabled,
footer,
showSearch,
style,
searchPlaceholder,
notFoundContent,
renderList,
onItemSelectAll,
showSelectAll,
} = this.props;
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
const listCls = classNames(prefixCls, {
[`${prefixCls}-with-footer`]: !!footerDom,
2015-12-24 17:44:54 +08:00
});
// ====================== Get filtered, checked item list ======================
const { filteredItems, filteredRenderItems } = this.getFilteredItems(dataSource, filterValue);
2016-01-13 16:28:36 +08:00
// ================================= List Body =================================
const listBody = this.getListBody(
prefixCls,
searchPlaceholder,
filterValue,
filteredItems,
notFoundContent,
filteredRenderItems,
checkedKeys,
renderList,
showSearch,
disabled,
);
// ================================ List Footer ================================
2018-12-07 16:17:45 +08:00
const listFooter = footerDom ? <div className={`${prefixCls}-footer`}>{footerDom}</div> : null;
const checkAllCheckbox = this.getCheckBox(
filteredItems,
onItemSelectAll,
showSelectAll,
disabled,
);
2016-11-28 19:14:43 +08:00
// ================================== Render ===================================
return (
2016-07-08 17:17:57 +08:00
<div className={listCls} style={style}>
{/* Header */}
<div className={`${prefixCls}-header`}>
{checkAllCheckbox}
<span className={`${prefixCls}-header-selected`}>
<span>{this.getSelectAllLabel(checkedKeys.length, filteredItems.length)}</span>
2018-12-07 16:17:45 +08:00
<span className={`${prefixCls}-header-title`}>{titleText}</span>
</span>
</div>
{/* Body */}
{listBody}
{/* Footer */}
{listFooter}
</div>
);
2015-11-25 23:17:06 +08:00
}
}