import React, { useState, useRef } from 'react';
import { Checkbox, message, Popover } from 'antd';
import classNames from 'classnames';
import _ from 'lodash';
import { config } from './base/config';
import { getFormat, setFormat } from './base';
import s from './Cell.less';
import { CellProps, CellDataProps } from './interface';
import FormHelper from '../utils/formHelper';
import expandIcon from '../assets/extend.png';
import { transferAttr } from './base/_utils/transferAttr';
import { emptyModel } from './base/_utils/setFormatter';
enum EDIT_STATUS {
'FREE', // 空闲
'EDITING', // 编辑中
}
const Cell = (props: CellProps) => {
const {
columnConfig,
cellClassName,
cellStyle,
record,
columnData,
value: cellData,
emitChangeCell,
columnIndex,
rowIndex,
paginationConfig,
showIndex,
showExpand,
emptyPlaceholder,
cellEditable,
rowSelection,
columns,
contentMenu,
onEmitMsg,
tableId,
maxPopHeight,
renderFirstLeft,
position,
} = props;
const {
columnType,
columnName,
requiredFlag,
renderDetailCell,
renderEditCell,
cellRenderProps,
readOnlyFlag,
columnAttrObj,
} = columnConfig;
const cellUnit = useRef(null);
const [status, setStatus] = useState('detail');
const resetEditStatus = () => {
setStatus('detail');
if (typeof onEmitMsg === 'function') {
onEmitMsg({ status: EDIT_STATUS.FREE });
}
};
const turnIntoEditStatus = (data: any) => {
setStatus('edit');
if (typeof onEmitMsg === 'function') {
onEmitMsg({ ...data, status: EDIT_STATUS.EDITING });
}
};
const changeCellData = (changedValue: any, option?: any) => {};
/**
*
* @param changedValue 修改的值
* @param optionValue 修改的原始数据
* @param reset 是否重置编辑状态
*/
const emitChangeCellData = (changedValue: any, optionValue: any, reset?: boolean) => {
if (reset) {
resetEditStatus();
return;
}
let temp: CellDataProps[] = [];
cellData &&
cellData.map((item: CellDataProps) => {
temp.push({ text: item.text, value: item.value });
});
if (temp.length === 0) {
temp = emptyModel;
}
if (_.isEqual(temp, changedValue)) {
resetEditStatus();
return;
}
changeValue(changedValue, optionValue);
};
const changeValue = async (changedValue: any, optionValue: any) => {
const extraData = FormHelper.changeTableData({
item: columnConfig,
changedKey: columnName,
changedValue,
originValue: optionValue,
});
extraData.push({
columnCode: columnName,
cellValueList: changedValue,
});
if (typeof emitChangeCell === 'function') {
emitChangeCell({
rowId: record.id,
value: extraData,
});
}
resetEditStatus();
};
// 添加行hover样式
const onMouseEnter = () => {
const doms = document.querySelectorAll(`.table_${tableId}.row_${rowIndex}`);
doms.forEach((dom) => {
dom.classList.add(s.hover);
});
};
// 去除行hover样式
const onMouseLeave = () => {
const doms = document.querySelectorAll(`.table_${tableId}.row_${rowIndex}`);
doms.forEach((dom) => {
dom.classList.remove(s.hover);
});
};
const renderFirst = () => {
const { current = 1, pageSize = 20 } = paginationConfig || {};
// 构造序号
const getIndex = () => {
if (typeof showIndex === 'function') {
return showIndex(props);
}
return (current - 1) * pageSize + rowIndex + 1;
};
// 获取当前行在所选列表中的index
const getCheckedIndex = () => {
if (!rowSelection) {
return -1;
}
const { selectedRows } = rowSelection;
const index =
selectedRows &&
selectedRows.findIndex((item: any) => {
return item.id === record.id;
});
return index;
};
// 构造行复选框
const getCheckbox = () => {
const { selectedRows, onChange } = rowSelection;
const index = getCheckedIndex();
const checked = index > -1;
const onToggle = () => {
if (typeof onChange === 'function') {
const data = _.cloneDeep(selectedRows);
if (checked) {
data.splice(index, 1);
} else {
data.push(record);
}
onChange(data);
}
};
return