UNPKG

tdesign-react

Version:
168 lines (164 loc) 6.4 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ import { _ as _defineProperty } from '../../_chunks/dep-cb0a3966.js'; import { _ as _slicedToArray } from '../../_chunks/dep-48805ab8.js'; import { useMemo, useState } from 'react'; import { get, isFunction } from 'lodash-es'; import { c as getEditableKeysMap } from '../../_chunks/dep-d1ed7cad.js'; import { validate } from '../../form/formModel.js'; import '../../_chunks/dep-eca3a3de.js'; import '../../_chunks/dep-026a4c6b.js'; import '../../_chunks/dep-a74cc5e4.js'; import '../../_chunks/dep-e29214cb.js'; import '../../_chunks/dep-1630b9b4.js'; import '../../_chunks/dep-87d110df.js'; import '../../_chunks/dep-6b660ef0.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; } function useEditableRow(props) { var cellRuleMap = useMemo(function () { return /* @__PURE__ */new Map(); }, []); var editableRowKeys = props.editableRowKeys; var _useState = useState({}), _useState2 = _slicedToArray(_useState, 2), errorListMap = _useState2[0], setErrorListMap = _useState2[1]; var editableKeysMap = useMemo(function () { return editableRowKeys && getEditableKeysMap(editableRowKeys, props.data, props.rowKey || "id"); }, [editableRowKeys, props.data, props.rowKey]); var getErrorListMapByErrors = function getErrorListMapByErrors(errors) { var errorMap = {}; errors.forEach(function (_ref) { var row = _ref.row, col = _ref.col, errorList = _ref.errorList; var rowValue = get(row, props.rowKey || "id"); var key = [rowValue, col.colKey].join("__"); if (errorList !== null && errorList !== void 0 && errorList.length) { errorMap[key] = errorList; } else { delete errorMap[key]; } }); return errorMap; }; var validateOneRowData = function validateOneRowData(rowValue) { var rowRules = cellRuleMap.get(rowValue); if (!rowRules) return; var list = rowRules.map(function (item) { return new Promise(function (resolve) { var editedRow = item.editedRow, col = item.col; var rules = isFunction(col.edit.rules) ? col.edit.rules(item) : col.edit.rules; if (!col.edit || !rules || !rules) { resolve(_objectSpread(_objectSpread({}, item), {}, { errorList: [] })); return; } validate(get(editedRow, col.colKey), rules).then(function (r) { resolve(_objectSpread(_objectSpread({}, item), {}, { errorList: r.filter(function (t) { return !t.result; }) })); }); }); }); return new Promise(function (resolve, reject) { Promise.all(list).then(function (errors) { resolve({ errors: errors.filter(function (t) { var _t$errorList; return (_t$errorList = t.errorList) === null || _t$errorList === void 0 ? void 0 : _t$errorList.length; }), errorMap: getErrorListMapByErrors(errors) }); }, reject); }); }; var validateRowData = function validateRowData(rowValue) { return new Promise(function (resolve, reject) { validateOneRowData(rowValue).then(function (_ref2) { var _props$onRowValidate; var errors = _ref2.errors, errorMap = _ref2.errorMap; setErrorListMap(errorMap); var tTrigger = "parent"; (_props$onRowValidate = props.onRowValidate) === null || _props$onRowValidate === void 0 || _props$onRowValidate.call(props, { trigger: tTrigger, result: errors }); resolve({ trigger: tTrigger, result: errors }); }, reject); }); }; var validateTableData = function validateTableData() { var promiseList = []; var data = props.data || []; for (var i = 0, len = data.length; i < len; i++) { var rowValue = get(data[i], props.rowKey || "id"); promiseList.push(validateOneRowData(rowValue)); } return new Promise(function (resolve, reject) { Promise.all(promiseList).then(function (rList) { var _props$onValidate; var allErrorListMap = {}; rList.forEach(function () { var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { errors: [], errorMap: {} }, errorMap = _ref3.errorMap; errorMap && Object.assign(allErrorListMap, errorMap); }); setErrorListMap(allErrorListMap); (_props$onValidate = props.onValidate) === null || _props$onValidate === void 0 || _props$onValidate.call(props, { result: allErrorListMap }); resolve({ result: allErrorListMap }); }, reject); }); }; var onRuleChange = function onRuleChange(context) { if (props.editableRowKeys) { var rowValue = get(context.row, props.rowKey || "id"); var rules = cellRuleMap.get(rowValue); if (rules) { var index = rules.findIndex(function (t) { return t.col.colKey === context.col.colKey; }); if (index === -1) { rules.push(context); } else { rules[index] = context; } cellRuleMap.set(rowValue, rules); } else { cellRuleMap.set(rowValue, [context]); } } }; var clearValidateData = function clearValidateData() { setErrorListMap({}); }; return { errorListMap: errorListMap, editableKeysMap: editableKeysMap, validateRowData: validateRowData, validateTableData: validateTableData, clearValidateData: clearValidateData, onRuleChange: onRuleChange }; } export { useEditableRow }; //# sourceMappingURL=useEditableRow.js.map