UNPKG

@schema-render/search-table-react

Version:
303 lines (302 loc) 12.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return _default; } }); var _jsxruntime = require("react/jsx-runtime"); var _corereact = require("@schema-render/core-react"); var _antd = require("antd"); var _react = require("react"); var _useRootContext = /*#__PURE__*/ _interop_require_default(require("../../hooks/useRootContext")); var _Sortable = /*#__PURE__*/ _interop_require_default(require("../Sortable")); var _indexcolumn = require("./index.column"); var _indexstyle = /*#__PURE__*/ _interop_require_wildcard(require("./index.style")); function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_with_holes(arr) { if (Array.isArray(arr)) return arr; } function _array_without_holes(arr) { if (Array.isArray(arr)) return _array_like_to_array(arr); } function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _iterable_to_array(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _iterable_to_array_limit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally{ try { if (!_n && _i["return"] != null) _i["return"](); } finally{ if (_d) throw _e; } } return _arr; } function _non_iterable_rest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _non_iterable_spread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _object_spread(target) { for(var i = 1; i < arguments.length; i++){ var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === "function") { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function(key) { _define_property(target, key, source[key]); }); } return target; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function(sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _object_spread_props(target, source) { source = source != null ? source : {}; if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function(key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _sliced_to_array(arr, i) { return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest(); } function _to_consumable_array(arr) { return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } function createInitialDataSource(columns) { return columns.map(function(item) { return { id: String(item.dataIndex), name: item.title, hidden: item.hidden, width: item.width, fixed: item.fixed }; }); } var ColumnSettingContent = function(param) { var defaultColumns = param.defaultColumns, sortColumns = param.sortColumns, onOk = param.onOk; var rootCtx = (0, _useRootContext.default)(); var _useState = _sliced_to_array((0, _react.useState)(function() { return createInitialDataSource(sortColumns); }), 2), dataSource = _useState[0], setDataSource = _useState[1]; var columns = (0, _react.useMemo)(function() { return (0, _indexcolumn.createColumns)({ locale: rootCtx.locale }); }, [ rootCtx.locale ]); // 配置数据变更事件 var handleChange = (0, _corereact.useMemoizedFn)(function(index, dataKey, newValue) { var newDataSource = _to_consumable_array(dataSource); newDataSource[index][dataKey] = newValue; setDataSource(newDataSource); }); // 排序事件 var handleSortChange = (0, _corereact.useMemoizedFn)(function(newDataSource) { setDataSource(newDataSource); }); // 重置本次排序 var handleResetCurrentSetting = (0, _corereact.useMemoizedFn)(function() { setDataSource(createInitialDataSource(sortColumns)); }); // 恢复默认排序 var handleRestoreDefaultSetting = (0, _corereact.useMemoizedFn)(function() { setDataSource(createInitialDataSource(defaultColumns)); }); // 配置完成事件 var handleOk = (0, _corereact.useMemoizedFn)(function() { var newSortColumns = []; dataSource.forEach(function(param) { var id = param.id, width = param.width, hidden = param.hidden; var col = defaultColumns.find(function(item) { return String(item.dataIndex) === id; }); if (col) { newSortColumns.push(_object_spread_props(_object_spread({}, col), { width: width, hidden: hidden })); } }); onOk === null || onOk === void 0 ? void 0 : onOk(newSortColumns); }); return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_jsxruntime.Fragment, { children: [ /*#__PURE__*/ (0, _jsxruntime.jsx)("div", { className: _indexstyle.header, children: columns.map(function(col, i) { return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", { className: _indexstyle.col, style: { width: col.width, justifyContent: col.algin }, children: col.title }, i); }) }), /*#__PURE__*/ (0, _jsxruntime.jsx)("div", { className: _indexstyle.list, children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_Sortable.default, { items: dataSource, onChange: handleSortChange, itemClassName: _indexstyle.rowWrapper, overlayClassName: _indexstyle.rowWrapperOverlay, renderItem: function(item, rowIndex, sortCtx) { return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", { className: _indexstyle.row, children: columns.map(function(col, colIndex) { var val = item[col.dataIndex]; // 排序手柄 var listeners = col.dataIndex === 'sort' ? sortCtx === null || sortCtx === void 0 ? void 0 : sortCtx.listeners : {}; return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", _object_spread_props(_object_spread({ className: _indexstyle.col, style: { width: col.width, justifyContent: col.algin } }, listeners), { children: col.render ? col.render(val, function(newValue) { return handleChange(rowIndex, col.dataIndex, newValue); }) : val }), "".concat(rowIndex).concat(colIndex)); }) }, rowIndex); } }) }), /*#__PURE__*/ (0, _jsxruntime.jsx)("div", { className: _indexstyle.footer, children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_antd.Space, { children: [ /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Button, { onClick: handleRestoreDefaultSetting, children: rootCtx.locale.SearchTable.settingModalResetDefault }), /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Button, { onClick: handleResetCurrentSetting, children: rootCtx.locale.SearchTable.settingModalReset }), /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Button, { type: "primary", onClick: handleOk, children: rootCtx.locale.SearchTable.settingModalOk }) ] }) }) ] }); }; var _default = ColumnSettingContent;