2019-01-12 03:33:27 +00:00
|
|
|
import Menu, { SubMenu, Item as MenuItem } from '../vc-menu';
|
|
|
|
import closest from 'dom-closest';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import shallowequal from 'shallowequal';
|
|
|
|
import Dropdown from '../dropdown';
|
|
|
|
import Icon from '../icon';
|
|
|
|
import Checkbox from '../checkbox';
|
|
|
|
import Radio from '../radio';
|
|
|
|
import FilterDropdownMenuWrapper from './FilterDropdownMenuWrapper';
|
|
|
|
import { FilterMenuProps } from './interface';
|
|
|
|
import { initDefaultProps, getOptionProps } from '../_util/props-util';
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2018-03-30 14:00:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function stopPropagation(e) {
|
|
|
|
e.stopPropagation();
|
2018-12-11 12:59:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 14:00:55 +00:00
|
|
|
export default {
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
name: 'FilterMenu',
|
|
|
|
props: initDefaultProps(FilterMenuProps, {
|
2019-01-12 03:33:27 +00:00
|
|
|
handleFilter() {},
|
2018-03-30 14:00:55 +00:00
|
|
|
column: {},
|
|
|
|
}),
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
|
|
|
const visible =
|
|
|
|
'filterDropdownVisible' in this.column ? this.column.filterDropdownVisible : false;
|
|
|
|
this.preProps = { ...getOptionProps(this) };
|
2018-03-30 14:00:55 +00:00
|
|
|
return {
|
|
|
|
sSelectedKeys: this.selectedKeys,
|
|
|
|
sKeyPathOfSelectedItem: {}, // 记录所有有选中子菜单的祖先菜单
|
|
|
|
sVisible: visible,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-30 14:00:55 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
|
|
|
const { column } = this;
|
2018-03-30 14:00:55 +00:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setNeverShown(column);
|
|
|
|
});
|
2018-03-30 14:00:55 +00:00
|
|
|
},
|
2018-04-01 08:50:31 +00:00
|
|
|
watch: {
|
2019-01-12 03:33:27 +00:00
|
|
|
_propsSymbol() {
|
|
|
|
const nextProps = getOptionProps(this);
|
|
|
|
const { column } = nextProps;
|
|
|
|
this.setNeverShown(column);
|
|
|
|
const newState = {};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
/**
|
2019-01-12 03:33:27 +00:00
|
|
|
* if the state is visible the component should ignore updates on selectedKeys prop to avoid
|
|
|
|
* that the user selection is lost
|
|
|
|
* this happens frequently when a table is connected on some sort of realtime data
|
|
|
|
* Fixes https://github.com/ant-design/ant-design/issues/10289 and
|
|
|
|
* https://github.com/ant-design/ant-design/issues/10209
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
'selectedKeys' in nextProps &&
|
|
|
|
!shallowequal(this.preProps.selectedKeys, nextProps.selectedKeys)
|
|
|
|
) {
|
|
|
|
newState.sSelectedKeys = nextProps.selectedKeys;
|
2018-04-01 08:50:31 +00:00
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
if ('filterDropdownVisible' in column) {
|
2019-01-12 03:33:27 +00:00
|
|
|
newState.sVisible = column.filterDropdownVisible;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
if (Object.keys(newState).length > 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState(newState);
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.preProps = { ...nextProps };
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
// 'column.fixed': function (val) {
|
|
|
|
// this.setNeverShown(this.column)
|
|
|
|
// },
|
|
|
|
// column (val) {
|
|
|
|
// if ('filterDropdownVisible' in val) {
|
|
|
|
// this.sVisible = val.filterDropdownVisible
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// selectedKeys (val) {
|
|
|
|
// this.sSelectedKeys = val
|
|
|
|
// },
|
2018-03-30 14:00:55 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
getDropdownVisible() {
|
|
|
|
return this.neverShown ? false : this.sVisible;
|
2018-12-11 12:59:23 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
setNeverShown(column) {
|
|
|
|
const rootNode = this.$el;
|
|
|
|
const filterBelongToScrollBody = !!closest(rootNode, `.ant-table-scroll`);
|
2018-04-01 08:50:31 +00:00
|
|
|
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
|
2019-01-12 03:33:27 +00:00
|
|
|
this.neverShown = !!column.fixed;
|
2018-04-01 08:50:31 +00:00
|
|
|
}
|
|
|
|
},
|
2018-03-30 14:00:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
setSelectedKeys({ selectedKeys }) {
|
|
|
|
this.setState({ sSelectedKeys: selectedKeys });
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
2018-03-30 14:00:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
setVisible(visible) {
|
|
|
|
const { column } = this;
|
2018-04-01 08:50:31 +00:00
|
|
|
if (!('filterDropdownVisible' in column)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState({ sVisible: visible });
|
2018-04-01 08:50:31 +00:00
|
|
|
}
|
|
|
|
if (column.onFilterDropdownVisibleChange) {
|
2019-01-12 03:33:27 +00:00
|
|
|
column.onFilterDropdownVisibleChange(visible);
|
2018-04-01 08:50:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
handleClearFilters() {
|
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
sSelectedKeys: [],
|
|
|
|
},
|
|
|
|
this.handleConfirm,
|
|
|
|
);
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
handleConfirm() {
|
|
|
|
this.setVisible(false);
|
|
|
|
this.confirmFilter2();
|
2018-12-11 12:59:23 +00:00
|
|
|
// Call `setSelectedKeys` & `confirm` in the same time will make filter data not up to date
|
|
|
|
// https://github.com/ant-design/ant-design/issues/12284
|
2019-01-12 03:33:27 +00:00
|
|
|
this.$forceUpdate();
|
2018-12-11 12:59:23 +00:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.confirmFilter;
|
|
|
|
});
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
onVisibleChange(visible) {
|
|
|
|
this.setVisible(visible);
|
2018-04-01 08:50:31 +00:00
|
|
|
if (!visible) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.confirmFilter2();
|
2018-04-01 08:50:31 +00:00
|
|
|
}
|
|
|
|
},
|
2018-03-30 14:00:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
confirmFilter2() {
|
2018-09-05 13:28:54 +00:00
|
|
|
if (!shallowequal(this.sSelectedKeys, this.selectedKeys)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.confirmFilter(this.column, this.sSelectedKeys);
|
2018-03-30 14:00:55 +00:00
|
|
|
}
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
renderMenuItem(item) {
|
|
|
|
const { column } = this;
|
|
|
|
const { sSelectedKeys: selectedKeys } = this.$data;
|
|
|
|
const multiple = 'filterMultiple' in column ? column.filterMultiple : true;
|
2018-04-01 08:50:31 +00:00
|
|
|
const input = multiple ? (
|
2018-12-13 13:26:21 +00:00
|
|
|
<Checkbox checked={selectedKeys && selectedKeys.indexOf(item.value.toString()) >= 0} />
|
2018-04-01 08:50:31 +00:00
|
|
|
) : (
|
2018-12-13 13:26:21 +00:00
|
|
|
<Radio checked={selectedKeys && selectedKeys.indexOf(item.value.toString()) >= 0} />
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-01 08:50:31 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<MenuItem key={item.value}>
|
|
|
|
{input}
|
|
|
|
<span>{item.text}</span>
|
|
|
|
</MenuItem>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
hasSubMenu() {
|
|
|
|
const {
|
|
|
|
column: { filters = [] },
|
|
|
|
} = this;
|
|
|
|
return filters.some(item => !!(item.children && item.children.length > 0));
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
renderMenus(items) {
|
2018-04-01 08:50:31 +00:00
|
|
|
return items.map(item => {
|
|
|
|
if (item.children && item.children.length > 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const { sKeyPathOfSelectedItem } = this;
|
2018-04-01 08:50:31 +00:00
|
|
|
const containSelected = Object.keys(sKeyPathOfSelectedItem).some(
|
|
|
|
key => sKeyPathOfSelectedItem[key].indexOf(item.value) >= 0,
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
|
|
|
const subMenuCls = containSelected
|
|
|
|
? `${this.dropdownPrefixCls}-submenu-contain-selected`
|
|
|
|
: '';
|
2018-04-01 08:50:31 +00:00
|
|
|
return (
|
|
|
|
<SubMenu title={item.text} class={subMenuCls} key={item.value.toString()}>
|
|
|
|
{this.renderMenus(item.children)}
|
|
|
|
</SubMenu>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-01 08:50:31 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return this.renderMenuItem(item);
|
|
|
|
});
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
handleMenuItemClick(info) {
|
|
|
|
const { sSelectedKeys: selectedKeys } = this.$data;
|
2018-09-05 13:28:54 +00:00
|
|
|
if (!info.keyPath || info.keyPath.length <= 1) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-01 08:50:31 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const keyPathOfSelectedItem = this.sKeyPathOfSelectedItem;
|
2018-12-13 13:26:21 +00:00
|
|
|
if (selectedKeys && selectedKeys.indexOf(info.key) >= 0) {
|
2018-04-01 08:50:31 +00:00
|
|
|
// deselect SubMenu child
|
2019-01-12 03:33:27 +00:00
|
|
|
delete keyPathOfSelectedItem[info.key];
|
2018-04-01 08:50:31 +00:00
|
|
|
} else {
|
|
|
|
// select SubMenu child
|
2019-01-12 03:33:27 +00:00
|
|
|
keyPathOfSelectedItem[info.key] = info.keyPath;
|
2018-04-01 08:50:31 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.setState({ keyPathOfSelectedItem });
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
2018-03-30 14:00:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
renderFilterIcon() {
|
|
|
|
const { column, locale, prefixCls, selectedKeys } = this;
|
|
|
|
const filtered = selectedKeys && selectedKeys.length > 0;
|
|
|
|
let filterIcon = column.filterIcon;
|
2018-09-05 13:28:54 +00:00
|
|
|
if (typeof filterIcon === 'function') {
|
2019-01-12 03:33:27 +00:00
|
|
|
filterIcon = filterIcon(filtered, column);
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2018-12-11 12:59:23 +00:00
|
|
|
const dropdownIconClass = classNames({
|
2018-12-13 13:26:21 +00:00
|
|
|
[`${prefixCls}-selected`]: filtered,
|
2018-12-11 12:59:23 +00:00
|
|
|
[`${prefixCls}-open`]: this.getDropdownVisible(),
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-30 14:00:55 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
return filterIcon ? (
|
|
|
|
cloneElement(filterIcon, {
|
|
|
|
attrs: {
|
|
|
|
title: locale.filterTitle,
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
click: stopPropagation,
|
|
|
|
},
|
|
|
|
class: classNames(`${prefixCls}-icon`, dropdownIconClass),
|
|
|
|
})
|
|
|
|
) : (
|
|
|
|
<Icon
|
2018-12-11 12:59:23 +00:00
|
|
|
title={locale.filterTitle}
|
2019-01-12 03:33:27 +00:00
|
|
|
type="filter"
|
|
|
|
theme="filled"
|
2018-12-11 12:59:23 +00:00
|
|
|
class={dropdownIconClass}
|
|
|
|
onClick={stopPropagation}
|
|
|
|
/>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-04-01 08:50:31 +00:00
|
|
|
},
|
2018-03-30 14:00:55 +00:00
|
|
|
},
|
2018-04-01 08:50:31 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { column, locale, prefixCls, dropdownPrefixCls, getPopupContainer } = this;
|
2018-03-30 14:00:55 +00:00
|
|
|
// default multiple selection in filter dropdown
|
2019-01-12 03:33:27 +00:00
|
|
|
const multiple = 'filterMultiple' in column ? column.filterMultiple : true;
|
2018-03-30 14:00:55 +00:00
|
|
|
const dropdownMenuClass = classNames({
|
|
|
|
[`${dropdownPrefixCls}-menu-without-submenu`]: !this.hasSubMenu(),
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
let { filterDropdown } = column;
|
2018-12-11 12:59:23 +00:00
|
|
|
if (filterDropdown instanceof Function) {
|
2018-09-05 13:28:54 +00:00
|
|
|
filterDropdown = filterDropdown({
|
|
|
|
prefixCls: `${dropdownPrefixCls}-custom`,
|
2019-01-12 03:33:27 +00:00
|
|
|
setSelectedKeys: selectedKeys => this.setSelectedKeys({ selectedKeys }),
|
2018-09-05 13:28:54 +00:00
|
|
|
selectedKeys: this.sSelectedKeys,
|
|
|
|
confirm: this.handleConfirm,
|
|
|
|
clearFilters: this.handleClearFilters,
|
|
|
|
filters: column.filters,
|
2019-01-12 03:33:27 +00:00
|
|
|
getPopupContainer: triggerNode => triggerNode.parentNode,
|
2019-01-06 10:11:50 +00:00
|
|
|
column,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const menus = filterDropdown ? (
|
2019-01-12 03:33:27 +00:00
|
|
|
<FilterDropdownMenuWrapper>{filterDropdown}</FilterDropdownMenuWrapper>
|
2018-03-30 14:00:55 +00:00
|
|
|
) : (
|
|
|
|
<FilterDropdownMenuWrapper class={`${prefixCls}-dropdown`}>
|
|
|
|
<Menu
|
|
|
|
multiple={multiple}
|
|
|
|
onClick={this.handleMenuItemClick}
|
|
|
|
prefixCls={`${dropdownPrefixCls}-menu`}
|
|
|
|
class={dropdownMenuClass}
|
|
|
|
onSelect={this.setSelectedKeys}
|
|
|
|
onDeselect={this.setSelectedKeys}
|
|
|
|
selectedKeys={this.sSelectedKeys}
|
2019-01-12 03:33:27 +00:00
|
|
|
getPopupContainer={triggerNode => triggerNode.parentNode}
|
2018-03-30 14:00:55 +00:00
|
|
|
>
|
|
|
|
{this.renderMenus(column.filters)}
|
|
|
|
</Menu>
|
|
|
|
<div class={`${prefixCls}-dropdown-btns`}>
|
2019-01-12 03:33:27 +00:00
|
|
|
<a class={`${prefixCls}-dropdown-link confirm`} onClick={this.handleConfirm}>
|
2018-03-30 14:00:55 +00:00
|
|
|
{locale.filterConfirm}
|
|
|
|
</a>
|
2019-01-12 03:33:27 +00:00
|
|
|
<a class={`${prefixCls}-dropdown-link clear`} onClick={this.handleClearFilters}>
|
2018-03-30 14:00:55 +00:00
|
|
|
{locale.filterReset}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</FilterDropdownMenuWrapper>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-30 14:00:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Dropdown
|
|
|
|
trigger={['click']}
|
2019-01-12 03:33:27 +00:00
|
|
|
placement="bottomRight"
|
2018-12-11 12:59:23 +00:00
|
|
|
visible={this.getDropdownVisible()}
|
2018-03-30 14:00:55 +00:00
|
|
|
onVisibleChange={this.onVisibleChange}
|
|
|
|
getPopupContainer={getPopupContainer}
|
|
|
|
forceRender
|
|
|
|
>
|
2019-01-12 03:33:27 +00:00
|
|
|
<template slot="overlay">{menus}</template>
|
2018-03-30 14:00:55 +00:00
|
|
|
{this.renderFilterIcon()}
|
|
|
|
</Dropdown>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-30 14:00:55 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|