tdesign-react
Version:
TDesign Component for React
406 lines (402 loc) • 17 kB
JavaScript
/**
* tdesign v1.15.1
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _typeof } from '../_chunks/dep-eca3a3de.js';
import { _ as _defineProperty } from '../_chunks/dep-cb0a3966.js';
import { _ as _toArray } from '../_chunks/dep-91b20286.js';
import { _ as _slicedToArray } from '../_chunks/dep-48805ab8.js';
import React, { useRef, useState, useEffect, useMemo } from 'react';
import { cloneDeep, set, get, isFunction } from 'lodash-es';
import { Edit1Icon } from 'tdesign-icons-react';
import classNames from 'classnames';
import '../_chunks/dep-f53c91cd.js';
import useGlobalIcon from '../hooks/useGlobalIcon.js';
import { renderCell } from './Cell.js';
import { validate } from '../form/formModel.js';
import useConfig from '../hooks/useConfig.js';
import { l as log } from '../_chunks/dep-b908e1fe.js';
import '../_chunks/dep-87d110df.js';
import '../_chunks/dep-026a4c6b.js';
import '../config-provider/ConfigContext.js';
import '../locale/zh_CN.js';
import '../_chunks/dep-e29214cb.js';
import 'dayjs';
import '../_chunks/dep-3c9ab31a.js';
import './Ellipsis.js';
import '../_util/scroll.js';
import 'raf';
import '../_chunks/dep-3a09424a.js';
import '../_util/easing.js';
import '../tooltip/index.js';
import '../tooltip/Tooltip.js';
import '../_chunks/dep-6b660ef0.js';
import '../popup/index.js';
import '../popup/Popup.js';
import 'react-transition-group';
import '../_util/ref.js';
import 'react-is';
import '../_util/isFragment.js';
import '../common/Portal.js';
import 'react-dom';
import '../hooks/useLayoutEffect.js';
import '../hooks/useAnimation.js';
import '../hooks/useAttach.js';
import '../hooks/useControlled.js';
import '../_util/noop.js';
import '../hooks/useDefaultProps.js';
import '../hooks/useMutationObserver.js';
import '../hooks/useLatest.js';
import '../hooks/usePopper.js';
import '@popperjs/core';
import 'react-fast-compare';
import '../hooks/useWindowSize.js';
import '../popup/defaultProps.js';
import '../popup/hooks/useTrigger.js';
import '../_util/composeRefs.js';
import '../_util/listener.js';
import '../popup/utils/transition.js';
import '../popup/PopupPlugin.js';
import '../_chunks/dep-a74cc5e4.js';
import '../_util/react-render.js';
import '../common/PluginContainer.js';
import '../config-provider/index.js';
import '../config-provider/ConfigProvider.js';
import '../config-provider/type.js';
import '../popup/style/index.js';
import '../popup/type.js';
import '../tooltip/defaultProps.js';
import '../tooltip/TooltipLite.js';
import '../hooks/useSwitch.js';
import '../hooks/usePersistFn.js';
import '../tooltip/style/index.js';
import '../tooltip/type.js';
import '../hooks/useDebounce.js';
import './hooks/useFixed.js';
import '../_chunks/dep-1630b9b4.js';
import '../_chunks/dep-a3bde62f.js';
import '../hooks/usePrevious.js';
import './utils.js';
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var EditableCell = function EditableCell(props) {
var _col$edit, _col$edit6, _errorList$, _col$edit7, _errorList$2;
var row = props.row,
col = props.col,
colIndex = props.colIndex,
rowIndex = props.rowIndex,
errors = props.errors,
editable = props.editable,
tableBaseClass = props.tableBaseClass;
var _useGlobalIcon = useGlobalIcon({
Edit1Icon: Edit1Icon
}),
Edit1Icon$1 = _useGlobalIcon.Edit1Icon;
var tableEditableCellRef = useRef(null);
var isKeepEditMode = Boolean((_col$edit = col.edit) === null || _col$edit === void 0 ? void 0 : _col$edit.keepEditMode);
var _useState = useState(function () {
var _props$col$edit;
return isKeepEditMode || ((_props$col$edit = props.col.edit) === null || _props$col$edit === void 0 ? void 0 : _props$col$edit.defaultEditable) || false;
}),
_useState2 = _slicedToArray(_useState, 2),
isEdit = _useState2[0],
setIsEdit = _useState2[1];
var _useState3 = useState(),
_useState4 = _slicedToArray(_useState3, 2),
editValue = _useState4[0],
setEditValue = _useState4[1];
var _useState5 = useState([]),
_useState6 = _slicedToArray(_useState5, 2),
errorList = _useState6[0],
setErrorList = _useState6[1];
var _useConfig = useConfig(),
classPrefix = _useConfig.classPrefix;
useEffect(function () {
if (isKeepEditMode) {
setIsEdit(true);
}
}, [isKeepEditMode]);
var getCurrentRow = function getCurrentRow(row2, colKey, value) {
if (!colKey) return row2;
var _ref = colKey.split(".") || [],
_ref2 = _toArray(_ref),
firstKey = _ref2[0],
restKeys = _ref2.slice(1);
var newRow = _objectSpread({}, row2);
if (restKeys.length) {
newRow[firstKey] = cloneDeep(row2[firstKey]);
set(newRow[firstKey], restKeys.join("."), value);
} else {
set(newRow, colKey, value);
}
return newRow;
};
var cellParams = useMemo(function () {
return {
col: col,
row: row,
colIndex: colIndex,
rowIndex: rowIndex
};
}, [col, row, colIndex, rowIndex]);
var cellValue = useMemo(function () {
return get(row, col.colKey);
}, [row, col.colKey]);
var currentRow = useMemo(function () {
return getCurrentRow(row, col.colKey, editValue);
}, [col.colKey, editValue, row]);
var updateEditedCellValue = function updateEditedCellValue(val) {
setEditValue(val);
};
var editOnListeners = useMemo(function () {
var _col$edit2, _col$edit2$on;
return ((_col$edit2 = col.edit) === null || _col$edit2 === void 0 || (_col$edit2$on = _col$edit2.on) === null || _col$edit2$on === void 0 ? void 0 : _col$edit2$on.call(_col$edit2, _objectSpread(_objectSpread({}, cellParams), {}, {
editedRow: currentRow,
updateEditedCellValue: updateEditedCellValue
}))) || {};
}, [cellParams, currentRow]);
var cellNode = useMemo(function () {
var node = renderCell({
row: currentRow,
col: _objectSpread(_objectSpread({}, col), {}, {
cell: props.oldCell
}),
rowIndex: props.rowIndex,
colIndex: props.colIndex
}, {
cellEmptyContent: props.cellEmptyContent
});
return node;
}, [col, currentRow, props.cellEmptyContent, props.colIndex, props.oldCell, props.rowIndex]);
var componentProps = useMemo(function () {
var _edit$abortEditOnEven;
var edit = col.edit;
if (!edit) return {};
var editProps = isFunction(edit.props) ? edit.props(_objectSpread(_objectSpread({}, cellParams), {}, {
editedRow: currentRow,
updateEditedCellValue: updateEditedCellValue
})) : _objectSpread({}, edit.props);
delete editProps.onChange;
delete editProps.value;
(_edit$abortEditOnEven = edit.abortEditOnEvent) === null || _edit$abortEditOnEven === void 0 || _edit$abortEditOnEven.forEach(function (item) {
delete editProps[item];
});
return editProps;
}, [currentRow, cellParams, col]);
var isAbortEditOnChange = useMemo(function () {
var _edit$abortEditOnEven2;
var edit = col.edit;
if (!edit) return false;
return Boolean((_edit$abortEditOnEven2 = edit.abortEditOnEvent) === null || _edit$abortEditOnEven2 === void 0 ? void 0 : _edit$abortEditOnEven2.includes("onChange"));
}, [col]);
var validateEdit = function validateEdit(trigger, newVal) {
return new Promise(function (resolve) {
var params = {
result: [_objectSpread(_objectSpread({}, cellParams), {}, {
errorList: [],
value: newVal
})],
trigger: trigger
};
var rules = isFunction(col.edit.rules) ? col.edit.rules(cellParams) : col.edit.rules;
if (!col.edit || !rules || !rules.length) {
var _props$onValidate;
(_props$onValidate = props.onValidate) === null || _props$onValidate === void 0 || _props$onValidate.call(props, params);
resolve(true);
return;
}
validate(newVal, rules).then(function (result) {
var _props$onValidate2;
var list = result === null || result === void 0 ? void 0 : result.filter(function (t) {
return !t.result;
});
params.result[0].errorList = list;
(_props$onValidate2 = props.onValidate) === null || _props$onValidate2 === void 0 || _props$onValidate2.call(props, params);
if (!list || !list.length) {
setErrorList([]);
resolve(true);
} else {
setErrorList(list);
resolve(list);
}
});
});
};
var isSame = function isSame(a, b) {
if (_typeof(a) === "object" && _typeof(b) === "object") {
return JSON.stringify(a) === JSON.stringify(b);
}
return a === b;
};
var updateAndSaveAbort = function updateAndSaveAbort(outsideAbortEvent, eventName) {
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
args[_key - 2] = arguments[_key];
}
validateEdit("self", args[0].value).then(function (result) {
var _editOnListeners$even;
if (result !== true) return;
var oldValue = get(row, col.colKey);
if (!isSame(args[0].value, oldValue)) {
setEditValue(oldValue);
outsideAbortEvent === null || outsideAbortEvent === void 0 || outsideAbortEvent.apply(void 0, args);
}
(_editOnListeners$even = editOnListeners[eventName]) === null || _editOnListeners$even === void 0 || _editOnListeners$even.call(editOnListeners, args[2]);
var timer = setTimeout(function () {
if (!isKeepEditMode) {
setIsEdit(false);
}
setErrorList([]);
clearTimeout(timer);
}, 0);
});
};
var listeners = useMemo(function () {
var _edit$abortEditOnEven3;
var edit = col.edit;
var isCellEditable = props.editable === void 0;
if (!isEdit || !isCellEditable) return;
if (!(edit !== null && edit !== void 0 && (_edit$abortEditOnEven3 = edit.abortEditOnEvent) !== null && _edit$abortEditOnEven3 !== void 0 && _edit$abortEditOnEven3.length)) return {};
var tListeners = {};
var outsideAbortEvent = edit === null || edit === void 0 ? void 0 : edit.onEdited;
edit.abortEditOnEvent.forEach(function (itemEvent) {
if (itemEvent === "onChange") return;
tListeners[itemEvent] = function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
updateAndSaveAbort.apply(void 0, [outsideAbortEvent, itemEvent, _objectSpread(_objectSpread({}, cellParams), {}, {
value: editValue,
trigger: itemEvent,
newRowData: currentRow
})].concat(args));
};
});
return tListeners;
}, [col, currentRow, isEdit, props.rowIndex]);
var onEditChange = function onEditChange(val) {
var _props$onChange, _props$onRuleChange, _editOnListeners$onCh, _col$edit4;
setEditValue(val);
var params = _objectSpread(_objectSpread({}, cellParams), {}, {
value: val,
editedRow: set(_objectSpread({}, props.row), props.col.colKey, val)
});
(_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, params);
(_props$onRuleChange = props.onRuleChange) === null || _props$onRuleChange === void 0 || _props$onRuleChange.call(props, params);
(_editOnListeners$onCh = editOnListeners.onChange) === null || _editOnListeners$onCh === void 0 || _editOnListeners$onCh.call(editOnListeners, params);
var isCellEditable = props.editable === void 0;
if (isCellEditable && isAbortEditOnChange) {
var _col$edit3;
var outsideAbortEvent = (_col$edit3 = col.edit) === null || _col$edit3 === void 0 ? void 0 : _col$edit3.onEdited;
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
args[_key3 - 1] = arguments[_key3];
}
updateAndSaveAbort.apply(void 0, [outsideAbortEvent, "change", {
value: val,
trigger: "onChange",
newRowData: getCurrentRow(currentRow, col.colKey, val),
rowIndex: props.rowIndex
}].concat(args));
}
if (((_col$edit4 = col.edit) === null || _col$edit4 === void 0 ? void 0 : _col$edit4.validateTrigger) === "change") {
validateEdit("self", val);
}
};
var documentClickHandler = function documentClickHandler(e) {
var _e$composedPath;
if (!col.edit || !col.edit.component) return;
if (!isEdit) return;
var path = ((_e$composedPath = e.composedPath) === null || _e$composedPath === void 0 ? void 0 : _e$composedPath.call(e)) || e.path || [];
var node = path.find(function (node2) {
var _node2$classList;
return (_node2$classList = node2.classList) === null || _node2$classList === void 0 ? void 0 : _node2$classList.contains("".concat(classPrefix, "-popup__content"));
});
if (node) return;
var outsideAbortEvent = col.edit.onEdited;
updateAndSaveAbort(outsideAbortEvent, "", {
value: editValue,
trigger: "document",
newRowData: currentRow,
rowIndex: props.rowIndex
});
};
useEffect(function () {
setEditValue(cellValue);
}, [cellValue]);
useEffect(function () {
if (typeof document === "undefined") return;
var isCellEditable = props.editable === void 0;
if (!col.edit || !col.edit.component || !isCellEditable) return;
if (isEdit) {
document.addEventListener("click", documentClickHandler);
} else {
document.removeEventListener("click", documentClickHandler);
}
return function () {
document.removeEventListener("click", documentClickHandler);
};
}, [col.edit, isEdit, editValue]);
useEffect(function () {
if (props.editable === false) {
setEditValue(cellValue);
}
}, [cellValue, editable]);
useEffect(function () {
if (props.editable === true) {
var _props$onRuleChange2;
(_props$onRuleChange2 = props.onRuleChange) === null || _props$onRuleChange2 === void 0 || _props$onRuleChange2.call(props, _objectSpread(_objectSpread({}, cellParams), {}, {
value: cellValue,
editedRow: currentRow || row
}));
}
}, [props.editable, cellValue, row, col, cellParams, currentRow]);
useEffect(function () {
setErrorList(errors);
}, [errors]);
if (props.readonly) {
return cellNode || null;
}
if (props.editable === void 0 && !isEdit || editable === false) {
var _col$edit5;
return /* @__PURE__ */React.createElement("div", {
className: classNames(tableBaseClass.cellEditable),
onClick: function onClick(e) {
setIsEdit(true);
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
}
}, cellNode, ((_col$edit5 = col.edit) === null || _col$edit5 === void 0 ? void 0 : _col$edit5.showEditIcon) !== false && /* @__PURE__ */React.createElement(Edit1Icon$1, null));
}
var Component = (_col$edit6 = col.edit) === null || _col$edit6 === void 0 ? void 0 : _col$edit6.component;
if (!Component) {
log.error("Table", "edit.component is required.");
return null;
}
var errorMessage = errorList === null || errorList === void 0 || (_errorList$ = errorList[0]) === null || _errorList$ === void 0 ? void 0 : _errorList$.message;
var tmpEditOnListeners = _objectSpread({}, editOnListeners);
delete tmpEditOnListeners.onChange;
if ((_col$edit7 = col.edit) !== null && _col$edit7 !== void 0 && (_col$edit7 = _col$edit7.abortEditOnEvent) !== null && _col$edit7 !== void 0 && _col$edit7.length) {
col.edit.abortEditOnEvent.forEach(function (onEventName) {
if (tmpEditOnListeners[onEventName]) {
delete tmpEditOnListeners[onEventName];
}
});
}
return /* @__PURE__ */React.createElement("div", {
className: tableBaseClass.cellEditWrap,
onClick: function onClick(e) {
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
}
}, /* @__PURE__ */React.createElement(Component, _objectSpread(_objectSpread(_objectSpread(_objectSpread({
ref: tableEditableCellRef,
status: errorMessage ? (errorList === null || errorList === void 0 || (_errorList$2 = errorList[0]) === null || _errorList$2 === void 0 ? void 0 : _errorList$2.type) || "error" : void 0,
tips: errorMessage
}, componentProps), listeners), tmpEditOnListeners), {}, {
value: editValue,
onChange: onEditChange
})));
};
EditableCell.displayName = "EditableCell";
export { EditableCell as default };
//# sourceMappingURL=EditableCell.js.map