2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2016-02-02 16:48:16 +08:00
|
|
|
import Menu, { SubMenu, Item as MenuItem } from 'rc-menu';
|
2017-02-25 02:09:18 +08:00
|
|
|
import closest from 'dom-closest';
|
2017-02-21 15:54:14 +08:00
|
|
|
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';
|
2016-06-01 11:46:27 +08:00
|
|
|
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() {},
|
2016-11-14 17:53:02 +08:00
|
|
|
column: {},
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-04-13 17:23:14 +08:00
|
|
|
|
2017-02-25 01:36:45 +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);
|
|
|
|
|
2016-12-08 11:18:35 +08:00
|
|
|
const visible = ('filterDropdownVisible' in props.column) ?
|
|
|
|
props.column.filterDropdownVisible : false;
|
|
|
|
|
2016-04-13 17:23:14 +08:00
|
|
|
this.state = {
|
2016-06-20 16:36:37 +08:00
|
|
|
selectedKeys: props.selectedKeys,
|
2016-01-10 21:10:10 +08:00
|
|
|
keyPathOfSelectedItem: {}, // 记录所有有选中子菜单的祖先菜单
|
2016-12-08 11:18:35 +08:00
|
|
|
visible,
|
2015-07-15 12:16:28 +08:00
|
|
|
};
|
2016-04-13 17:23:14 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 01:36:45 +08:00
|
|
|
componentDidMount() {
|
|
|
|
const { column } = this.props;
|
2017-10-22 15:54:12 +08:00
|
|
|
this.setNeverShown(column);
|
2017-02-25 01:36:45 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
componentWillReceiveProps(nextProps: FilterMenuProps<T>) {
|
2016-11-14 17:53:02 +08:00
|
|
|
const { column } = nextProps;
|
2017-10-22 15:54:12 +08:00
|
|
|
this.setNeverShown(column);
|
2017-01-24 15:10:04 +08:00
|
|
|
const newState = {} as {
|
|
|
|
selectedKeys: string[];
|
|
|
|
visible: boolean;
|
|
|
|
};
|
2016-11-14 17:53:02 +08:00
|
|
|
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-14 17:53:02 +08:00
|
|
|
}
|
2016-11-18 11:31:02 +08:00
|
|
|
if (Object.keys(newState).length > 0) {
|
2016-11-14 17:53:02 +08:00
|
|
|
this.setState(newState);
|
|
|
|
}
|
2016-04-13 17:23:14 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
setNeverShown = (column: ColumnProps<T>) => {
|
2017-10-22 15:54:12 +08:00
|
|
|
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) {
|
2016-11-14 17:53:02 +08:00
|
|
|
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 = () => {
|
2016-11-14 17:53:02 +08:00
|
|
|
this.setVisible(false);
|
2016-07-04 12:23:43 +08:00
|
|
|
this.confirmFilter();
|
2016-04-13 17:23:14 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
onVisibleChange = (visible: boolean) => {
|
2016-11-14 17:53:02 +08:00
|
|
|
this.setVisible(visible);
|
2015-12-02 11:36:59 +08:00
|
|
|
if (!visible) {
|
2016-07-04 12:23:43 +08:00
|
|
|
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) {
|
2016-06-01 11:46:27 +08:00
|
|
|
const { column } = this.props;
|
|
|
|
const multiple = ('filterMultiple' in column) ? column.filterMultiple : true;
|
2016-11-25 12:03:39 +08:00
|
|
|
const input = multiple ? (
|
|
|
|
<Checkbox checked={this.state.selectedKeys.indexOf(item.value.toString()) >= 0} />
|
|
|
|
) : (
|
|
|
|
<Radio checked={this.state.selectedKeys.indexOf(item.value.toString()) >= 0} />
|
|
|
|
);
|
|
|
|
|
2016-01-11 10:10:32 +08:00
|
|
|
return (
|
2016-02-02 16:48:16 +08:00
|
|
|
<MenuItem key={item.value}>
|
2016-11-25 12:03:39 +08:00
|
|
|
{input}
|
2016-04-15 23:34:40 +08:00
|
|
|
<span>{item.text}</span>
|
2016-02-02 16:48:16 +08:00
|
|
|
</MenuItem>
|
2016-01-11 10:10:32 +08:00
|
|
|
);
|
2016-04-13 17:23:14 +08:00
|
|
|
}
|
|
|
|
|
2017-02-21 15:54: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 => {
|
2016-01-10 21:10:10 +08:00
|
|
|
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(
|
2017-03-23 21:15:49 +08:00
|
|
|
key => keyPathOfSelectedItem[key].indexOf(item.value) >= 0,
|
2016-06-01 11:37:13 +08:00
|
|
|
);
|
2016-09-14 16:18:33 +08:00
|
|
|
const subMenuCls = containSelected ? `${this.props.dropdownPrefixCls}-submenu-contain-selected` : '';
|
2016-01-10 21:10:10 +08:00
|
|
|
return (
|
2016-04-12 19:43:10 +08:00
|
|
|
<SubMenu title={item.text} className={subMenuCls} key={item.value.toString()}>
|
2017-01-13 21:13:31 +08:00
|
|
|
{this.renderMenus(item.children)}
|
2016-01-10 21:10:10 +08:00
|
|
|
</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 }) => {
|
2016-01-10 21:10:10 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-27 17:38: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() {
|
2017-05-24 12:17:40 +08:00
|
|
|
const { column, locale, prefixCls, dropdownPrefixCls, getPopupContainer } = this.props;
|
2015-11-03 22:32:36 +08:00
|
|
|
// default multiple selection in filter dropdown
|
2016-06-01 11:46:27 +08:00
|
|
|
const multiple = ('filterMultiple' in column) ? column.filterMultiple : true;
|
2017-02-21 15:54:14 +08:00
|
|
|
const dropdownMenuClass = classNames({
|
|
|
|
[`${dropdownPrefixCls}-menu-without-submenu`]: !this.hasSubMenu(),
|
|
|
|
});
|
2016-11-25 16:24:24 +08:00
|
|
|
const menus = column.filterDropdown ? (
|
|
|
|
<FilterDropdownMenuWrapper>
|
|
|
|
{column.filterDropdown}
|
|
|
|
</FilterDropdownMenuWrapper>
|
|
|
|
) : (
|
2016-09-22 14:36:22 +08:00
|
|
|
<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}
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls={`${dropdownPrefixCls}-menu`}
|
2017-02-21 15:54:14 +08:00
|
|
|
className={dropdownMenuClass}
|
2016-01-07 17:46:46 +08:00
|
|
|
onSelect={this.setSelectedKeys}
|
|
|
|
onDeselect={this.setSelectedKeys}
|
2016-06-06 13:54:10 +08:00
|
|
|
selectedKeys={this.state.selectedKeys}
|
|
|
|
>
|
2017-11-21 13:48:37 +08:00
|
|
|
{this.renderMenus(column.filters!)}
|
2016-01-07 14:21:29 +08:00
|
|
|
</Menu>
|
2016-09-14 16:18:33 +08:00
|
|
|
<div className={`${prefixCls}-dropdown-btns`}>
|
2016-06-20 16:25:35 +08:00
|
|
|
<a
|
2016-09-14 16:18:33 +08:00
|
|
|
className={`${prefixCls}-dropdown-link confirm`}
|
2016-06-06 13:54:10 +08:00
|
|
|
onClick={this.handleConfirm}
|
|
|
|
>
|
2016-01-07 14:21:29 +08:00
|
|
|
{locale.filterConfirm}
|
|
|
|
</a>
|
2016-06-20 16:25:35 +08:00
|
|
|
<a
|
2016-09-14 16:18:33 +08:00
|
|
|
className={`${prefixCls}-dropdown-link clear`}
|
2016-06-06 13:54:10 +08:00
|
|
|
onClick={this.handleClearFilters}
|
|
|
|
>
|
2016-01-07 14:21:29 +08:00
|
|
|
{locale.filterReset}
|
|
|
|
</a>
|
|
|
|
</div>
|
2016-07-26 16:46:31 +08:00
|
|
|
</FilterDropdownMenuWrapper>
|
2016-01-07 14:21:29 +08:00
|
|
|
);
|
2015-08-17 18:14:39 +08:00
|
|
|
|
2016-01-07 14:21:29 +08:00
|
|
|
return (
|
2016-06-20 16:25:35 +08:00
|
|
|
<Dropdown
|
|
|
|
trigger={['click']}
|
2016-01-07 17:46:46 +08:00
|
|
|
overlay={menus}
|
2017-02-25 01:36:45 +08:00
|
|
|
visible={this.neverShown ? false : this.state.visible}
|
2016-01-07 17:46:46 +08:00
|
|
|
onVisibleChange={this.onVisibleChange}
|
2017-05-24 12:17:40 +08:00
|
|
|
getPopupContainer={getPopupContainer}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2017-03-27 17:38:14 +08:00
|
|
|
{this.renderFilterIcon()}
|
2016-01-07 14:21:29 +08:00
|
|
|
</Dropdown>
|
|
|
|
);
|
2015-07-15 12:16:28 +08:00
|
|
|
}
|
2016-04-13 17:23:14 +08:00
|
|
|
}
|