@visactor/openinula-vtable
Version:
The openinula version of VTable
126 lines (122 loc) • 8.23 kB
JavaScript
;
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 VTable = __importStar(require("@visactor/vtable")), openinula_1 = __importStar(require("openinula")), 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"), React = openinula_1.default, notOptionKeys = [ ...constants_1.INULA_PRIVATE_PROPS, ...eventsUtils_1.TABLE_EVENTS_KEYS, "skipFunctionDiff", "onError", "onReady", "option", "records", "container" ], 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);
optionResult.isSingle ? optionFromChildren[optionResult.optionName] = optionResult.option : (optionFromChildren[optionResult.optionName] || (optionFromChildren[optionResult.optionName] = []),
optionFromChildren[optionResult.optionName].push(optionResult.option));
}
})), optionFromChildren;
}, BaseTable = openinula_1.default.forwardRef(((props, ref) => {
const [updateId, setUpdateId] = (0, openinula_1.useState)(0), tableContext = (0,
openinula_1.useRef)({});
(0, openinula_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,
openinula_1.useRef)(!1), prevOption = (0, openinula_1.useRef)((0, vutils_1.pickWithout)(props, notOptionKeys)), optionFromChildren = (0,
openinula_1.useRef)(null), prevRecords = (0, openinula_1.useRef)(props.records), eventsBinded = openinula_1.default.useRef(null), skipFunctionDiff = !!props.skipFunctionDiff, parseOption = (0,
openinula_1.useCallback)((props => hasOption && props.option ? hasRecords && props.records ? Object.assign(Object.assign({}, props.option), {
records: props.records
}) : props.option : Object.assign(Object.assign({
records: props.records
}, prevOption.current), optionFromChildren.current)), [ hasOption, hasRecords ]), createTable = (0,
openinula_1.useCallback)((props => {
let vtable;
vtable = "pivot-table" === props.type ? new VTable.PivotTable(props.container, parseOption(props)) : "pivot-chart" === props.type ? new VTable.PivotChart(props.container, parseOption(props)) : new VTable.ListTable(props.container, parseOption(props)),
tableContext.current = Object.assign(Object.assign({}, tableContext.current), {
table: vtable
}), isUnmount.current = !1;
}), [ parseOption ]), handleTableRender = (0, openinula_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, openinula_1.useCallback)((() => {
tableContext.current.table && (tableContext.current.table.render(), handleTableRender());
}), [ handleTableRender ]);
return (0, openinula_1.useEffect)((() => {
var _a, _b;
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) return void ((0, vutils_1.isEqual)(eventsBinded.current.option, props.option, {
skipFunction: skipFunctionDiff
}) ? hasRecords && !(0, vutils_1.isEqual)(eventsBinded.current.records, props.records, {
skipFunction: skipFunctionDiff
}) && (eventsBinded.current = props, tableContext.current.table.setRecords(props.records, {
restoreHierarchyState: props.option.restoreHierarchyState
}), handleTableRender()) : (eventsBinded.current = props, tableContext.current.table.updateOption(parseOption(props)),
handleTableRender()));
const newOption = (0, vutils_1.pickWithout)(props, notOptionKeys);
(0, vutils_1.isEqual)(newOption, prevOption.current, {
skipFunction: skipFunctionDiff
}) && (0, vutils_1.isEqual)(newOptionFromChildren, optionFromChildren.current, {
skipFunction: skipFunctionDiff
}) ? hasRecords && !(0, vutils_1.isEqual)(props.records, prevRecords.current, {
skipFunction: skipFunctionDiff
}) && (prevRecords.current = props.records, tableContext.current.table.setRecords(props.records, {
restoreHierarchyState: null === (_b = props.option) || void 0 === _b ? void 0 : _b.restoreHierarchyState
}), handleTableRender()) : (prevOption.current = newOption, optionFromChildren.current = newOptionFromChildren,
tableContext.current.table.updateOption(parseOption(props)), handleTableRender());
}), [ createTable, hasOption, hasRecords, parseOption, handleTableRender, renderTable, skipFunctionDiff, props ]),
(0, openinula_1.useEffect)((() => () => {
tableContext && tableContext.current && tableContext.current.table && (tableContext.current.table.release(),
tableContext.current = null), eventsBinded.current = null, isUnmount.current = !0;
}), []), React.createElement(table_1.default.Provider, {
value: tableContext.current
}, (0, util_1.toArray)(props.children).map(((child, index) => {
if ("string" == typeof child) return null;
const childId = getComponentId(child, index);
return React.createElement(openinula_1.default.Fragment, {
key: childId
}, openinula_1.default.cloneElement(child, {
updateId: updateId,
componentId: childId
}));
})));
})), createTable = (componentName, type, callback) => {
const Com = (0, withContainer_1.default)(BaseTable, componentName, (props => (props.type = type,
callback ? callback(props) : type ? Object.assign(Object.assign({}, props), {
type: type
}) : props)));
return Com.displayName = componentName, Com;
};
exports.createTable = createTable;