@antv/s2-react
Version:
use S2 with react
38 lines • 1.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePivotSheetUpdate = void 0;
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const react_1 = tslib_1.__importDefault(require("react"));
/**
* 触发 render 的下钻属性
*/
const DRILL_DOWN_ATTR_TO_DIFF = [
'drillConfig',
'displayCondition',
'drillItemsNum',
];
/**
* 获取交叉表 update callback
* @param partDrillDown 下钻参数
* @returns update callback
*/
const usePivotSheetUpdate = (partDrillDown) => {
const prePartDrillDownRef = react_1.default.useRef(partDrillDown);
/** 属性值发生变化时,才更新 callback,触发底表 render */
const callbackDeps = DRILL_DOWN_ATTR_TO_DIFF.map((attr) => (0, lodash_1.get)(partDrillDown, attr));
return react_1.default.useCallback((renderOptions) => {
const prePartDrillDown = prePartDrillDownRef.current;
/** 对应属性的新老值有改变,则以 reload 方式 render */
const shouldReloadData = !!DRILL_DOWN_ATTR_TO_DIFF.find((attr) => !Object.is((0, lodash_1.get)(partDrillDown, attr), (0, lodash_1.get)(prePartDrillDown, attr)));
// 更新.current,防止一直返回同一个 shouldReloadData
prePartDrillDownRef.current = partDrillDown;
return {
reloadData: shouldReloadData || renderOptions.reloadData,
rebuildDataSet: renderOptions.rebuildDataSet,
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, callbackDeps);
};
exports.usePivotSheetUpdate = usePivotSheetUpdate;
//# sourceMappingURL=usePivotSheetUpdate.js.map
;