2015-07-15 12:16:28 +08:00
|
|
|
import React from 'react';
|
2016-02-02 16:48:16 +08:00
|
|
|
import Menu, { SubMenu, Item as MenuItem } from 'rc-menu';
|
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';
|
2015-07-15 12:16:28 +08:00
|
|
|
|
2015-09-01 16:18:46 +08:00
|
|
|
let FilterMenu = React.createClass({
|
2015-07-15 12:16:28 +08:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
2015-11-03 17:21:51 +08:00
|
|
|
selectedKeys: this.props.selectedKeys,
|
2016-01-10 21:10:10 +08:00
|
|
|
keyPathOfSelectedItem: {}, // 记录所有有选中子菜单的祖先菜单
|
|
|
|
visible: false,
|
2015-07-15 12:16:28 +08:00
|
|
|
};
|
|
|
|
},
|
2016-01-07 14:21:29 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2015-07-31 10:40:23 +08:00
|
|
|
this.setState({
|
2015-08-10 17:57:28 +08:00
|
|
|
selectedKeys: nextProps.selectedKeys
|
2015-07-31 10:40:23 +08:00
|
|
|
});
|
|
|
|
},
|
2015-07-15 12:16:28 +08:00
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
2016-01-07 17:46:46 +08:00
|
|
|
handleFilter() {},
|
2015-07-15 12:16:28 +08:00
|
|
|
column: null
|
|
|
|
};
|
|
|
|
},
|
2016-01-07 17:46:46 +08:00
|
|
|
setSelectedKeys({ selectedKeys }) {
|
2015-11-03 17:21:51 +08:00
|
|
|
this.setState({ selectedKeys });
|
2015-07-15 12:16:28 +08:00
|
|
|
},
|
|
|
|
handleClearFilters() {
|
|
|
|
this.setState({
|
2015-08-10 17:57:28 +08:00
|
|
|
selectedKeys: []
|
2015-08-12 12:47:04 +08:00
|
|
|
}, this.handleConfirm);
|
2015-07-15 12:16:28 +08:00
|
|
|
},
|
2015-08-17 18:14:39 +08:00
|
|
|
handleConfirm() {
|
|
|
|
this.setState({
|
|
|
|
visible: false
|
|
|
|
});
|
2015-08-10 17:57:28 +08:00
|
|
|
this.props.confirmFilter(this.props.column, this.state.selectedKeys);
|
2015-07-31 10:40:23 +08:00
|
|
|
},
|
2015-08-17 18:14:39 +08:00
|
|
|
onVisibleChange(visible) {
|
|
|
|
this.setState({
|
2016-01-07 17:46:46 +08:00
|
|
|
visible,
|
2015-08-17 18:14:39 +08:00
|
|
|
});
|
2015-12-02 11:36:59 +08:00
|
|
|
if (!visible) {
|
|
|
|
this.props.confirmFilter(this.props.column, this.state.selectedKeys);
|
|
|
|
}
|
2015-08-17 18:14:39 +08:00
|
|
|
},
|
2016-01-10 21:10:10 +08:00
|
|
|
renderMenuItem(item) {
|
2016-01-11 10:10:32 +08:00
|
|
|
return (
|
2016-02-02 16:48:16 +08:00
|
|
|
<MenuItem key={item.value}>
|
2016-01-11 10:10:32 +08:00
|
|
|
<Checkbox checked={this.state.selectedKeys.indexOf(item.value) >= 0} />
|
|
|
|
{item.text}
|
2016-02-02 16:48:16 +08:00
|
|
|
</MenuItem>
|
2016-01-11 10:10:32 +08:00
|
|
|
);
|
2016-01-10 21:10:10 +08:00
|
|
|
},
|
2015-07-15 12:16:28 +08:00
|
|
|
renderMenus(items) {
|
2016-01-10 21:10:10 +08:00
|
|
|
let menuItems = items.map(item => {
|
|
|
|
if (item.children && item.children.length > 0) {
|
|
|
|
const keyPathOfSelectedItem = this.state.keyPathOfSelectedItem;
|
|
|
|
const containSelected = Object.keys(keyPathOfSelectedItem).some(key => {
|
|
|
|
const keyPath = keyPathOfSelectedItem[key];
|
2016-02-22 10:52:30 +08:00
|
|
|
return keyPath.indexOf(item.value) >= 0;
|
2016-01-10 21:10:10 +08:00
|
|
|
});
|
|
|
|
const subMenuCls = containSelected ? 'ant-dropdown-submenu-contain-selected' : '';
|
|
|
|
return (
|
|
|
|
<SubMenu title={item.text} className={subMenuCls} key={item.value}>
|
|
|
|
{item.children.map(child => this.renderMenuItem(child))}
|
|
|
|
</SubMenu>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return this.renderMenuItem(item);
|
2015-07-15 12:16:28 +08:00
|
|
|
});
|
|
|
|
return menuItems;
|
|
|
|
},
|
2016-01-10 21:10:10 +08:00
|
|
|
handleMenuItemClick(info) {
|
|
|
|
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 });
|
|
|
|
},
|
2015-07-15 12:16:28 +08:00
|
|
|
render() {
|
2016-01-07 17:46:46 +08:00
|
|
|
let { column, locale } = this.props;
|
2015-11-03 22:32:36 +08:00
|
|
|
// default multiple selection in filter dropdown
|
|
|
|
let multiple = true;
|
|
|
|
if ('filterMultiple' in column) {
|
|
|
|
multiple = column.filterMultiple;
|
|
|
|
}
|
2016-01-07 14:21:29 +08:00
|
|
|
let menus = (
|
|
|
|
<div className="ant-table-filter-dropdown">
|
|
|
|
<Menu multiple={multiple}
|
2016-03-15 19:31:56 +08:00
|
|
|
onClick={this.handleMenuItemClick}
|
2016-01-07 17:46:46 +08:00
|
|
|
prefixCls="ant-dropdown-menu"
|
|
|
|
onSelect={this.setSelectedKeys}
|
|
|
|
onDeselect={this.setSelectedKeys}
|
|
|
|
selectedKeys={this.state.selectedKeys}>
|
2016-01-07 14:21:29 +08:00
|
|
|
{this.renderMenus(column.filters)}
|
|
|
|
</Menu>
|
|
|
|
<div className="ant-table-filter-dropdown-btns">
|
|
|
|
<a className="ant-table-filter-dropdown-link confirm"
|
2016-01-07 17:46:46 +08:00
|
|
|
onClick={this.handleConfirm}>
|
2016-01-07 14:21:29 +08:00
|
|
|
{locale.filterConfirm}
|
|
|
|
</a>
|
|
|
|
<a className="ant-table-filter-dropdown-link clear"
|
2016-01-07 17:46:46 +08:00
|
|
|
onClick={this.handleClearFilters}>
|
2016-01-07 14:21:29 +08:00
|
|
|
{locale.filterReset}
|
|
|
|
</a>
|
|
|
|
</div>
|
2015-12-10 12:13:13 +08:00
|
|
|
</div>
|
2016-01-07 14:21:29 +08:00
|
|
|
);
|
2015-08-17 18:14:39 +08:00
|
|
|
|
|
|
|
let dropdownSelectedClass = '';
|
|
|
|
if (this.props.selectedKeys.length > 0) {
|
|
|
|
dropdownSelectedClass = 'ant-table-filter-selected';
|
|
|
|
}
|
|
|
|
|
2016-01-07 14:21:29 +08:00
|
|
|
return (
|
|
|
|
<Dropdown trigger={['click']}
|
2016-01-07 17:46:46 +08:00
|
|
|
overlay={menus}
|
|
|
|
visible={this.state.visible}
|
|
|
|
onVisibleChange={this.onVisibleChange}
|
|
|
|
closeOnSelect={false}>
|
2016-01-07 14:21:29 +08:00
|
|
|
<Icon title={locale.filterTitle} type="filter" className={dropdownSelectedClass} />
|
|
|
|
</Dropdown>
|
|
|
|
);
|
2015-07-15 12:16:28 +08:00
|
|
|
}
|
|
|
|
});
|
2015-07-31 10:40:23 +08:00
|
|
|
|
|
|
|
export default FilterMenu;
|