import React from 'react'; import Menu, { SubMenu, Item as MenuItem } from 'rc-menu'; import Dropdown from '../dropdown'; import Icon from '../icon'; import Checkbox from '../checkbox'; import Radio from '../radio'; import FilterDropdownMenuWrapper from './FilterDropdownMenuWrapper'; export interface FilterMenuProps { locale: any; selectedKeys: string[]; column: { filterMultiple?: boolean, filterDropdown?: React.ReactNode, filters?: string[], filterDropdownVisible?: boolean, onFilterDropdownVisibleChange?: (visible: boolean) => any, }; confirmFilter: (column: Object, selectedKeys: string[]) => any; prefixCls: string; dropdownPrefixCls: string; } export default class FilterMenu extends React.Component { static defaultProps = { handleFilter() {}, column: {}, }; constructor(props) { super(props); this.state = { selectedKeys: props.selectedKeys, keyPathOfSelectedItem: {}, // 记录所有有选中子菜单的祖先菜单 visible: false, }; } componentWillReceiveProps(nextProps) { const { column } = nextProps; let newState: { selectedKeys?: string[]; visible?: boolean; } = {}; if ('selectedKeys' in nextProps) { newState.selectedKeys = nextProps.selectedKeys; } if ('filterDropdownVisible' in column) { newState.visible = column.filterDropdownVisible; } if (Object.keys(newState).length > 0) { this.setState(newState); } } setSelectedKeys = ({ selectedKeys }) => { this.setState({ selectedKeys }); } setVisible(visible) { const { column } = this.props; if (!('filterDropdownVisible' in column)) { this.setState({ visible }); } if (column.onFilterDropdownVisibleChange) { column.onFilterDropdownVisibleChange(visible); } } handleClearFilters = () => { this.setState({ selectedKeys: [], }, this.handleConfirm); } handleConfirm = () => { this.setVisible(false); this.confirmFilter(); } onVisibleChange = (visible) => { this.setVisible(visible); if (!visible) { this.confirmFilter(); } } confirmFilter() { if (this.state.selectedKeys !== this.props.selectedKeys) { this.props.confirmFilter(this.props.column, this.state.selectedKeys); } } renderMenuItem(item) { const { column } = this.props; const multiple = ('filterMultiple' in column) ? column.filterMultiple : true; const input = multiple ? ( = 0} /> ) : ( = 0} /> ); return ( {input} {item.text} ); } renderMenus(items) { return items.map(item => { if (item.children && item.children.length > 0) { const { keyPathOfSelectedItem } = this.state; const containSelected = Object.keys(keyPathOfSelectedItem).some( key => keyPathOfSelectedItem[key].indexOf(item.value) >= 0 ); const subMenuCls = containSelected ? `${this.props.dropdownPrefixCls}-submenu-contain-selected` : ''; return ( {item.children.map(child => this.renderMenuItem(child))} ); } return this.renderMenuItem(item); }); } 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 }); } render() { const { column, locale, prefixCls, dropdownPrefixCls } = this.props; // default multiple selection in filter dropdown const multiple = ('filterMultiple' in column) ? column.filterMultiple : true; const menus = column.filterDropdown ? ( {column.filterDropdown} ) : ( {this.renderMenus(column.filters)}
{locale.filterConfirm} {locale.filterReset}
); const dropdownSelectedClass = (this.props.selectedKeys.length > 0) ? `${prefixCls}-selected` : ''; return ( ); } }