ant-design/components/table/filterDropdown.tsx

318 lines
10 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { polyfill } from 'react-lifecycles-compat';
import Menu, { SubMenu, Item as MenuItem } from 'rc-menu';
2017-02-25 02:09:18 +08:00
import closest from 'dom-closest';
import classNames from 'classnames';
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';
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';
import { generateValueMaps } from './util';
2016-07-26 16:46:31 +08:00
function stopPropagation(e: React.SyntheticEvent<any>) {
e.stopPropagation();
if (e.nativeEvent.stopImmediatePropagation) {
e.nativeEvent.stopImmediatePropagation();
}
}
class FilterMenu<T> extends React.Component<FilterMenuProps<T>, FilterMenuState<T>> {
2016-04-13 17:23:14 +08:00
static defaultProps = {
handleFilter() {},
column: {},
2016-07-13 11:14:24 +08:00
};
2016-04-13 17:23:14 +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;
}
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-04-13 17:23:14 +08:00
this.state = {
selectedKeys: props.selectedKeys,
valueKeys: generateValueMaps(props.column.filters),
2018-12-07 16:17:45 +08:00
keyPathOfSelectedItem: {}, // 记录所有有选中子菜单的祖先菜单
visible,
prevProps: props,
2015-07-15 12:16:28 +08:00
};
2016-04-13 17:23:14 +08:00
}
componentDidMount() {
const { column } = this.props;
this.setNeverShown(column);
}
componentDidUpdate() {
const { column } = this.props;
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>) => {
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-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) {
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 = () => {
this.setVisible(false);
// 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) => {
this.setVisible(visible);
2015-12-02 11:36:59 +08:00
if (!visible) {
this.confirmFilter();
}
2018-12-07 16:17:45 +08:00
};
confirmFilter() {
const { selectedKeys, valueKeys } = this.state;
const { filterDropdown } = this.props.column;
if (!shallowequal(selectedKeys, this.props.selectedKeys)) {
this.props.confirmFilter(
this.props.column,
filterDropdown
? selectedKeys
: selectedKeys.map(key => valueKeys[key]).filter(key => key !== undefined),
);
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) {
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;
// 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 ? (
<Checkbox checked={internalSelectedKeys.indexOf(item.value.toString()) >= 0} />
2018-12-07 16:17:45 +08:00
) : (
<Radio checked={internalSelectedKeys.indexOf(item.value.toString()) >= 0} />
2018-12-07 16:17:45 +08:00
);
return (
<MenuItem key={item.value}>
{input}
2016-04-15 23:34:40 +08:00
<span>{item.text}</span>
</MenuItem>
);
2016-04-13 17:23:14 +08:00
}
hasSubMenu() {
2018-12-07 16:17:45 +08:00
const {
column: { filters = [] },
} = this.props;
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 => {
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,
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`
: '';
return (
2016-04-12 19:43:10 +08:00
<SubMenu title={item.text} className={subMenuCls} key={item.value.toString()}>
{this.renderMenus(item.children)}
</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 }) => {
const { selectedKeys } = this.state;
if (!info.keyPath || info.keyPath.length <= 1) {
return;
}
const keyPathOfSelectedItem = this.state.keyPathOfSelectedItem;
if (selectedKeys && selectedKeys.indexOf(info.key) >= 0) {
// 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
renderFilterIcon = () => {
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') {
filterIcon = filterIcon(filtered);
2018-07-02 18:34:17 +08:00
}
2018-09-17 18:49:07 +08:00
const dropdownIconClass = classNames({
[`${prefixCls}-selected`]: filtered,
2018-09-17 18:49:07 +08:00
[`${prefixCls}-open`]: this.getDropdownVisible(),
});
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}
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() {
const { selectedKeys: originSelectedKeys } = this.state;
const { column, locale, prefixCls, dropdownPrefixCls, getPopupContainer } = this.props;
// default multiple selection in filter dropdown
2018-12-07 16:17:45 +08:00
const multiple = 'filterMultiple' in column ? column.filterMultiple : true;
const dropdownMenuClass = classNames({
[`${dropdownPrefixCls}-menu-without-submenu`]: !this.hasSubMenu(),
});
let { filterDropdown } = column;
if (filterDropdown instanceof Function) {
filterDropdown = filterDropdown({
prefixCls: `${dropdownPrefixCls}-custom`,
setSelectedKeys: (selectedKeys: Array<any>) => this.setSelectedKeys({ selectedKeys }),
selectedKeys: originSelectedKeys,
confirm: this.handleConfirm,
clearFilters: this.handleClearFilters,
filters: column.filters,
2019-05-07 11:53:08 +08:00
getPopupContainer: (triggerNode: HTMLElement) => triggerNode.parentNode as HTMLElement,
});
}
const menus = filterDropdown ? (
<FilterDropdownMenuWrapper className={`${prefixCls}-dropdown`}>
{filterDropdown}
</FilterDropdownMenuWrapper>
) : (
<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}
prefixCls={`${dropdownPrefixCls}-menu`}
className={dropdownMenuClass}
onSelect={this.setSelectedKeys}
onDeselect={this.setSelectedKeys}
selectedKeys={originSelectedKeys && originSelectedKeys.map(val => val.toString())}
2018-02-26 17:24:55 +08:00
getPopupContainer={(triggerNode: HTMLElement) => triggerNode.parentNode}
>
2017-11-21 13:48:37 +08:00
{this.renderMenus(column.filters!)}
</Menu>
<div className={`${prefixCls}-dropdown-btns`}>
2018-12-07 16:17:45 +08:00
<a className={`${prefixCls}-dropdown-link confirm`} onClick={this.handleConfirm}>
{locale.filterConfirm}
</a>
2018-12-07 16:17:45 +08:00
<a className={`${prefixCls}-dropdown-link clear`} onClick={this.handleClearFilters}>
{locale.filterReset}
</a>
</div>
2016-07-26 16:46:31 +08:00
</FilterDropdownMenuWrapper>
);
2015-08-17 18:14:39 +08:00
return (
2016-06-20 16:25:35 +08:00
<Dropdown
trigger={['click']}
2018-09-17 15:28:49 +08:00
placement="bottomRight"
overlay={menus}
2018-09-17 18:49:07 +08:00
visible={this.getDropdownVisible()}
onVisibleChange={this.onVisibleChange}
getPopupContainer={getPopupContainer}
forceRender
>
{this.renderFilterIcon()}
</Dropdown>
);
2015-07-15 12:16:28 +08:00
}
2016-04-13 17:23:14 +08:00
}
polyfill(FilterMenu);
export default FilterMenu;