import React, { Component } from 'react'; import _ from 'lodash'; import { Popover, Button, Select } from 'antd'; import classNames from 'classnames'; import s from '../hide/index.less'; import styles from './index.less'; import groupIcon from '../../assets/group.png'; import closeIcon from '../../assets/close.png'; import { ColumnProps } from '../interface'; import { Consumer } from '../context'; interface Props { columns: any; operate: any; } interface State { visible: boolean; groupConfig: any; temp: any; } /** * 操作栏显隐控制 * 参数 * @configs 操作栏配置 * @columns 表头列表配置 * @onChange 显隐变化回调 */ export default class Sort extends Component { constructor(props: Props) { super(props); this.state = { visible: false, groupConfig: [], temp: { // 临时排序数据,不上传接口 colName: undefined, colChsName: undefined, sortType: 'ASC', }, }; } componentDidMount() { const { operate: { value }, } = this.props; this.setState({ groupConfig: value && value.length > 0 ? value : [], }); } UNSAFE_componentWillReceiveProps(nextProps: Props) { const oldGroupConfig = this.props.operate.value; const newGroupConfig = nextProps.operate.value; if (JSON.stringify(oldGroupConfig) !== JSON.stringify(newGroupConfig)) { this.setState({ groupConfig: newGroupConfig && newGroupConfig.length > 0 ? newGroupConfig : [], temp: { colName: undefined, colChsName: undefined, sortType: 'ASC', }, }); } } getGroupColumns = (columns: ColumnProps[]) => { return columns.filter((item) => { return !!item.groupFlag; }); }; toggleOption = () => { const { visible } = this.state; this.setState({ visible: !visible, }); }; // 修改临时排序条件 changeColName = (value: string) => { const { temp } = this.state; const { columns } = this.props; const groupColumnConfig = this.getGroupColumns(columns); const group: any = groupColumnConfig.find((item) => { return item.columnName === value; }); const newTemp = _.assign({}, temp, { colName: value, colChsName: group.columnChsName }); this.addGroupItem(newTemp); }; // 修改临时排序顺序 changeNewSortType = (value: string) => { const { temp } = this.state; const newTemp = _.assign({}, temp, { sortType: value }); this.addGroupItem(newTemp); }; // 添加一条 addGroupItem = async (data: any) => { if (data.colName && data.sortType) { const { groupConfig } = this.state; const temp = _.cloneDeep(groupConfig) || []; temp.push(data); // 条件填齐后回调保存 this.onChange(temp); } else { this.setState({ temp: data, }); } }; // 删除一条 delGroupItem = (index: number) => { const { groupConfig } = this.state; const temp = _.cloneDeep(groupConfig); temp.splice(index, 1); this.onChange(temp); }; // 重置 resetGroupItem = () => { this.onChange([]); }; // 改变已有排序条件 changeGroupKey = (index: number, value: any) => { const { groupConfig } = this.state; const { columns } = this.props; const groupColumnConfig = this.getGroupColumns(columns); const temp = _.cloneDeep(groupConfig); temp[index].colName = value; const group: any = groupColumnConfig.find((item) => { return item.columnName === value; }); temp[index].colChsName = group.columnChsName; this.onChange(temp); }; // 改变已有排序顺序 changeGroupType = (index: number, value: any) => { const { groupConfig } = this.state; const temp = _.cloneDeep(groupConfig); temp[index].sortType = value; this.onChange(temp); }; // 回调表格更改操作栏方法 onChange = (config: any[]) => { const { operate: { onChange }, } = this.props; if (typeof onChange === 'function') { onChange({ type: 'groupConfig', config }); } }; getFilterConfig = () => { const { groupConfig, temp } = this.state; const { columns } = this.props; const groupColumnConfig = this.getGroupColumns(columns); const tempConfig = _.cloneDeep(groupColumnConfig); return tempConfig.map((item: any) => { item.disabled = groupConfig.some((group: any) => { return group.colName === item.columnName; }) || temp.colName === item.columnName; return item; }); }; renderContent = (locale) => { const { groupConfig, temp } = this.state; const filterConfig = this.getFilterConfig(); return (
{groupConfig.map((item: any, i: number) => { return (
); })} {groupConfig.length < 3 && (
)}
{locale.sortReset}
); }; render() { const { visible } = this.state; const { operate: { value }, } = this.props; const len = value && value.length; let bg = ''; if (len > 0) { bg = 'rgba(92,153,255,0.2)'; } return ( {({ locale }) => { const txt = locale.group === '分组' ? `${len}条分组` : `Grouped by ${len} fields`; return (
{len > 0 ? txt : locale.group}
); }}
); } }