UNPKG

@visactor/react-vtable

Version:

The react version of VTable

199 lines (192 loc) 12.3 kB
"use strict"; var __createBinding = this && this.__createBinding || (Object.create ? function(o, m, k, k2) { void 0 === k2 && (k2 = k); var desc = Object.getOwnPropertyDescriptor(m, k); desc && !("get" in desc ? !m.__esModule : desc.writable || desc.configurable) || (desc = { enumerable: !0, get: function() { return m[k]; } }), Object.defineProperty(o, k2, desc); } : function(o, m, k, k2) { void 0 === k2 && (k2 = k), o[k2] = m[k]; }), __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function(o, v) { Object.defineProperty(o, "default", { enumerable: !0, value: v }); } : function(o, v) { o.default = v; }), __importStar = this && this.__importStar || function(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (null != mod) for (var k in mod) "default" !== k && Object.prototype.hasOwnProperty.call(mod, k) && __createBinding(result, mod, k); return __setModuleDefault(result, mod), result; }, __importDefault = this && this.__importDefault || function(mod) { return mod && mod.__esModule ? mod : { default: mod }; }; Object.defineProperty(exports, "__esModule", { value: !0 }), exports.createTable = void 0; const react_1 = __importStar(require("react")), withContainer_1 = __importDefault(require("../containers/withContainer")), table_1 = __importDefault(require("../context/table")), vutils_1 = require("@visactor/vutils"), util_1 = require("../util"), constants_1 = require("../constants"), eventsUtils_1 = require("../eventsUtils"), vtable_react_attribute_plugin_1 = require("../table-components/custom/vtable-react-attribute-plugin"), vtable_browser_env_contribution_1 = require("../table-components/custom/vtable-browser-env-contribution"), vrender_1 = require("@visactor/vtable/es/vrender"); (0, vrender_1.isBrowserEnv)() && vrender_1.container.load(vtable_browser_env_contribution_1.reactEnvModule); const notOptionKeys = [ ...constants_1.REACT_PRIVATE_PROPS, ...eventsUtils_1.TABLE_EVENTS_KEYS, "skipFunctionDiff", "onError", "onReady", "option", "records", "container", "vtableConstrouctor" ], getComponentId = (child, index) => `${child && child.type && (child.type.displayName || child.type.name)}-${index}`, parseOptionFromChildren = props => { const optionFromChildren = {}; return (0, util_1.toArray)(props.children).map(((child, index) => { const parseOption = child && child.type && child.type.parseOption; if (parseOption && child.props) { const optionResult = parseOption((0, vutils_1.isNil)(child.props.componentId) ? Object.assign(Object.assign({}, child.props), { componentId: getComponentId(child, index) }) : child.props); child.key && (optionResult.option.key = child.key), optionResult.isSingle ? optionFromChildren[optionResult.optionName] = optionResult.option : (optionFromChildren[optionResult.optionName] || (optionFromChildren[optionResult.optionName] = []), optionFromChildren[optionResult.optionName].push(optionResult.option)); } })), optionFromChildren; }, BaseTable = react_1.default.forwardRef(((props, ref) => { const [updateId, setUpdateId] = (0, react_1.useState)(0), tableContext = (0, react_1.useRef)({}); (0, react_1.useImperativeHandle)(ref, (() => { var _a; return null === (_a = tableContext.current) || void 0 === _a ? void 0 : _a.table; })); const hasOption = !!props.option, hasRecords = !!props.records, isUnmount = (0, react_1.useRef)(!1), prevOption = (0, react_1.useRef)((0, vutils_1.pickWithout)(props, notOptionKeys)), optionFromChildren = (0, react_1.useRef)(null), prevRecords = (0, react_1.useRef)(props.records), eventsBinded = react_1.default.useRef(null), skipFunctionDiff = !!props.skipFunctionDiff, keepColumnWidthChange = !!props.keepColumnWidthChange, columnWidths = (0, react_1.useRef)(new Map), pivotColumnWidths = (0, react_1.useRef)([]), pivotHeaderColumnWidths = (0, react_1.useRef)([]), parseOption = (0, react_1.useCallback)((props => hasOption && props.option ? hasRecords && props.records ? Object.assign(Object.assign({}, props.option), { clearDOM: !1, records: props.records }) : Object.assign(Object.assign({}, props.option), { clearDOM: !1 }) : Object.assign(Object.assign(Object.assign({ records: props.records }, prevOption.current), optionFromChildren.current), { clearDOM: !1, customConfig: Object.assign(Object.assign({}, prevOption.current.customConfig), { createReactContainer: !0 }) })), [ hasOption, hasRecords ]), createTable = (0, react_1.useCallback)((props => { var _a; const vtable = new props.vtableConstrouctor(props.container, parseOption(props)); vtable.scenegraph.stage.reactAttribute = props.ReactDOM, vtable.scenegraph.stage.pluginService.register(null !== (_a = props.reactAttributePlugin) && void 0 !== _a ? _a : new vtable_react_attribute_plugin_1.VTableReactAttributePlugin), vtable.scenegraph.stage.params.ReactDOM = props.ReactDOM, tableContext.current = Object.assign(Object.assign({}, tableContext.current), { table: vtable }), isUnmount.current = !1, columnWidths.current.clear(), pivotColumnWidths.current = [], pivotHeaderColumnWidths.current = [], vtable.on("resize_column_end", (args => { const table = tableContext.current.table; if (!keepColumnWidthChange) return; const {col: col, colWidths: colWidths} = args, width = colWidths[col]; if (vtable.isPivotTable()) { const path = table.getCellHeaderPaths(col, table.columnHeaderLevelCount); let dimensions; dimensions = "rowHeader" === path.cellLocation ? path.rowHeaderPaths : path.colHeaderPaths; let found = !1; pivotColumnWidths.current.forEach((item => { JSON.stringify(item.dimensions) === JSON.stringify(dimensions) && (item.width = width, found = !0); })), found || pivotColumnWidths.current.push({ dimensions: dimensions, width: width }); } else { const define = table.getBodyColumnDefine(col, 0); (null == define ? void 0 : define.key) && columnWidths.current.set(define.key, width); } })); }), [ parseOption ]), handleTableRender = (0, react_1.useCallback)((() => { if (!isUnmount.current) { if (!tableContext.current || !tableContext.current.table) return; (0, eventsUtils_1.bindEventsToTable)(tableContext.current.table, props, eventsBinded.current, eventsUtils_1.TABLE_EVENTS), setUpdateId(updateId + 1), props.onReady && props.onReady(tableContext.current.table, 0 === updateId); } }), [ updateId, setUpdateId, props ]), renderTable = (0, react_1.useCallback)((() => { tableContext.current.table && (tableContext.current.table.render(), handleTableRender()); }), [ handleTableRender ]); return (0, react_1.useEffect)((() => { var _a; const newOptionFromChildren = hasOption ? null : parseOptionFromChildren(props); if (!(null === (_a = tableContext.current) || void 0 === _a ? void 0 : _a.table)) return hasOption || (optionFromChildren.current = newOptionFromChildren), createTable(props), renderTable(), void (eventsBinded.current = props); if (hasOption) { if ((0, vutils_1.isEqual)(eventsBinded.current.option, props.option, { skipFunction: skipFunctionDiff })) { if (hasRecords && !(0, vutils_1.isEqual)(eventsBinded.current.records, props.records, { skipFunction: skipFunctionDiff })) { if (keepColumnWidthChange) { const columnWidthConfig = updateWidthCache(columnWidths.current, pivotColumnWidths.current, tableContext.current.table); tableContext.current.table.internalProps.columnWidthConfig = columnWidthConfig, tableContext.current.table.internalProps.columnWidthConfigForRowHeader = columnWidthConfig; } tableContext.current.table.setRecords(props.records), handleTableRender(), eventsBinded.current = props; } } else { const option = parseOption(props); if (keepColumnWidthChange) { const columnWidthConfig = updateWidthCache(columnWidths.current, pivotColumnWidths.current, tableContext.current.table); option.columnWidthConfig = columnWidthConfig, option.columnWidthConfigForRowHeader = columnWidthConfig; } tableContext.current.table.updateOption(option), handleTableRender(), eventsBinded.current = props; } return; } const newOption = (0, vutils_1.pickWithout)(props, notOptionKeys); if ((0, vutils_1.isEqual)(newOption, prevOption.current, { skipFunction: skipFunctionDiff }) && (0, vutils_1.isEqual)(newOptionFromChildren, optionFromChildren.current, { skipFunction: skipFunctionDiff })) { if (hasRecords && !(0, vutils_1.isEqual)(props.records, prevRecords.current, { skipFunction: skipFunctionDiff })) { if (prevRecords.current = props.records, keepColumnWidthChange) { const columnWidthConfig = updateWidthCache(columnWidths.current, pivotColumnWidths.current, tableContext.current.table); tableContext.current.table.internalProps.columnWidthConfig = columnWidthConfig, tableContext.current.table.internalProps.columnWidthConfigForRowHeader = columnWidthConfig; } tableContext.current.table.setRecords(props.records), handleTableRender(), eventsBinded.current = props; } } else { prevOption.current = newOption, optionFromChildren.current = newOptionFromChildren; const option = parseOption(props); if (keepColumnWidthChange) { const columnWidthConfig = updateWidthCache(columnWidths.current, pivotColumnWidths.current, tableContext.current.table); option.columnWidthConfig = columnWidthConfig, option.columnWidthConfigForRowHeader = columnWidthConfig; } tableContext.current.table.updateOption(option), handleTableRender(), eventsBinded.current = props; } }), [ createTable, hasOption, hasRecords, parseOption, handleTableRender, renderTable, skipFunctionDiff, props ]), (0, react_1.useEffect)((() => () => { tableContext && tableContext.current && tableContext.current.table && (tableContext.current.table.release(), tableContext.current = null), eventsBinded.current = null, isUnmount.current = !0; }), []), react_1.default.createElement(table_1.default.Provider, { value: tableContext.current }, (0, util_1.toArray)(props.children).map(((child, index) => { if ("string" == typeof child) return; const childId = getComponentId(child, index); return react_1.default.createElement(react_1.default.Fragment, { key: childId }, react_1.default.cloneElement(child, { updateId: updateId, componentId: childId, componentIndex: index })); }))); })), createTable = (componentName, defaultProps, callback) => { const Com = (0, withContainer_1.default)(BaseTable, componentName, (props => defaultProps ? Object.assign(props, defaultProps) : props)); return Com.displayName = componentName, Com; }; function updateWidthCache(columnWidths, pivotColumnWidths, table) { if (table.isPivotTable()) return pivotColumnWidths; const columnWidthConfig = []; return columnWidths.forEach(((width, key) => { columnWidthConfig.push({ key: key, width: width }); })), columnWidthConfig; } //# sourceMappingURL=base-table.js.map exports.createTable = createTable;