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 sortIcon from '../../assets/sort.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; sortConfig: any; temp: any; } /** * 操作栏显隐控制 * 参数 * @configs 操作栏配置 * @columns 表头列表配置 * @onChange 显隐变化回调 */ export default class Sort extends Component { constructor(props: Props) { super(props); this.state = { visible: false, sortConfig: [], temp: { // 临时排序数据,不上传接口 colName: undefined, colChsName: undefined, sortType: 'ASC', }, }; } componentDidMount() { const { operate: { value }, } = this.props; this.setState({ sortConfig: value && value.length > 0 ? value : [], }); } UNSAFE_componentWillReceiveProps(nextProps: Props) { const oldSortConfig = this.props.operate.value; const newSortConfig = nextProps.operate.value; if (JSON.stringify(oldSortConfig) !== JSON.stringify(newSortConfig)) { this.setState({ sortConfig: newSortConfig && newSortConfig.length > 0 ? newSortConfig : [], temp: { colName: undefined, colChsName: undefined, sortType: 'ASC', }, }); } } getSortColumns = (columns: ColumnProps[]) => { return columns.filter((item) => { return !!item.sortFlag; }); }; toggleOption = () => { const { visible } = this.state; this.setState({ visible: !visible, }); }; // 修改临时排序条件 changeColName = (value: string) => { const { temp } = this.state; const { columns } = this.props; const sortColumnConfig = this.getSortColumns(columns); const sort: any = sortColumnConfig.find((item) => { return item.columnName === value; }); const newTemp = _.assign({}, temp, { colName: value, colChsName: sort.columnChsName }); this.addSortItem(newTemp); }; // 修改临时排序顺序 changeNewSortType = (value: string) => { const { temp } = this.state; const newTemp = _.assign({}, temp, { sortType: value }); this.addSortItem(newTemp); }; // 添加一条 addSortItem = async (data: any) => { if (data.colName && data.sortType) { const { sortConfig } = this.state; const temp = _.cloneDeep(sortConfig) || []; temp.push(data); // 条件填齐后回调保存 this.onChange(temp); } else { this.setState({ temp: data, }); } }; // 删除一条 delSortItem = (index: number) => { const { sortConfig } = this.state; const temp = _.cloneDeep(sortConfig); temp.splice(index, 1); this.onChange(temp); }; // 重置 resetSortItem = () => { this.onChange([]); }; // 改变已有排序条件 changeSortKey = (index: number, value: any) => { const { sortConfig } = this.state; const { columns } = this.props; const sortColumnConfig = this.getSortColumns(columns); const temp = _.cloneDeep(sortConfig); temp[index].colName = value; const sort: any = sortColumnConfig.find((item) => { return item.columnName === value; }); temp[index].colChsName = sort.columnChsName; this.onChange(temp); }; // 改变已有排序顺序 changeSortType = (index: number, value: any) => { const { sortConfig } = this.state; const temp = _.cloneDeep(sortConfig); temp[index].sortType = value; this.onChange(temp); }; // 回调表格更改操作栏方法 onChange = (config: any[]) => { const { operate: { onChange }, } = this.props; if (typeof onChange === 'function') { onChange({ type: 'sortConfig', config }); } }; getFilterConfig = () => { const { sortConfig, temp } = this.state; const { columns } = this.props; const sortColumnConfig = this.getSortColumns(columns); const tempConfig = _.cloneDeep(sortColumnConfig); return tempConfig.map((item: any) => { item.disabled = sortConfig.some((sort: any) => { return sort.colName === item.columnName; }) || temp.colName === item.columnName; return item; }); }; renderContent = (locale) => { const { sortConfig, temp } = this.state; const filterConfig = this.getFilterConfig(); return (
{sortConfig.map((item: any, i: number) => { return (
); })} {sortConfig.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.sort === '排序' ? `${len}条排序` : `Sorted by ${len} fields`; return (
{len > 0 ? txt : locale.sort}
); }}
); } }