ant-design/components/table/filterDropdown.jsx

137 lines
3.9 KiB
React
Raw Normal View History

2015-07-15 12:16:28 +08:00
import React from 'react';
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,
keyPathOfSelectedItem: {}, // 记录所有有选中子菜单的祖先菜单
visible: false,
2015-07-15 12:16:28 +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 {
handleFilter() {},
2015-07-15 12:16:28 +08:00
column: null
};
},
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({
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
},
renderMenuItem(item) {
return (
<MenuItem key={item.value}>
<Checkbox checked={this.state.selectedKeys.indexOf(item.value) >= 0} />
{item.text}
</MenuItem>
);
},
2015-07-15 12:16:28 +08:00
renderMenus(items) {
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];
return keyPath.indexOf(item.value) >= 0;
});
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;
},
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() {
let { column, locale } = this.props;
// default multiple selection in filter dropdown
let multiple = true;
if ('filterMultiple' in column) {
multiple = column.filterMultiple;
}
let menus = (
<div className="ant-table-filter-dropdown">
<Menu multiple={multiple}
2016-03-15 19:31:56 +08:00
onClick={this.handleMenuItemClick}
prefixCls="ant-dropdown-menu"
onSelect={this.setSelectedKeys}
onDeselect={this.setSelectedKeys}
selectedKeys={this.state.selectedKeys}>
{this.renderMenus(column.filters)}
</Menu>
<div className="ant-table-filter-dropdown-btns">
<a className="ant-table-filter-dropdown-link confirm"
onClick={this.handleConfirm}>
{locale.filterConfirm}
</a>
<a className="ant-table-filter-dropdown-link clear"
onClick={this.handleClearFilters}>
{locale.filterReset}
</a>
</div>
2015-12-10 12:13:13 +08:00
</div>
);
2015-08-17 18:14:39 +08:00
let dropdownSelectedClass = '';
if (this.props.selectedKeys.length > 0) {
dropdownSelectedClass = 'ant-table-filter-selected';
}
return (
<Dropdown trigger={['click']}
overlay={menus}
visible={this.state.visible}
onVisibleChange={this.onVisibleChange}
closeOnSelect={false}>
<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;