2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2019-02-26 14:26:08 +08:00
|
|
|
import { polyfill } from 'react-lifecycles-compat';
|
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';
|
2018-05-04 14:51:15 +08:00
|
|
|
import shallowequal from 'shallowequal';
|
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';
|
2019-02-26 14:26:08 +08:00
|
|
|
import { generateValueMaps } from './util';
|
2016-07-26 16:46:31 +08:00
|
|
|
|
2018-09-17 17:33:02 +08:00
|
|
|
function stopPropagation(e: React.SyntheticEvent<any>) {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (e.nativeEvent.stopImmediatePropagation) {
|
|
|
|
e.nativeEvent.stopImmediatePropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 14:26:08 +08:00
|
|
|
class FilterMenu<T> extends React.Component<FilterMenuProps<T>, FilterMenuState<T>> {
|
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
|
|
|
|
2019-02-26 14:26:08 +08:00
|
|
|
static getDerivedStateFromProps<T>(nextProps: FilterMenuProps<T>, prevState: FilterMenuState<T>) {
|
|
|
|
const { column } = nextProps;
|
|
|
|
const { prevProps } = prevState;
|
|
|
|
|
|
|
|
const newState: Partial<FilterMenuState<T>> = {
|
|
|
|
prevProps: nextProps,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(prevProps.selectedKeys, nextProps.selectedKeys)
|
|
|
|
) {
|
|
|
|
newState.selectedKeys = nextProps.selectedKeys;
|
|
|
|
}
|
|
|
|
if (!shallowequal((prevProps.column || {}).filters, (nextProps.column || {}).filters)) {
|
|
|
|
newState.valueKeys = generateValueMaps(nextProps.column.filters);
|
|
|
|
}
|
|
|
|
if ('filterDropdownVisible' in column) {
|
|
|
|
newState.visible = column.filterDropdownVisible as boolean;
|
|
|
|
}
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
const visible =
|
|
|
|
'filterDropdownVisible' in props.column ? props.column.filterDropdownVisible : false;
|
2016-12-08 11:18:35 +08:00
|
|
|
|
2016-04-13 17:23:14 +08:00
|
|
|
this.state = {
|
2016-06-20 16:36:37 +08:00
|
|
|
selectedKeys: props.selectedKeys,
|
2019-02-26 14:26:08 +08:00
|
|
|
valueKeys: generateValueMaps(props.column.filters),
|
2018-12-07 16:17:45 +08:00
|
|
|
keyPathOfSelectedItem: {}, // 记录所有有选中子菜单的祖先菜单
|
2016-12-08 11:18:35 +08:00
|
|
|
visible,
|
2019-02-26 14:26:08 +08:00
|
|
|
prevProps: props,
|
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
|
|
|
}
|
|
|
|
|
2019-02-26 14:26:08 +08:00
|
|
|
componentDidUpdate() {
|
|
|
|
const { column } = this.props;
|
2017-10-22 15:54:12 +08:00
|
|
|
this.setNeverShown(column);
|
2016-04-13 17:23:14 +08:00
|
|
|
}
|
|
|
|
|
2018-09-17 18:49:07 +08:00
|
|
|
getDropdownVisible() {
|
|
|
|
return this.neverShown ? false : this.state.visible;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2017-10-22 15:54:12 +08:00
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
setSelectedKeys = ({ selectedKeys }: { selectedKeys: string[] }) => {
|
2015-11-03 17:21:51 +08:00
|
|
|
this.setState({ selectedKeys });
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
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 = () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
selectedKeys: [],
|
|
|
|
},
|
|
|
|
this.handleConfirm,
|
|
|
|
);
|
|
|
|
};
|
2016-04-13 17:23:14 +08:00
|
|
|
|
|
|
|
handleConfirm = () => {
|
2016-11-14 17:53:02 +08:00
|
|
|
this.setVisible(false);
|
2018-09-19 18:33:09 +08: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
|
|
|
|
this.setState({}, this.confirmFilter);
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
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();
|
|
|
|
}
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2016-07-04 12:23:43 +08:00
|
|
|
|
|
|
|
confirmFilter() {
|
2019-02-26 14:26:08 +08:00
|
|
|
const { selectedKeys, valueKeys } = this.state;
|
|
|
|
const { filterDropdown } = this.props.column;
|
2018-07-04 20:24:44 +08:00
|
|
|
|
|
|
|
if (!shallowequal(selectedKeys, this.props.selectedKeys)) {
|
2019-02-26 14:26:08 +08:00
|
|
|
this.props.confirmFilter(
|
|
|
|
this.props.column,
|
2019-06-17 18:50:38 +08:00
|
|
|
filterDropdown
|
|
|
|
? selectedKeys
|
|
|
|
: selectedKeys.map(key => valueKeys[key]).filter(key => key !== undefined),
|
2019-02-26 14:26:08 +08:00
|
|
|
);
|
2015-12-02 11:36:59 +08:00
|
|
|
}
|
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;
|
2018-05-10 11:01:58 +08:00
|
|
|
const { selectedKeys } = this.state;
|
2018-12-07 16:17:45 +08:00
|
|
|
const multiple = 'filterMultiple' in column ? column.filterMultiple : true;
|
2019-04-02 22:06:51 +08:00
|
|
|
|
|
|
|
// We still need trade key as string since Menu render need string
|
|
|
|
const internalSelectedKeys = (selectedKeys || []).map(key => key.toString());
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
const input = multiple ? (
|
2019-04-02 22:06:51 +08:00
|
|
|
<Checkbox checked={internalSelectedKeys.indexOf(item.value.toString()) >= 0} />
|
2018-12-07 16:17:45 +08:00
|
|
|
) : (
|
2019-04-02 22:06:51 +08:00
|
|
|
<Radio checked={internalSelectedKeys.indexOf(item.value.toString()) >= 0} />
|
2018-12-07 16:17:45 +08:00
|
|
|
);
|
2016-11-25 12:03:39 +08:00
|
|
|
|
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() {
|
2018-12-07 16:17:45 +08:00
|
|
|
const {
|
|
|
|
column: { filters = [] },
|
|
|
|
} = this.props;
|
2017-02-21 15:54:14 +08:00
|
|
|
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
|
|
|
);
|
2018-12-07 16:17:45 +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
|
|
|
}
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
handleMenuItemClick = (info: { keyPath: string; key: string }) => {
|
2018-11-15 23:24:59 +08:00
|
|
|
const { selectedKeys } = this.state;
|
2018-04-29 23:01:58 +08:00
|
|
|
if (!info.keyPath || info.keyPath.length <= 1) {
|
2016-01-10 21:10:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const keyPathOfSelectedItem = this.state.keyPathOfSelectedItem;
|
2018-11-15 23:24:59 +08:00
|
|
|
if (selectedKeys && selectedKeys.indexOf(info.key) >= 0) {
|
2016-01-10 21:10:10 +08:00
|
|
|
// deselect SubMenu child
|
|
|
|
delete keyPathOfSelectedItem[info.key];
|
|
|
|
} else {
|
|
|
|
// select SubMenu child
|
|
|
|
keyPathOfSelectedItem[info.key] = info.keyPath;
|
|
|
|
}
|
|
|
|
this.setState({ keyPathOfSelectedItem });
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2016-04-13 17:23:14 +08:00
|
|
|
|
2017-03-27 17:38:14 +08:00
|
|
|
renderFilterIcon = () => {
|
2018-11-15 23:24:59 +08:00
|
|
|
const { column, locale, prefixCls, selectedKeys } = this.props;
|
|
|
|
const filtered = selectedKeys && selectedKeys.length > 0;
|
2018-07-02 18:34:17 +08:00
|
|
|
let filterIcon = column.filterIcon as any;
|
|
|
|
if (typeof filterIcon === 'function') {
|
2018-11-15 23:24:59 +08:00
|
|
|
filterIcon = filterIcon(filtered);
|
2018-07-02 18:34:17 +08:00
|
|
|
}
|
2018-09-17 18:49:07 +08:00
|
|
|
|
|
|
|
const dropdownIconClass = classNames({
|
2018-11-15 23:24:59 +08:00
|
|
|
[`${prefixCls}-selected`]: filtered,
|
2018-09-17 18:49:07 +08:00
|
|
|
[`${prefixCls}-open`]: this.getDropdownVisible(),
|
|
|
|
});
|
2017-03-27 17:38:14 +08:00
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
return filterIcon ? (
|
|
|
|
React.cloneElement(filterIcon as any, {
|
|
|
|
title: locale.filterTitle,
|
|
|
|
className: classNames(`${prefixCls}-icon`, dropdownIconClass, filterIcon.props.className),
|
|
|
|
onClick: stopPropagation,
|
|
|
|
})
|
|
|
|
) : (
|
2018-09-17 15:28:49 +08:00
|
|
|
<Icon
|
|
|
|
title={locale.filterTitle}
|
|
|
|
type="filter"
|
|
|
|
theme="filled"
|
2018-09-17 18:49:07 +08:00
|
|
|
className={dropdownIconClass}
|
2018-09-17 17:33:02 +08:00
|
|
|
onClick={stopPropagation}
|
2018-09-17 15:28:49 +08:00
|
|
|
/>
|
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2018-09-17 18:49:07 +08:00
|
|
|
|
2015-07-15 12:16:28 +08:00
|
|
|
render() {
|
2019-02-26 14:26:08 +08:00
|
|
|
const { selectedKeys: originSelectedKeys } = this.state;
|
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
|
2018-12-07 16:17:45 +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(),
|
|
|
|
});
|
2018-05-04 00:09:05 +08:00
|
|
|
let { filterDropdown } = column;
|
2018-09-30 17:04:05 +08:00
|
|
|
if (filterDropdown instanceof Function) {
|
|
|
|
filterDropdown = filterDropdown({
|
2018-05-04 00:09:05 +08:00
|
|
|
prefixCls: `${dropdownPrefixCls}-custom`,
|
|
|
|
setSelectedKeys: (selectedKeys: Array<any>) => this.setSelectedKeys({ selectedKeys }),
|
2019-02-26 14:26:08 +08:00
|
|
|
selectedKeys: originSelectedKeys,
|
2018-05-04 00:09:05 +08:00
|
|
|
confirm: this.handleConfirm,
|
|
|
|
clearFilters: this.handleClearFilters,
|
|
|
|
filters: column.filters,
|
2019-05-07 11:53:08 +08:00
|
|
|
getPopupContainer: (triggerNode: HTMLElement) => triggerNode.parentNode as HTMLElement,
|
2018-05-04 00:09:05 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const menus = filterDropdown ? (
|
2019-01-10 13:52:12 +08:00
|
|
|
<FilterDropdownMenuWrapper className={`${prefixCls}-dropdown`}>
|
|
|
|
{filterDropdown}
|
|
|
|
</FilterDropdownMenuWrapper>
|
2016-11-25 16:24:24 +08:00
|
|
|
) : (
|
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}
|
2019-02-26 14:26:08 +08:00
|
|
|
selectedKeys={originSelectedKeys && originSelectedKeys.map(val => val.toString())}
|
2018-02-26 17:24:55 +08:00
|
|
|
getPopupContainer={(triggerNode: HTMLElement) => triggerNode.parentNode}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
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`}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<a className={`${prefixCls}-dropdown-link confirm`} onClick={this.handleConfirm}>
|
2016-01-07 14:21:29 +08:00
|
|
|
{locale.filterConfirm}
|
|
|
|
</a>
|
2018-12-07 16:17:45 +08:00
|
|
|
<a className={`${prefixCls}-dropdown-link clear`} 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']}
|
2018-09-17 15:28:49 +08:00
|
|
|
placement="bottomRight"
|
2016-01-07 17:46:46 +08:00
|
|
|
overlay={menus}
|
2018-09-17 18:49:07 +08:00
|
|
|
visible={this.getDropdownVisible()}
|
2016-01-07 17:46:46 +08:00
|
|
|
onVisibleChange={this.onVisibleChange}
|
2017-05-24 12:17:40 +08:00
|
|
|
getPopupContainer={getPopupContainer}
|
2017-12-03 19:09:39 +08:00
|
|
|
forceRender
|
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
|
|
|
}
|
2019-02-26 14:26:08 +08:00
|
|
|
|
|
|
|
polyfill(FilterMenu);
|
|
|
|
|
|
|
|
export default FilterMenu;
|