import React from 'react'; import { message, Popover, Modal } from 'antd'; import { config } from './config'; import style from './Cell.less'; import _ from 'lodash'; import { CellProps, CellState } from './interface'; import EditCell from './EditCell'; import FormHelper from '../utils/formHelper'; import insertMenu from '../assets/insertMenu.png'; import copyMenu from '../assets/copyMenu.png'; import extendMenu from '../assets/extendMenu.png'; import delMenu from '../assets/delMenu.png'; export default class Cell extends React.Component { static getDerivedStateFromProps(nextProps: CellProps, prevState: CellState) { const { prevProps } = prevState; let nextState: CellState = { ...prevState, prevProps: nextProps, editValue: nextProps.cellData, }; if (JSON.stringify(prevProps.cellData) !== JSON.stringify(nextProps.cellData)) { nextState.editValue = nextProps.cellData; } return nextState; } constructor(props: CellProps) { super(props); const { cellData } = props; this.state = { prevProps: props, isEditVisible: false, isMenuVisible: false, editValue: cellData, originValue: [], }; } handleVisibleChange = (visible: boolean) => { if (!visible) { this.emitChange(); } this.setState({ isEditVisible: visible, isMenuVisible: false }); }; menuVisibleChange = (visible: boolean) => { this.setState({ isMenuVisible: visible, isEditVisible: false }); }; handleChange = ({ newValue, originValue, emitFlag }: any) => { this.setState( { editValue: newValue, originValue, }, () => { if (emitFlag) { this.emitChange(); } }, ); }; emitChange = () => { let { originValue, editValue } = this.state; let { cellData } = this.props; if (_.isEqual(cellData, editValue)) { return; } const { updateData, rowData, columnConfig, columnConfig: { key }, } = this.props; const extraData = FormHelper.changeTableData({ item: columnConfig, changedKey: key, changedValue: editValue, originValue, }); extraData.push({ columnCode: key, cellValueList: editValue, }); if (typeof updateData === 'function') { updateData({ rowId: rowData.id, value: extraData, }); } this.setState({ editValue: [], originValue: [], }); }; copy = (record: any) => { const { showForm } = this.props; if (typeof showForm === 'function') { showForm({ rowData: record.rowData }); } }; delData = (record: any) => { const { delData, getData, hasGroup, tableId } = this.props; // 拆分情况需检查数据是否原始数据 if (hasGroup) { if (record.id === record.groupId) { Modal.warning({ title: '删除', content: '不能删除原始拆分数据' }); return; } } Modal.confirm({ title: '确认要删除该条数据吗?', autoFocusButton: null, onOk: async () => { if (typeof delData === 'function') { const response = await delData({ tableId, data: { id: record.id } }); if (response && response.success) { message.success('删除成功'); if (typeof getData === 'function') { getData({ isClean: true }); } } } }, }); }; clickExtraMenu = (menu: any, record: any) => { if (menu.check && typeof menu.check === 'function') { menu.check({ data: [record] }, this.menuClickFunc.bind(this, menu, record)); } else { this.menuClickFunc(menu, record); } }; menuClickFunc = async (menu: any, record: any) => { const { flush } = this.props; if (typeof menu.onClick === 'function') { await menu.onClick({ data: [record], flush }); } if (!menu.noNeedFlush) { if (typeof flush === 'function') { flush(); } } }; renderPopEdit = ({ columnAttrObj, component }: any) => { let { columnConfig, rowData, cellData, columns, tableId } = this.props; let { editValue } = this.state; return ( ); }; renderPopMenu = () => { let { cellData, showForm, extraMenu, noAdd, noDel, hasGroup } = this.props; return (
{!noAdd && (
插入 插入
)} {!noAdd && (
复制 复制
)}
展开 展开
{!noDel && (!hasGroup || (hasGroup && cellData.id !== cellData.groupId)) && (
删除 删除
)} {extraMenu && extraMenu.length > 0 && (
{extraMenu.map((item: any, i: number) => { if (typeof item.hidden === 'function' && item.hidden({ record: cellData, menu: item })) { return; } return (
{item.icon && (typeof item.icon === 'string' ? ( ) : ( {item.icon} ))} {item.label}
); })} )}
); }; renderCellContent = () => { let { columnConfig, cellData, noEdit, tableId } = this.props; let { isEditVisible, isMenuVisible } = this.state; const { readOnlyFlag, columnType } = columnConfig || {}; let { columnAttrObj } = columnConfig || {}; if (config[String(columnType)] === undefined) { return false; } columnAttrObj = columnAttrObj || {}; columnAttrObj.disabled = readOnlyFlag || noEdit || false; const { component } = config[String(columnType)]; return (
); }; render() { const { columnConfig, overlayClassName } = this.props; const { width } = columnConfig || {}; return (
{this.renderCellContent()}
); } }