UNPKG

linkmore-design

Version:

🌈 🚀lm组件库。🚀

1,145 lines (1,121 loc) 70.7 kB
import _objectDestructuringEmpty from "@babel/runtime/helpers/esm/objectDestructuringEmpty"; import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _extends from "@babel/runtime/helpers/esm/extends"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _excluded = ["changeSize", "columns", "dataSource", "autoSize", "rowClick", "onDoubleClick", "checkConfig", "summary", "pagination", "hiddenPage", "loading", "virtual", "colSortOpen", "resizeable", "customCheck", "components", "rowSelection", "columnsState", "onChange", "filterChange", "tableRowType", "size", "openSheet", "editSheet", "dataChange", "openColGroup", "colGroupTitle", "openRowGroup", "rowGroupTitle", "groupWidth", "groupRowKeys", "groupColKeys", "onGroupChange", "onExpressionChange", "rowStyle", "headerColSort", "colChange", "sortOpen", "rowSortEnd", "emptyProps", "showClickBorder", "selectRowChange", "roleFiledKeys", "columnsValueDecimal", "columnsMinWidth", "rowClassName"], _excluded2 = ["onResize", "width", "dataIndex", "onResizeStop"]; import { arrayMove } from '@dnd-kit/sortable'; import { useUpdate } from 'ahooks'; import cls from 'classnames'; import { produce } from 'immer'; import { cloneDeep, compact, difference, isBoolean, isFunction, isObject, omit, throttle, uniqBy } from 'lodash'; import React, { forwardRef, memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Resizable } from 'react-resizable'; import { toFixedNumber } from "./toFixed"; import ConfigProvider from "../config-provider"; import Empty from "../empty"; import Table from "../table"; import useControllableValue from "./hooks/useRowSelectControllableValue"; import CalcExpression from "./components/CalcExpression"; import SheelTabelCell from "./components/sheelTableCell"; /** 表表列排序组件 */ import DndContainer from "./components/DndContainer"; /** 表格行排序组件 */ import DraggableContainerRow from "./components/rowSort/DraggableContainer"; import DragRowHandle from "./components/rowSort/Dragger"; import SortableItem from "./components/rowSort/sortableItem"; import { checkIsSelectd, checkStatus, getDataSourceIndex, getLeafNodes, getSelectionColText, getSelectionRowText, isSelectCrossInvalidCol, isSelectCrossRowGroup, range, transformWithColGroup } from "./util"; import ResetConfig from "./resetConfig"; import HeaderCellContent from "./components/HeaderCol"; import HeaderRowContent from "./components/HeaderRow"; import HeaderRowSortWarp from "./components/headRowSortWarp"; import useDndItems from "./hooks/useDndItems"; import useGroupDataSource from "./hooks/useGroupDataSource"; import { VirtualRow, VirtualTable, VirtualWrapper } from "./virTual"; import { treeFind } from "./common/index"; var Summary = Table.Summary; var ConfigContext = ConfigProvider.ConfigContext; var isEmpty = function isEmpty(obj) { var _Object$keys; return ((_Object$keys = Object.keys(obj || {})) === null || _Object$keys === void 0 ? void 0 : _Object$keys.length) === 0; }; var defaultParsePaste = function defaultParsePaste(str) { return str.split(/\r\n|\n|\r/).map(function (row) { return row.split('\t'); }); }; var decimalLists = ['amount', 'price', 'number']; var ResetTable = /*#__PURE__*/forwardRef(function (props, ref) { var _useSelectedRows$sele; var changeSize = props.changeSize, customizeColumns = props.columns, customizeDataSource = props.dataSource, autoSize = props.autoSize, rowClick = props.rowClick, _onDoubleClick = props.onDoubleClick, checkConfig = props.checkConfig, _summary = props.summary, pagination = props.pagination, hiddenPage = props.hiddenPage, loading = props.loading, _props$virtual = props.virtual, virtual = _props$virtual === void 0 ? false : _props$virtual, _props$colSortOpen = props.colSortOpen, colSortOpen = _props$colSortOpen === void 0 ? false : _props$colSortOpen, _props$resizeable = props.resizeable, resizeable = _props$resizeable === void 0 ? false : _props$resizeable, customCheck = props.customCheck, components = props.components, rowSelection = props.rowSelection, columnsState = props.columnsState, onChange = props.onChange, filterChange = props.filterChange, _props$tableRowType = props.tableRowType, tableRowType = _props$tableRowType === void 0 ? 'default' : _props$tableRowType, _props$size = props.size, size = _props$size === void 0 ? 'default' : _props$size, _props$openSheet = props.openSheet, openSheet = _props$openSheet === void 0 ? false : _props$openSheet, _props$editSheet = props.editSheet, editSheet = _props$editSheet === void 0 ? false : _props$editSheet, dataChange = props.dataChange, openColGroup = props.openColGroup, colGroupTitle = props.colGroupTitle, openRowGroup = props.openRowGroup, rowGroupTitle = props.rowGroupTitle, groupWidth = props.groupWidth, customizeGroupRowKeys = props.groupRowKeys, customizeGroupColKeys = props.groupColKeys, _props$onGroupChange = props.onGroupChange, onGroupChange = _props$onGroupChange === void 0 ? function () {} : _props$onGroupChange, onExpressionChange = props.onExpressionChange, rowStyle = props.rowStyle, headerColSort = props.headerColSort, colChange = props.colChange, _props$sortOpen = props.sortOpen, sortOpen = _props$sortOpen === void 0 ? false : _props$sortOpen, rowSortEnd = props.rowSortEnd, emptyProps = props.emptyProps, _props$showClickBorde = props.showClickBorder, showClickBorder = _props$showClickBorde === void 0 ? false : _props$showClickBorde, selectRowChange = props.selectRowChange, _props$roleFiledKeys = props.roleFiledKeys, roleFiledKeys = _props$roleFiledKeys === void 0 ? [] : _props$roleFiledKeys, _props$columnsValueDe = props.columnsValueDecimal, columnsValueDecimal = _props$columnsValueDe === void 0 ? { amount: 2 } : _props$columnsValueDe, _props$columnsMinWidt = props.columnsMinWidth, columnsMinWidth = _props$columnsMinWidt === void 0 ? 56 : _props$columnsMinWidt, rowClassName = props.rowClassName, resetProps = _objectWithoutProperties(props, _excluded); if (!Array.isArray(customizeColumns) || !customizeColumns.length) { return null; } var _resetProps$rowKey = resetProps.rowKey, rowKey = _resetProps$rowKey === void 0 ? 'id' : _resetProps$rowKey; var _React$useContext = React.useContext(ConfigContext), locale = _React$useContext.locale; var transformRowClick = function transformRowClick(data) { return rowClick === null || rowClick === void 0 ? void 0 : rowClick(data.selectedRows || data); }; // const [useSelectedRows, setSelectedRows] = useState({ selectedRows: [] }) // 复选中的值 var _useControllableValue = useControllableValue({ value: { selectedRows: (rowSelection === null || rowSelection === void 0 ? void 0 : rowSelection.selectedRows) || (rowSelection === null || rowSelection === void 0 ? void 0 : rowSelection.selectedRowKeys) || [] }, onChange: rowClick ? transformRowClick : undefined }), _useControllableValue2 = _slicedToArray(_useControllableValue, 2), useSelectedRows = _useControllableValue2[0], setSelectedRows = _useControllableValue2[1]; var _useState = useState(''), _useState2 = _slicedToArray(_useState, 2), useActiveKey = _useState2[0], setActiveKey = _useState2[1]; // 行选中 var tableWarpRef = useRef(null); var tableRef = useRef(null); /** 记录最原始数据 */ var dataSourceRef = useRef(null); dataSourceRef.current = customizeDataSource; var columnsRef = useRef(null); columnsRef.current = customizeColumns; /** 右键的ref */ var contentMenuRef = useRef(null); /** 当前展开的列 */ var _useState3 = useState([]), _useState4 = _slicedToArray(_useState3, 2), expandedRowKeys = _useState4[0], setExpandedRowKeys = _useState4[1]; var deepDataSourceRef = useRef([]); var deepColumnsRef = useRef([]); // var copyAllData = useRef(null); // var update = useUpdate(); /** 最后一个点击的行 */ var _useState5 = useState(null), _useState6 = _slicedToArray(_useState5, 2), lastClickRow = _useState6[0], setLastClickRow = _useState6[1]; useEffect(function () { if (showClickBorder && selectRowChange) { var _dataSourceRef$curren; var item = (_dataSourceRef$curren = dataSourceRef.current) === null || _dataSourceRef$curren === void 0 ? void 0 : _dataSourceRef$curren.find(function (item) { return item[rowKey] === lastClickRow; }); selectRowChange === null || selectRowChange === void 0 ? void 0 : selectRowChange(item || lastClickRow); } }, [lastClickRow]); var _useDndItems = useDndItems({ columns: customizeColumns, openColGroup: openColGroup, openRowGroup: openRowGroup, colSortOpen: colSortOpen, filterChange: filterChange, customizeGroupRowKeys: customizeGroupRowKeys, customizeGroupColKeys: customizeGroupColKeys }), dndColumns = _useDndItems.dndColumns, items = _useDndItems.items, groupRowKeys = _useDndItems.groupRowKeys, groupColKeys = _useDndItems.groupColKeys, updateItems = _useDndItems.updateItems; onGroupChange && onGroupChange(groupRowKeys, groupColKeys); /** 行分组 */ var _useGroupDataSource = useGroupDataSource({ groupRowKeys: groupRowKeys, customizeDataSource: customizeDataSource, columnsRef: columnsRef }), _useGroupDataSource2 = _slicedToArray(_useGroupDataSource, 1), groupDataSource = _useGroupDataSource2[0]; /** 列分组 */ var _useMemo = useMemo(function () { if (!(groupColKeys !== null && groupColKeys !== void 0 && groupColKeys.length)) { return { dataSource: groupDataSource, columns: dndColumns }; } return transformWithColGroup(dndColumns, groupDataSource, groupColKeys, groupRowKeys); }, [dndColumns, groupDataSource, groupColKeys, groupRowKeys]), columns = _useMemo.columns, dataSource = _useMemo.dataSource; useEffect(function () { setExpandedRowKeys([]); deepDataSourceRef.current = []; deepColumnsRef.current = []; }, [groupRowKeys, groupColKeys]); /** sheel数据的表格 */ var _useState7 = useState({ // 记录框选的行列坐标信息 start: {}, end: {}, // 是否在框选 selecting: false, forceEdit: false, // 是否在编辑中 editing: {}, clear: {}, // 记录右下角小方块的拖动信息 commiting: {} }), _useState8 = _slicedToArray(_useState7, 2), sheelStataus = _useState8[0], setSheelStatus = _useState8[1]; var shellStatusRef = useRef({ start: {}, end: {}, selecting: false, forceEdit: false, editing: {}, clear: {}, commiting: {} }); var _useState9 = useState(false), _useState10 = _slicedToArray(_useState9, 2), selectIng = _useState10[0], setSelectIng = _useState10[1]; var _useState11 = useState(false), _useState12 = _slicedToArray(_useState11, 2), editIng = _useState12[0], setEditIng = _useState12[1]; var _useState13 = useState(false), _useState14 = _slicedToArray(_useState13, 2), commitIng = _useState14[0], setCommitIng = _useState14[1]; /** 右键菜单是否显示 */ var _useState15 = useState(false), _useState16 = _slicedToArray(_useState15, 2), contextMenuStatus = _useState16[0], setContextMenuStatus = _useState16[1]; /** 右键按钮的位置 */ var _useState17 = useState({ position: 'fixed', left: 300, top: 200 }), _useState18 = _slicedToArray(_useState17, 2), style = _useState18[0], setStyle = _useState18[1]; // 列设置 var _useState19 = useState(false), _useState20 = _slicedToArray(_useState19, 2), columnsStateMap = _useState20[0], setColumnsStateMap = _useState20[1]; // 行分组数据铺平, 在框选时获取数据使用 useEffect(function () { var source = []; var deepChildren = function deepChildren(arr) { arr === null || arr === void 0 ? void 0 : arr.forEach(function (item) { source.push(omit(item, 'children')); // 展开 if (item !== null && item !== void 0 && item.children && expandedRowKeys.includes(item.key)) { deepChildren(item === null || item === void 0 ? void 0 : item.children); } }); }; deepChildren(dataSource); deepDataSourceRef.current = source; }, [dataSource, expandedRowKeys]); // 列分组数据铺平,在框选时获取数据使用 useEffect(function () { var source = []; var deepChildren = function deepChildren(arr) { arr === null || arr === void 0 ? void 0 : arr.forEach(function (item) { if (item.children && item.children.length) { deepChildren(item.children); } else { source.push(item); } }); }; deepChildren(columns); deepColumnsRef.current = source; }, [columns, groupColKeys]); var _useState21 = useState(columns), _useState22 = _slicedToArray(_useState21, 2), useColumns = _useState22[0], setColumns = _useState22[1]; useEffect(function () { setColumns(columns); }, [columns]); var onSortEnd = function onSortEnd(active, over) { var cloneArr = _toConsumableArray(dataSourceRef.current); var oldIndex = cloneArr.findIndex(function (v) { return v[rowKey] === active; }); var newIndex = cloneArr.findIndex(function (v) { return v[rowKey] === over; }); rowSortEnd === null || rowSortEnd === void 0 ? void 0 : rowSortEnd(arrayMove(cloneArr, oldIndex, newIndex)); }; var DraggableContainer = function DraggableContainer(props) { var _tableWarpRef$current, _tableWarpRef$current2, _dataSourceRef$curren2; return /*#__PURE__*/React.createElement(DraggableContainerRow, _extends({}, props, { virtual: true, options: dataSourceRef.current, rowKey: rowKey, onSortEnd: onSortEnd, tableWidth: tableWarpRef === null || tableWarpRef === void 0 ? void 0 : (_tableWarpRef$current = tableWarpRef.current) === null || _tableWarpRef$current === void 0 ? void 0 : (_tableWarpRef$current2 = _tableWarpRef$current.getBoundingClientRect()) === null || _tableWarpRef$current2 === void 0 ? void 0 : _tableWarpRef$current2.width, columns: columnsRef.current || resultColumns, keys: (_dataSourceRef$curren2 = dataSourceRef.current) === null || _dataSourceRef$curren2 === void 0 ? void 0 : _dataSourceRef$curren2.map(function (item) { return item[rowKey]; }) })); }; var DraggableBodyRow = function DraggableBodyRow(props) { return /*#__PURE__*/React.createElement(SortableItem, _extends({ virtual: true }, props)); }; var sortDefaultColumnItem = { title: '排序', dataIndex: 'sort', width: 54, maxWidth: 54, fixed: 'left', order: -2, className: 'drag-visible', render: function render(_, record) { return /*#__PURE__*/React.createElement(DragRowHandle, { id: (record === null || record === void 0 ? void 0 : record[rowKey]) || (record === null || record === void 0 ? void 0 : record.id) }); } }; /** 列宽拖动时速度 */ var handleResize = function handleResize(index) { return function (e, _ref) { var size = _ref.size; var i = index; var nextColumns = _toConsumableArray(useColumns); nextColumns.forEach(function (item, index) { if (index <= i && item.show === false) { i += 1; } }); nextColumns[i] = _objectSpread(_objectSpread({}, nextColumns[i]), {}, { width: size.width }); setColumns(nextColumns); }; }; /** 列宽拖动结束事件 */ var endResize = function endResize() { setTimeout(function () { filterChange === null || filterChange === void 0 ? void 0 : filterChange(useColumns); }); }; /** 表格sheet化事件中心START */ var isSelected = function isSelected(i, j) { var _shellStatusRef$curre = shellStatusRef.current, start = _shellStatusRef$curre.start, end = _shellStatusRef$curre.end, editing = _shellStatusRef$curre.editing, commiting = _shellStatusRef$curre.commiting; var maxi = Math.max(start === null || start === void 0 ? void 0 : start.i, end.i); var mini = Math.min(start === null || start === void 0 ? void 0 : start.i, end.i); var maxj = Math.max(start === null || start === void 0 ? void 0 : start.j, end.j); var minj = Math.min(start === null || start === void 0 ? void 0 : start.j, end.j); var isSelected = checkIsSelectd(i, j, start, end); return { /** 是否选中 */ isSelected: isSelected, /** 是否尾行 */ isEnd: i === maxi && j >= minj && j <= maxj, /** 是否尾列 */ isRightEnd: j === maxj && i >= mini && i <= maxi, /** 是否在编辑中 */ isEditing: (editing === null || editing === void 0 ? void 0 : editing.i) === i && (editing === null || editing === void 0 ? void 0 : editing.j) === j, /** 是否有效拉伸 */ isVaildCommit: isEmpty(commiting) ? false : isSelected && !checkIsSelectd(i, j, commiting === null || commiting === void 0 ? void 0 : commiting.start, commiting === null || commiting === void 0 ? void 0 : commiting.end), /** 是否选中只读框 */ // TODO selectedReadonly: true }; }; /** 获取框选的数据 */ var getSelectDatas = function getSelectDatas(start, end) { return range(start.i, end.i).map(function (i) { return range(start.j, end.j).map(function (j) { var _columns$j, _columns$j2; var columnKey = groupColKeys !== null && groupColKeys !== void 0 && groupColKeys.length ? deepColumnsRef.current[j].dataIndex : (_columns$j = columns[j]) === null || _columns$j === void 0 ? void 0 : _columns$j.dataIndex; var dataItem = groupRowKeys !== null && groupRowKeys !== void 0 && groupRowKeys.length ? deepDataSourceRef.current[i] : dataSource[i]; var cell = dataItem === null || dataItem === void 0 ? void 0 : dataItem[columnKey]; if ((_columns$j2 = columns[j]) !== null && _columns$j2 !== void 0 && _columns$j2.render) { var _columns$j3, _columns$j3$render; return (_columns$j3 = columns[j]) === null || _columns$j3 === void 0 ? void 0 : (_columns$j3$render = _columns$j3.render) === null || _columns$j3$render === void 0 ? void 0 : _columns$j3$render.call(_columns$j3, cell, dataItem, i); } return cell; }).join('\t'); }).join('\n'); }; /** 复制选中内容 */ var handleCopy = function handleCopy(e) { var _e$clipboardData; e === null || e === void 0 ? void 0 : e.preventDefault(); var _shellStatusRef$curre2 = shellStatusRef.current, start = _shellStatusRef$curre2.start, end = _shellStatusRef$curre2.end; var text = copyAllData.current || getSelectDatas(start, end); if (window.clipboardData && window.clipboardData.setData) { window.clipboardData.setData('Text', text); document.removeEventListener('copy', handleCopy); } else if ((_e$clipboardData = e.clipboardData) !== null && _e$clipboardData !== void 0 && _e$clipboardData.setData) { e.clipboardData.setData('text/plain', text); document.removeEventListener('copy', handleCopy); } else if (!copyAllData.current) { var _document; (_document = document) === null || _document === void 0 ? void 0 : _document.execCommand('copy'); if (contextMenuStatus) { setContextMenuStatus(false); } } copyAllData.current = null; }; /** 复制所有数据 */ var handleCopyAll = function handleCopyAll(e) { var _document2; e === null || e === void 0 ? void 0 : e.preventDefault(); var source = dataSourceRef.current; var columns = columnsRef.current; var text = source.map(function (rItem, i) { return columns.map(function (cItem, j) { var _columns$j4, _columns$j5; var columnKey = (_columns$j4 = columns[j]) === null || _columns$j4 === void 0 ? void 0 : _columns$j4.dataIndex; var dataItem = dataSource[i]; var cell = dataItem === null || dataItem === void 0 ? void 0 : dataItem[columnKey]; if ((_columns$j5 = columns[j]) !== null && _columns$j5 !== void 0 && _columns$j5.render) { var _columns$j6, _columns$j6$render; return (_columns$j6 = columns[j]) === null || _columns$j6 === void 0 ? void 0 : (_columns$j6$render = _columns$j6.render) === null || _columns$j6$render === void 0 ? void 0 : _columns$j6$render.call(_columns$j6, cell, dataItem, i); } return cell; }).join('\t'); }).join('\n'); copyAllData.current = text; (_document2 = document) === null || _document2 === void 0 ? void 0 : _document2.execCommand('copy'); setContextMenuStatus(false); }; var onMouseOver = function onMouseOver(i, j) { var _shellStatusRef$curre3, _shellStatusRef$curre6, _checkStatus, _shellStatusRef$curre7; if (isEmpty(shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre3 = shellStatusRef.current) === null || _shellStatusRef$curre3 === void 0 ? void 0 : _shellStatusRef$curre3.commiting)) { var _shellStatusRef$curre4, _shellStatusRef$curre5; if (shellStatusRef !== null && shellStatusRef !== void 0 && (_shellStatusRef$curre4 = shellStatusRef.current) !== null && _shellStatusRef$curre4 !== void 0 && _shellStatusRef$curre4.selecting && isEmpty(shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre5 = shellStatusRef.current) === null || _shellStatusRef$curre5 === void 0 ? void 0 : _shellStatusRef$curre5.editing)) { shellStatusRef.current = _objectSpread(_objectSpread({}, shellStatusRef.current), {}, { end: { i: i, j: j } }); setSheelStatus(_objectSpread(_objectSpread({}, sheelStataus), {}, { end: { i: i, j: j } })); } } else if (!isEmpty((shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre6 = shellStatusRef.current) === null || _shellStatusRef$curre6 === void 0 ? void 0 : _shellStatusRef$curre6.commiting) || {}) && (_checkStatus = checkStatus(i, j, shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre7 = shellStatusRef.current) === null || _shellStatusRef$curre7 === void 0 ? void 0 : _shellStatusRef$curre7.commiting)) !== null && _checkStatus !== void 0 && _checkStatus.isVaildCommit) { if (isSelectCrossRowGroup(i, shellStatusRef, deepDataSourceRef)) { return; } if (isSelectCrossInvalidCol(i, j, shellStatusRef, deepDataSourceRef, deepColumnsRef)) { return; } shellStatusRef.current = _objectSpread(_objectSpread({}, shellStatusRef.current), {}, { end: { i: i, j: j } }); setSheelStatus(_objectSpread(_objectSpread({}, sheelStataus), {}, { end: { i: i, j: j } })); } }; // 通过表格的行列索引,获取对应的原始数据的item信息 var getSourceItemInfo = function getSourceItemInfo(trIndex, tdIndex) { var innerColumns = groupColKeys !== null && groupColKeys !== void 0 && groupColKeys.length ? deepColumnsRef.current : columns; var columnsKeysList = innerColumns.map(function (item) { return item.dataIndex; }); var key = columnsKeysList[tdIndex]; var index; var nil = { index: -1, key: undefined }; if (key === undefined) { return nil; } // 列分组的情况下 if (groupColKeys !== null && groupColKeys !== void 0 && groupColKeys.length) { var _deepDataSourceRef$cu; if (groupRowKeys !== null && groupRowKeys !== void 0 && groupRowKeys.length && deepDataSourceRef !== null && deepDataSourceRef !== void 0 && (_deepDataSourceRef$cu = deepDataSourceRef.current) !== null && _deepDataSourceRef$cu !== void 0 && _deepDataSourceRef$cu[trIndex]._group) { return nil; } index = getDataSourceIndex({ key: key, dataSource: dataSource }); var arr = key.split('_'); key = arr[arr.length - 2]; } else if (groupRowKeys.length) { index = deepDataSourceRef.current[trIndex].__index; } else { index = trIndex; } return { index: index, key: key }; }; /** sheel 表格 鼠标松开 */ /** TODO: 在分组的情况,需要排除_group行的数据转换,以及要进行对子级的覆盖 */ var sheelOnMouseUp = function sheelOnMouseUp() { var _shellStatusRef$curre8; if (!isEmpty((_shellStatusRef$curre8 = shellStatusRef.current) === null || _shellStatusRef$curre8 === void 0 ? void 0 : _shellStatusRef$curre8.commiting)) { var _commiting$start, _commiting$end, _commiting$start2, _commiting$end2, _difference, _difference2, _obj$i; var _shellStatusRef$curre9 = shellStatusRef.current, start = _shellStatusRef$curre9.start, end = _shellStatusRef$curre9.end, commiting = _shellStatusRef$curre9.commiting; // var innerColumns = groupColKeys !== null && groupColKeys !== void 0 && groupColKeys.length ? deepColumnsRef.current : columns; var innerDataSource = groupRowKeys !== null && groupRowKeys !== void 0 && groupRowKeys.length ? deepDataSourceRef.current : dataSource; // var columnsKeysList = innerColumns.map(function (item) { return item.dataIndex; }); // 行文本 var rowtext = getSelectionRowText({ selection: commiting, columnsKeysList: columnsKeysList, dataSource: innerDataSource }); // 列文本 var coltext = getSelectionColText({ selection: commiting, columns: innerColumns, dataSource: innerDataSource }); /** 选择范围 例如:i:[0,1], j:[1,2] */ var selectedRange = { i: range(start.i, end.i), j: range(start.j, end.j) }; /** 提交范围 */ var commitRange = { i: range(commiting === null || commiting === void 0 ? void 0 : (_commiting$start = commiting.start) === null || _commiting$start === void 0 ? void 0 : _commiting$start.i, commiting === null || commiting === void 0 ? void 0 : (_commiting$end = commiting.end) === null || _commiting$end === void 0 ? void 0 : _commiting$end.i), j: range(commiting === null || commiting === void 0 ? void 0 : (_commiting$start2 = commiting.start) === null || _commiting$start2 === void 0 ? void 0 : _commiting$start2.j, commiting === null || commiting === void 0 ? void 0 : (_commiting$end2 = commiting.end) === null || _commiting$end2 === void 0 ? void 0 : _commiting$end2.j) }; // obj 为新增的范围 var obj = { i: [], j: [] }; var type = ''; if ((_difference = difference(selectedRange.i, commitRange.i)) !== null && _difference !== void 0 && _difference.length) { obj = { i: difference(selectedRange.i, commitRange.i), j: selectedRange.j }; type = 'row'; } else if ((_difference2 = difference(selectedRange.j, commitRange.j)) !== null && _difference2 !== void 0 && _difference2.length) { obj = { i: selectedRange.i, j: difference(selectedRange.j, commitRange.j) }; type = 'col'; } var data = cloneDeep(dataSourceRef.current); (_obj$i = obj.i) === null || _obj$i === void 0 ? void 0 : _obj$i.forEach(function (tri, index) { var _obj$j; (_obj$j = obj.j) === null || _obj$j === void 0 ? void 0 : _obj$j.forEach(function (tdj) { var _isSelected; if ((_isSelected = isSelected(tri, tdj)) !== null && _isSelected !== void 0 && _isSelected.isSelected) { var _deepColumnsRef$curre, _deepColumnsRef$curre2; // 只读列无法修改其内容 if (deepColumnsRef !== null && deepColumnsRef !== void 0 && (_deepColumnsRef$curre = deepColumnsRef.current) !== null && _deepColumnsRef$curre !== void 0 && (_deepColumnsRef$curre2 = _deepColumnsRef$curre[tdj]) !== null && _deepColumnsRef$curre2 !== void 0 && _deepColumnsRef$curre2.readOnly) { return; } var _getSourceItemInfo = getSourceItemInfo(tri, tdj), sourceIndex = _getSourceItemInfo.index, key = _getSourceItemInfo.key; if (sourceIndex < 0 || sourceIndex >= data.length) { return; } // 行替换 if (type === 'row') { var _rowtext; data[sourceIndex][key] = (_rowtext = rowtext[index % rowtext.length]) === null || _rowtext === void 0 ? void 0 : _rowtext[tdj]; } else { var _coltext; // 列替换 data[sourceIndex][key] = (_coltext = coltext[index % coltext.length]) === null || _coltext === void 0 ? void 0 : _coltext[tdj % coltext[index % coltext.length].length]; } } }); }); if (editSheet) { dataChange === null || dataChange === void 0 ? void 0 : dataChange(data); } setSheelStatus(_objectSpread(_objectSpread({}, sheelStataus), {}, { selecting: false, commiting: {} })); shellStatusRef.current = _objectSpread(_objectSpread({}, shellStatusRef.current), {}, { selecting: false, commiting: {} }); setCommitIng(false); } else { setSheelStatus(_objectSpread(_objectSpread({}, sheelStataus), {}, { selecting: false })); shellStatusRef.current = _objectSpread(_objectSpread({}, shellStatusRef.current), {}, { selecting: false }); } document.removeEventListener('mouseup', sheelOnMouseUp); }; /** 剪切内容 */ var handleCut = function handleCut(e) { var _shellStatusRef$curre10; if (isEmpty((_shellStatusRef$curre10 = shellStatusRef.current) === null || _shellStatusRef$curre10 === void 0 ? void 0 : _shellStatusRef$curre10.editing)) { e.preventDefault(); handleCopy(e); /** 暂时先不隐藏 */ // const { start, end } = shellStatusRef.current // clearSelectedCells(start, end) } }; /** 内容粘贴 */ /** TODO: 在分组的情况,需要排除_group行的数据转换 */ var handlePaste = function handlePaste(e) { var _shellStatusRef$curre11; if (isEmpty((_shellStatusRef$curre11 = shellStatusRef.current) === null || _shellStatusRef$curre11 === void 0 ? void 0 : _shellStatusRef$curre11.editing)) { var _pasteData; var _shellStatusRef$curre12 = shellStatusRef.current, start = _shellStatusRef$curre12.start, end = _shellStatusRef$curre12.end; start = { i: Math.min(start.i, end.i), j: Math.min(start.j, end.j) }; end = { i: Math.max(start.i, end.i), j: Math.max(start.j, end.j) }; var parse = defaultParsePaste; var pasteData = []; if (window.clipboardData && window.clipboardData.getData) { // IE pasteData = parse(window.clipboardData.getData('Text')); } else if (e.clipboardData && e.clipboardData.getData) { pasteData = parse(e.clipboardData.getData('text/plain')); } // in order of preference var resultEnd = []; (_pasteData = pasteData) === null || _pasteData === void 0 ? void 0 : _pasteData.forEach(function (row, i) { row === null || row === void 0 ? void 0 : row.forEach(function (value, j) { resultEnd.push({ i: start.i + i, j: start.j + j, value: value }); }); }); var res = produce(dataSourceRef.current, function (draft) { resultEnd === null || resultEnd === void 0 ? void 0 : resultEnd.forEach(function (item) { var _isSelected2; if ((_isSelected2 = isSelected(item.i, item.j)) !== null && _isSelected2 !== void 0 && _isSelected2.isSelected) { var _getSourceItemInfo2 = getSourceItemInfo(item.i, item.j), index = _getSourceItemInfo2.index, key = _getSourceItemInfo2.key; if (index < 0 || index >= draft.length) { return; } draft[index][key] = item.value; } }); }); /** 只有开通了编辑权限之后才可以 */ if (editSheet) { dataChange === null || dataChange === void 0 ? void 0 : dataChange(res); } } // 修复重复问题 document.removeEventListener('paste', handlePaste); document.removeEventListener('copy', handleCopy); document.removeEventListener('cut', handleCut); }; var pageClick = function pageClick(e) { // if (this.props.disablePageClick) return; var element = tableWarpRef.current; if (!(element !== null && element !== void 0 && element.contains(e.target))) { shellStatusRef.current = { start: {}, end: {}, selecting: false, forceEdit: false, editing: {}, clear: {}, commiting: {} }; setSelectIng(false); document.removeEventListener('mousedown', pageClick); document.removeEventListener('mouseup', sheelOnMouseUp); document.removeEventListener('cut', handleCut); document.removeEventListener('copy', handleCopy); document.removeEventListener('paste', handlePaste); } /** 关闭右键 */ var contentElement = contentMenuRef.current; if (!(contentElement !== null && contentElement !== void 0 && contentElement.contains(e.target))) { setContextMenuStatus(false); } }; /** sheel 表格 鼠标点击 */ /** * i 行 * j 列 * e 点击元素 * commitStatus boolena 是否点击了快速选择 */ var sheelMouseDown = function sheelMouseDown(i, j, e, commitStatus) { var _shellStatusRef$curre13, _shellStatusRef$curre14, _shellStatusRef$curre15, _shellStatusRef$curre16, _shellStatusRef$curre17, _shellStatusRef$curre18, _shellStatusRef$curre19, _shellStatusRef$curre20, _shellStatusRef$curre21, _shellStatusRef$curre22; var isNowEditingSameCell = !isEmpty((_shellStatusRef$curre13 = shellStatusRef.current) === null || _shellStatusRef$curre13 === void 0 ? void 0 : _shellStatusRef$curre13.editing) && ((_shellStatusRef$curre14 = shellStatusRef.current) === null || _shellStatusRef$curre14 === void 0 ? void 0 : _shellStatusRef$curre14.editing.i) === i && ((_shellStatusRef$curre15 = shellStatusRef.current) === null || _shellStatusRef$curre15 === void 0 ? void 0 : _shellStatusRef$curre15.editing.j) === j; var editing = isEmpty((_shellStatusRef$curre16 = shellStatusRef.current) === null || _shellStatusRef$curre16 === void 0 ? void 0 : _shellStatusRef$curre16.editing) || ((_shellStatusRef$curre17 = shellStatusRef.current) === null || _shellStatusRef$curre17 === void 0 ? void 0 : _shellStatusRef$curre17.editing.i) !== i || ((_shellStatusRef$curre18 = shellStatusRef.current) === null || _shellStatusRef$curre18 === void 0 ? void 0 : _shellStatusRef$curre18.editing.j) !== j ? {} : (_shellStatusRef$curre19 = shellStatusRef.current) === null || _shellStatusRef$curre19 === void 0 ? void 0 : _shellStatusRef$curre19.editing; shellStatusRef.current = { selecting: !isNowEditingSameCell, start: e !== null && e !== void 0 && e.shiftKey || commitStatus ? (_shellStatusRef$curre20 = shellStatusRef.current) === null || _shellStatusRef$curre20 === void 0 ? void 0 : _shellStatusRef$curre20.start : { i: i, j: j }, end: { i: i, j: j }, editing: editing, forceEdit: !!isNowEditingSameCell, commiting: commitStatus ? { start: (_shellStatusRef$curre21 = shellStatusRef.current) === null || _shellStatusRef$curre21 === void 0 ? void 0 : _shellStatusRef$curre21.start, end: { i: i, j: j } } : {} }; setSelectIng(!isNowEditingSameCell); if (commitStatus) { setCommitIng(true); } if (isEmpty((_shellStatusRef$curre22 = shellStatusRef.current) === null || _shellStatusRef$curre22 === void 0 ? void 0 : _shellStatusRef$curre22.editing)) { setEditIng(false); } document.addEventListener('mouseup', sheelOnMouseUp); document.addEventListener('mousedown', pageClick); document.addEventListener('cut', handleCut); document.addEventListener('copy', handleCopy); document.addEventListener('paste', handlePaste); }; /** 鼠标右键点击事件 */ var onContextMenu = function onContextMenu(evt, i, j) { evt.preventDefault(); var _shellStatusRef$curre23 = shellStatusRef.current, start = _shellStatusRef$curre23.start, end = _shellStatusRef$curre23.end; /** 如果表格当前没有选择,则把当前的右击的td块默认选中 */ if (isEmpty(start) && isEmpty(end)) { sheelMouseDown(i, j, evt); } if (!checkIsSelectd(i, j, start, end)) { sheelMouseDown(i, j, evt); } setContextMenuStatus(true); // 获得点击的位置 var clientX = evt.clientX, clientY = evt.clientY; // 文档显示区的宽度 var screenW = window.innerWidth; var screenH = window.innerHeight; // 右键菜单的宽度 // const rightClickRefW = rightClickRef.current.offsetWidth; // const rightClickRefH = rightClickRef.current.offsetHeight; var rightClickRefW = 200; var rightClickRefH = 200; // right为true,说明鼠标点击的位置到浏览器的右边界的宽度可以放下contextmenu。 // 否则,菜单放到左边。 var right = screenW - clientX > rightClickRefW; var top = screenH - clientY > rightClickRefH; // 赋值右键菜单离鼠标一些距离 clientX = right ? clientX + 6 : clientX - rightClickRefW - 6; clientY = top ? clientY + 6 : clientY - rightClickRefH - 6; setStyle(_objectSpread(_objectSpread({}, style), {}, { left: clientX, top: clientY })); }; var onSheelDoubleClick = function onSheelDoubleClick(i, j, col) { if (editSheet) { if (!col.readOnly) { shellStatusRef.current = _objectSpread(_objectSpread({}, shellStatusRef.current), {}, { editing: { i: i, j: j }, forceEdit: true, clear: {} }); setEditIng(true); } } }; var dataSourceChange = function dataSourceChange(i, j, value) { var _getSourceItemInfo3 = getSourceItemInfo(i, j), index = _getSourceItemInfo3.index, key = _getSourceItemInfo3.key; if (index < 0 || index >= dataSourceRef.current.length) { return; } // 表格中的行数 转化为 对应的数据源索引 var res = produce(dataSourceRef.current, function (draft) { draft[index][key] = value; }); dataChange === null || dataChange === void 0 ? void 0 : dataChange(res); shellStatusRef.current = _objectSpread(_objectSpread({}, shellStatusRef.current), {}, { editing: {}, forceEdit: false, clear: {} }); }; useEffect(function () { return function () { document.removeEventListener('mousedown', pageClick); document.removeEventListener('mouseup', sheelOnMouseUp); document.removeEventListener('cut', handleCut); document.removeEventListener('copy', handleCopy); document.removeEventListener('paste', handlePaste); }; }, []); /** 表格sheet化事件中心END */ var resetColumns = useCallback(function () { var _roleFiledKeys$filter; var roleHideKeys = (roleFiledKeys === null || roleFiledKeys === void 0 ? void 0 : (_roleFiledKeys$filter = roleFiledKeys.filter(function (item) { return item.type === 0; })) === null || _roleFiledKeys$filter === void 0 ? void 0 : _roleFiledKeys$filter.map(function (item) { return item.key; })) || []; var colIndex = 0; var traverseColumns = function traverseColumns(columns) { var _loop = function _loop(_i) { var col = columns[_i]; var mergeColDecimal = _objectSpread({ number: null, price: null, amount: 2 }, columnsValueDecimal || {}); var decimalType = col.decimalType; var hasDecimal = decimalLists === null || decimalLists === void 0 ? void 0 : decimalLists.includes(decimalType); var align = col.align || (hasDecimal ? 'right' : 'left'); col.align = align; col.render = col.render ? col.render : hasDecimal && !!mergeColDecimal[decimalType] ? function (text) { return /*#__PURE__*/React.createElement("span", { style: { textAlign: 'right' } }, toFixedNumber(text, mergeColDecimal[decimalType])); } : null; if (col.show === false || roleHideKeys.includes(col.roleKey || col.dataIndex)) { columns.splice(_i, 1); _i -= 1; } else if (col.children && col.children.length) { traverseColumns(col.children); } else { // $_ 开头表示私有属性 col.$_colIndex = colIndex; // eslint-disable-next-line no-loop-func col.onHeaderCell = function (column) { return resizeable ? { width: column.width, dataIndex: column.dataIndex, onResize: handleResize(_i), onResizeStop: endResize, minWidth: column.minWidth } : { width: column.width, dataIndex: column.dataIndex }; }; if (openSheet) { col.onCell = function (record, rowIndex) { return { record: record, rowKey: rowKey, col: col, colIndex: col.$_colIndex, selectIng: selectIng, editIng: editIng, commitIng: commitIng, rowIndex: rowIndex, onMouseDown: sheelMouseDown, onMouseOver: onMouseOver, onContextMenu: onContextMenu, isSelected: isSelected, onDoubleClick: onSheelDoubleClick, dataSourceChange: dataSourceChange }; }; } else if (openRowGroup) { col.onCell = function (record) { return { record: record, col: col, onChangeRecord: function onChangeRecord(dfs, dataIndex, value, isUpdate) { dfs(dataSource, dataIndex, value); isUpdate && update(); }, onExpressionChange: onExpressionChange }; }; } colIndex += 1; } i = _i; }; for (var i = 0; i < columns.length; i += 1) { _loop(i); } }; var innerColumns = cloneDeep(useColumns.filter(function (item) { return item.show !== false; })); if (groupRowKeys !== null && groupRowKeys !== void 0 && groupRowKeys.length) { /** 如果是进行了行分组,则会生成一列dataIndex为Group的Col, 如果需要修改此处,请备注原因 */ innerColumns = [{ type: 'name', title: '分组', dataIndex: '_group', key: '_group', width: groupWidth || 100, fixed: 'left' }].concat(_toConsumableArray(innerColumns)); } traverseColumns(innerColumns, null); return sortOpen ? [sortDefaultColumnItem].concat(_toConsumableArray(innerColumns)) : innerColumns; }, [useColumns, sortOpen, selectIng, dataSource, groupRowKeys, commitIng, roleFiledKeys]); // 行点击事件 var onRecord = function onRecord(record) { var _Object$assign = Object.assign(ResetConfig.checkConfig, checkConfig), trigger = _Object$assign.trigger, highlight = _Object$assign.highlight; if (highlight) { setActiveKey(record[rowKey]); } setLastClickRow(record[rowKey]); if (trigger === 'row') { var selectedRows = useSelectedRows.selectedRows; if (showClickBorder) { /** 如果是点击出现框的,则不变 */ setSelectedRows({ selectedRows: selectedRows }); } else if (tableRowType === 'default' && selectedRows.length < 2) { /** TODO: 点击行时默认开启单选模式 */ setSelectedRows({ selectedRows: [isObject(selectedRows[0]) || !selectedRows.length ? record : record[rowKey]] }); } else if (tableRowType === 'select' || tableRowType === 'default' && selectedRows.length) { if ((rowSelection === null || rowSelection === void 0 ? void 0 : rowSelection.type) === 'radio') { setSelectedRows({ selectedRows: [isObject(selectedRows[0]) || !selectedRows.length ? record : record[rowKey]] }); } else { var selectIndex = selectedRows === null || selectedRows === void 0 ? void 0 : selectedRows.findIndex(function (v) { return isObject(v) ? v[rowKey] === record[rowKey] : v === record[rowKey]; }); selectIndex >= 0 ? selectedRows.splice(selectIndex, 1) : selectedRows.push(isObject(selectedRows[0]) || !selectedRows.length ? record : record[rowKey]); setSelectedRows({ selectedRows: cloneDeep(selectedRows) }); } } } else { rowClick === null || rowClick === void 0 ? void 0 : rowClick(record); } }; var restRowClassName = useCallback(function (record, index) { var customRowClassName = (rowClassName === null || rowClassName === void 0 ? void 0 : rowClassName(record, index)) || ''; return cls(customRowClassName, record[rowKey] === useActiveKey && 'row-active', record[rowKey] === lastClickRow && !!showClickBorder && 'row-clicking'); }, [rowClassName, rowKey, useActiveKey, lastClickRow, showClickBorder]); // 复选框配置 var config = useMemo(function () { var autoSizeHeight = autoSize.height; if (autoSizeHeight === '100%' && (openRowGroup || openColGroup)) { autoSizeHeight = 'calc(100% - 100px)'; } return _objectSpread({ defaultSize: 'small', tableLayout: 'fixed', rowKey: rowKey, debounceTime: 300, revalidateOnFocus: false, options: { setting: false, density: false, fullScreen: false, reload: false }, columnsState: { value: columnsStateMap, onChange: function onChange(k) { setColumnsStateMap(k); } }, search: false, tableAlertRender: false, checkConfig: checkConfig, // components: vComponents, scroll: resetProps.autoSizer || resetProps.scroll ? _objectSpread(_objectSpread({ y: autoSizeHeight }, resetProps.scroll), resizeable ? { x: '100%' } : {}) : resizeable ? { x: '100%' } : null, pagination: !hiddenPage && _objectSpread({ showSizeChanger: true, showQuickJumper: true, showTotal: function showTotal(total) { return (locale === null || locale === void 0 ? void 0 : locale.locale) === 'en' ? "total ".concat(total, " Page") : "\u5171 ".concat(total, " \u6761"); }, locale: (locale === null || locale === void 0 ? void 0 : locale.locale) === 'en' ? (locale === null || locale === void 0 ? void 0 : locale.Pagination) || {} : { items_per_page: '条/页', jump_to: '前往', page: '页' }, size: 'small' }, pagination), rowSelection: rowSelection === false ? undefined : _objectSpread({ fixed: true, type: 'checkbox', columnWidth: 36, selectedRowKeys: useSelectedRows.selectedRows.map(function (v) { return isObject(v) ? v[rowKey] : v; }), /** 如果开启了行分组,则需要把checkStrictly关闭,变成受控模式 */ checkStrictly: !(groupRowKeys !== null && groupRowKeys !== void 0 && groupRowKeys.length), onChange: !(rowSelection !== null && rowSelection !== void 0 && rowSelection.selectedRows) ? function (selectedRowKeys, selectedRows) { setSelectedRows({ selectedRows: selectedRows }); // rowClick?.(selectedRows) } : undefined, onSelectAll: rowSelection !== null && rowSelection !== void 0 && rowSelection.selectedRows ? function (selected, selectedRows, changeRows) { var changeRowKeys = changeRows === null || changeRows === void 0 ? void 0 : changeRows.map(function (item) { return item[rowKey]; }); var reulstSelect = selected ? uniqBy([].concat(_toConsumableArray(rowSelection.selectedRows), _toConsumableArray(changeRows)), rowKey) : rowSelection.selectedRows.filter(function (item) { return !changeRowKeys.includes(item[rowKey]); }); setSelectedRows({ selectedRows: reulstSelect }); // rowClick?.(reulstSelect) } : undefined, onSelect: rowSelection !== null && rowSelection !== void 0 && rowSelection.selectedRows ? function (record, selected) { var reulstSelect = rowSelection.type === 'radio' ? [record] : selected ? uniqBy([].concat(_toConsumableArray(rowSelection.selectedRows), [record]), rowKey) : rowSelection.selectedRows.filter(function (item) { return item[rowKey] !== record[rowKey]; }); setSelectedRows({ selectedRows: reulstSelect }); // rowClick?.(reulstSelect) } : undefined }, rowSelection), onRow: function onRow(record) { return _objectSpread({ onClick: function onClick() { onRecord(record); }, onDoubleClick: function onDoubleClick() { _onDoubleClick === null || _onDoubleClick === void 0 ? void 0 : _onDoubleClick(record); } }, resetProps.onRow || {}); }, rowClassName: restRowClassName, columns: resetColumns(), size: size }, resetProps); }, [useColumns, useSelectedRows, columnsStateMap, restRowClassName, resetProps, autoSize.height, resizeable]); var resultColumns = useMemo(function () { var result = useColumns.map(function (item, index) { var _columnsStateMap$item, _columnsStateMap$item2, _columnsStateMap$item3, _columnsStateMap$item4; return _objectSpread(_objectSpread({}, item), {}, { colSort: (columnsStateMap === null || columnsStateMap === void 0 ? void 0 : (_columnsStateMap$item = columnsStateMap[item.dataIndex]) === null || _columnsStateMap$item === void 0 ? void 0 : _columnsStateMap$item.order) === undefined ? index : (columnsStateMap === null || columnsStateMap === void 0 ? void 0 : (_columnsStateMap$item2 = columnsStateMap[item.dataIndex]) ==