import React, { PureComponent } from 'react'; import classNames from 'classnames'; import { Checkbox, Tooltip } from 'antd'; import _ from 'lodash'; import s from './Column.less'; import { ColumnProps } from './interface'; import { memoSelected } from './Cell'; export default class TableColumn extends PureComponent { changeSort = (type: string) => { const { sortConfig = {}, columnName, columnChsName } = this.props; const { onChange } = sortConfig; if (typeof onChange === 'function') { const newSort = { colChsName: columnChsName, colName: columnName, sortType: type }; onChange({ type: 'sortConfig', config: [newSort] }); } }; removeSort = () => { const { sortConfig = {}, columnName } = this.props; const { value, onChange } = sortConfig; if (typeof onChange === 'function') { const newArr = value.filter((item: any) => { return item.colName !== columnName; }); onChange({ type: 'sortConfig', config: newArr }); } }; getCheckbox = () => { const { rowSelection, dataSource } = this.props; const { selectedRows, onChange } = rowSelection; const selectIds = memoSelected(selectedRows); let checked = false; if (dataSource.length > 0 && selectedRows.length > 0) { for (let i = 0; i < dataSource.length; i++) { if (!selectIds.includes(dataSource[i].id)) { break; } if (i === dataSource.length - 1) { checked = true; } } } const onToggle = () => { if (typeof onChange === 'function') { // 初始为传过来的选中数据 const data = _.cloneDeep(selectedRows); dataSource.map((item: any) => { // 当前操作是否操作了选中数据 const index = data.findIndex((temp: any) => { return temp.id === item.id; }); if (checked) { // 反选,从data中删除 index > -1 && data.splice(index, 1); } else { // 全选,data中没有的新增 index === -1 && data.push(item); } }); // data为最终选中的数据(此处使用完整数据不使用id,为了兼容之前的业务逻辑) onChange(data); } }; return ; }; render() { const { columnChsName, columnName, sortFlag, sortConfig, showIndex, columnIndex, questionText, rowSelection, icon, required, leftMargin = 0, columnAttrObj, } = this.props; const { value = [] } = sortConfig || {}; const sort = value.find((item: any) => { return item.colName === columnName; }); const { sortType }: { sortType: string } = sort || {}; let marginLeft: any = 0; if (columnIndex === 0 && (rowSelection || showIndex)) { marginLeft = `${leftMargin + 36}px`; if (!rowSelection) { marginLeft = `${leftMargin + 70}px`; } } const { remark } = columnAttrObj || {}; const tip = remark || questionText; return (
{rowSelection && columnIndex === 0 &&
{this.getCheckbox()}
}
{icon &&
{icon()}
} {columnChsName} {tip && (
?
)}
{sortConfig && !!sortFlag && (
{ if (sortType === 'ASC') { this.removeSort(); return; } this.changeSort('ASC'); }} />
{ if (sortType === 'DESC') { this.removeSort(); return; } this.changeSort('DESC'); }} />
)}
); } }