ant-design/components/table/filterDropdown.tsx

232 lines
7.2 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Menu, { SubMenu, Item as MenuItem } from 'rc-menu';
2017-02-25 02:09:18 +08:00
import closest from 'dom-closest';
import classNames from 'classnames';
2015-08-17 18:14:39 +08:00
import Dropdown from '../dropdown';
2015-11-20 11:05:34 +08:00
import Icon from '../icon';
2015-12-02 11:36:59 +08:00
import Checkbox from '../checkbox';
import Radio from '../radio';
2016-10-24 16:30:38 +08:00
import FilterDropdownMenuWrapper from './FilterDropdownMenuWrapper';
2017-11-21 13:48:37 +08:00
import { FilterMenuProps, FilterMenuState, ColumnProps, ColumnFilterItem } from './interface';
2016-07-26 16:46:31 +08:00
2017-11-21 13:48:37 +08:00
export default class FilterMenu<T> extends React.Component<FilterMenuProps<T>, FilterMenuState> {
2016-04-13 17:23:14 +08:00
static defaultProps = {
handleFilter() {},
column: {},
2016-07-13 11:14:24 +08:00
};
2016-04-13 17:23:14 +08:00
neverShown: boolean;
2017-11-21 13:48:37 +08:00
constructor(props: FilterMenuProps<T>) {
2016-04-13 17:23:14 +08:00
super(props);
const visible = ('filterDropdownVisible' in props.column) ?
props.column.filterDropdownVisible : false;
2016-04-13 17:23:14 +08:00
this.state = {
selectedKeys: props.selectedKeys,
keyPathOfSelectedItem: {}, // 记录所有有选中子菜单的祖先菜单
visible,
2015-07-15 12:16:28 +08:00
};
2016-04-13 17:23:14 +08:00
}
componentDidMount() {
const { column } = this.props;
this.setNeverShown(column);
}
2017-11-21 13:48:37 +08:00
componentWillReceiveProps(nextProps: FilterMenuProps<T>) {
const { column } = nextProps;
this.setNeverShown(column);
2017-01-24 15:10:04 +08:00
const newState = {} as {
selectedKeys: string[];
visible: boolean;
};
if ('selectedKeys' in nextProps) {
newState.selectedKeys = nextProps.selectedKeys;
}
if ('filterDropdownVisible' in column) {
2017-11-21 13:48:37 +08:00
newState.visible = column.filterDropdownVisible as boolean;
}
2016-11-18 11:31:02 +08:00
if (Object.keys(newState).length > 0) {
this.setState(newState);
}
2016-04-13 17:23:14 +08:00
}
2017-11-21 13:48:37 +08:00
setNeverShown = (column: ColumnProps<T>) => {
const rootNode = ReactDOM.findDOMNode(this);
const filterBelongToScrollBody = !!closest(rootNode, `.ant-table-scroll`);
if (filterBelongToScrollBody) {
// When fixed column have filters, there will be two dropdown menus
// Filter dropdown menu inside scroll body should never be shown
// To fix https://github.com/ant-design/ant-design/issues/5010 and
// https://github.com/ant-design/ant-design/issues/7909
this.neverShown = !!column.fixed;
}
}
2017-11-21 13:48:37 +08:00
setSelectedKeys = ({ selectedKeys }: { selectedKeys: string[] }) => {
2015-11-03 17:21:51 +08:00
this.setState({ selectedKeys });
2016-04-13 17:23:14 +08:00
}
2017-11-21 13:48:37 +08:00
setVisible(visible: boolean) {
const { column } = this.props;
if (!('filterDropdownVisible' in column)) {
this.setState({ visible });
}
if (column.onFilterDropdownVisibleChange) {
column.onFilterDropdownVisibleChange(visible);
}
}
2016-04-13 17:23:14 +08:00
handleClearFilters = () => {
2015-07-15 12:16:28 +08:00
this.setState({
2016-05-11 09:32:33 +08:00
selectedKeys: [],
2015-08-12 12:47:04 +08:00
}, this.handleConfirm);
2016-04-13 17:23:14 +08:00
}
handleConfirm = () => {
this.setVisible(false);
this.confirmFilter();
2016-04-13 17:23:14 +08:00
}
2017-11-21 13:48:37 +08:00
onVisibleChange = (visible: boolean) => {
this.setVisible(visible);
2015-12-02 11:36:59 +08:00
if (!visible) {
this.confirmFilter();
}
}
confirmFilter() {
if (this.state.selectedKeys !== this.props.selectedKeys) {
2015-12-02 11:36:59 +08:00
this.props.confirmFilter(this.props.column, this.state.selectedKeys);
}
2016-04-13 17:23:14 +08:00
}
2017-11-21 13:48:37 +08:00
renderMenuItem(item: ColumnFilterItem) {
const { column } = this.props;
const multiple = ('filterMultiple' in column) ? column.filterMultiple : true;
const input = multiple ? (
<Checkbox checked={this.state.selectedKeys.indexOf(item.value.toString()) >= 0} />
) : (
<Radio checked={this.state.selectedKeys.indexOf(item.value.toString()) >= 0} />
);
return (
<MenuItem key={item.value}>
{input}
2016-04-15 23:34:40 +08:00
<span>{item.text}</span>
</MenuItem>
);
2016-04-13 17:23:14 +08:00
}
hasSubMenu() {
const { column: { filters = [] } } = this.props;
return filters.some(item => !!(item.children && item.children.length > 0));
}
2017-11-21 13:48:37 +08:00
renderMenus(items: ColumnFilterItem[]): React.ReactElement<any>[] {
2016-06-01 11:37:13 +08:00
return items.map(item => {
if (item.children && item.children.length > 0) {
2016-06-01 11:37:13 +08:00
const { keyPathOfSelectedItem } = this.state;
const containSelected = Object.keys(keyPathOfSelectedItem).some(
key => keyPathOfSelectedItem[key].indexOf(item.value) >= 0,
2016-06-01 11:37:13 +08:00
);
const subMenuCls = containSelected ? `${this.props.dropdownPrefixCls}-submenu-contain-selected` : '';
return (
2016-04-12 19:43:10 +08:00
<SubMenu title={item.text} className={subMenuCls} key={item.value.toString()}>
{this.renderMenus(item.children)}
</SubMenu>
);
}
return this.renderMenuItem(item);
2015-07-15 12:16:28 +08:00
});
2016-04-13 17:23:14 +08:00
}
2017-11-21 13:48:37 +08:00
handleMenuItemClick = (info: { keyPath: string, key: string }) => {
if (info.keyPath.length <= 1) {
return;
}
const keyPathOfSelectedItem = this.state.keyPathOfSelectedItem;
if (this.state.selectedKeys.indexOf(info.key) >= 0) {
// deselect SubMenu child
delete keyPathOfSelectedItem[info.key];
} else {
// select SubMenu child
keyPathOfSelectedItem[info.key] = info.keyPath;
}
this.setState({ keyPathOfSelectedItem });
2016-04-13 17:23:14 +08:00
}
renderFilterIcon = () => {
const { column, locale, prefixCls } = this.props;
const filterIcon = column.filterIcon as any;
const dropdownSelectedClass = this.props.selectedKeys.length > 0 ? `${prefixCls}-selected` : '';
return filterIcon ? React.cloneElement(filterIcon as any, {
title: locale.filterTitle,
className: classNames(filterIcon.className, {
[`${prefixCls}-icon`]: true,
}),
}) : <Icon title={locale.filterTitle} type="filter" className={dropdownSelectedClass} />;
}
2015-07-15 12:16:28 +08:00
render() {
const { column, locale, prefixCls, dropdownPrefixCls, getPopupContainer } = this.props;
// default multiple selection in filter dropdown
const multiple = ('filterMultiple' in column) ? column.filterMultiple : true;
const dropdownMenuClass = classNames({
[`${dropdownPrefixCls}-menu-without-submenu`]: !this.hasSubMenu(),
});
const menus = column.filterDropdown ? (
<FilterDropdownMenuWrapper>
{column.filterDropdown}
</FilterDropdownMenuWrapper>
) : (
<FilterDropdownMenuWrapper className={`${prefixCls}-dropdown`}>
2016-06-20 16:25:35 +08:00
<Menu
multiple={multiple}
2016-03-15 19:31:56 +08:00
onClick={this.handleMenuItemClick}
prefixCls={`${dropdownPrefixCls}-menu`}
className={dropdownMenuClass}
onSelect={this.setSelectedKeys}
onDeselect={this.setSelectedKeys}
selectedKeys={this.state.selectedKeys}
getPopupContainer={(triggerNode: HTMLElement) => {
return triggerNode.parentNode;
}}
>
2017-11-21 13:48:37 +08:00
{this.renderMenus(column.filters!)}
</Menu>
<div className={`${prefixCls}-dropdown-btns`}>
2016-06-20 16:25:35 +08:00
<a
className={`${prefixCls}-dropdown-link confirm`}
onClick={this.handleConfirm}
>
{locale.filterConfirm}
</a>
2016-06-20 16:25:35 +08:00
<a
className={`${prefixCls}-dropdown-link clear`}
onClick={this.handleClearFilters}
>
{locale.filterReset}
</a>
</div>
2016-07-26 16:46:31 +08:00
</FilterDropdownMenuWrapper>
);
2015-08-17 18:14:39 +08:00
return (
2016-06-20 16:25:35 +08:00
<Dropdown
trigger={['click']}
overlay={menus}
visible={this.neverShown ? false : this.state.visible}
onVisibleChange={this.onVisibleChange}
getPopupContainer={getPopupContainer}
forceRender
>
{this.renderFilterIcon()}
</Dropdown>
);
2015-07-15 12:16:28 +08:00
}
2016-04-13 17:23:14 +08:00
}