ant-design/components/table/filterDropdown.jsx

150 lines
4.2 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';
import Radio from '../radio';
2015-07-15 12:16:28 +08:00
2016-04-13 17:23:14 +08:00
export default class FilterMenu extends React.Component {
static defaultProps = {
handleFilter() {},
2016-05-11 09:32:33 +08:00
column: null,
2016-04-13 17:23:14 +08:00
}
constructor(props) {
super(props);
this.state = {
2015-11-03 17:21:51 +08:00
selectedKeys: this.props.selectedKeys,
keyPathOfSelectedItem: {}, // 记录所有有选中子菜单的祖先菜单
visible: false,
2015-07-15 12:16:28 +08:00
};
2016-04-13 17:23:14 +08:00
}
componentWillReceiveProps(nextProps) {
2015-07-31 10:40:23 +08:00
this.setState({
2016-05-11 09:32:33 +08:00
selectedKeys: nextProps.selectedKeys,
2015-07-31 10:40:23 +08:00
});
2016-04-13 17:23:14 +08:00
}
setSelectedKeys = ({ selectedKeys }) => {
2015-11-03 17:21:51 +08:00
this.setState({ selectedKeys });
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 = () => {
2015-08-17 18:14:39 +08:00
this.setState({
2016-05-11 09:32:33 +08:00
visible: false,
2015-08-17 18:14:39 +08:00
});
2015-08-10 17:57:28 +08:00
this.props.confirmFilter(this.props.column, this.state.selectedKeys);
2016-04-13 17:23:14 +08:00
}
onVisibleChange = (visible) => {
2015-08-17 18:14:39 +08:00
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);
}
2016-04-13 17:23:14 +08:00
}
renderMenuItem(item) {
const { column } = this.props;
const multiple = ('filterMultiple' in column) ? column.filterMultiple : true;
return (
<MenuItem key={item.value}>
{
multiple
? <Checkbox checked={this.state.selectedKeys.indexOf(item.value.toString()) >= 0} />
: <Radio checked={this.state.selectedKeys.indexOf(item.value.toString()) >= 0} />
}
2016-04-15 23:34:40 +08:00
<span>{item.text}</span>
</MenuItem>
);
2016-04-13 17:23:14 +08:00
}
2015-07-15 12:16:28 +08:00
renderMenus(items) {
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
);
const subMenuCls = containSelected ? 'ant-dropdown-submenu-contain-selected' : '';
return (
2016-04-12 19:43:10 +08:00
<SubMenu title={item.text} className={subMenuCls} key={item.value.toString()}>
{item.children.map(child => this.renderMenuItem(child))}
</SubMenu>
);
}
return this.renderMenuItem(item);
2015-07-15 12:16:28 +08:00
});
2016-04-13 17:23:14 +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 });
2016-04-13 17:23:14 +08:00
}
2015-07-15 12:16:28 +08:00
render() {
const { column, locale } = this.props;
// default multiple selection in filter dropdown
const multiple = ('filterMultiple' in column) ? column.filterMultiple : true;
const 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
const dropdownSelectedClass = (this.props.selectedKeys.length > 0)
? 'ant-table-filter-selected' : '';
2015-08-17 18:14:39 +08:00
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
}
2016-04-13 17:23:14 +08:00
}