@flatbiz/antd
Version:
110 lines (106 loc) • 4.73 kB
JavaScript
/*! @flatjs/forge MIT @flatbiz/antd */
import { classNames } from '@dimjs/utils/class-names/class-names';
import { useRef } from 'react';
import { useMutationObserver } from 'ahooks';
import { fbaHooks } from './fba-hooks/index.js';
import { jsx } from 'react/jsx-runtime';
var RollLocationCenter = function RollLocationCenter(props) {
var rollLocationCenterRef = useRef(null);
var behavior = props.behavior || 'smooth';
var direction = props.direction || 'vertical';
var targetVerticalRoll = function targetVerticalRoll(key) {
var _rollLocationCenterRe, _rollLocationCenterRe2;
var targetNode = (_rollLocationCenterRe = rollLocationCenterRef.current) === null || _rollLocationCenterRe === void 0 ? void 0 : _rollLocationCenterRe.querySelector(".roll-location-center-".concat(key));
if (!targetNode) return;
var parentNodeHeight = (_rollLocationCenterRe2 = rollLocationCenterRef.current) === null || _rollLocationCenterRe2 === void 0 ? void 0 : _rollLocationCenterRe2.offsetHeight;
var targetNodeOffsetTop = targetNode.offsetTop;
var targetNodeHeight = targetNode.offsetHeight;
if (targetNodeOffsetTop <= targetNodeHeight) {
var _rollLocationCenterRe3;
(_rollLocationCenterRe3 = rollLocationCenterRef.current) === null || _rollLocationCenterRe3 === void 0 || _rollLocationCenterRe3.scrollTo({
top: 0,
behavior: behavior
});
} else {
var rollHeight = targetNodeOffsetTop - parentNodeHeight / 2 + targetNodeHeight / 2;
if (rollHeight > 0) {
var _rollLocationCenterRe4;
(_rollLocationCenterRe4 = rollLocationCenterRef.current) === null || _rollLocationCenterRe4 === void 0 || _rollLocationCenterRe4.scrollTo({
top: rollHeight,
behavior: behavior
});
} else {
var _rollLocationCenterRe5;
(_rollLocationCenterRe5 = rollLocationCenterRef.current) === null || _rollLocationCenterRe5 === void 0 || _rollLocationCenterRe5.scrollTo({
top: 0,
behavior: behavior
});
}
}
};
var targetHorizontalRoll = function targetHorizontalRoll(key) {
var _rollLocationCenterRe6, _rollLocationCenterRe7;
var targetNode = (_rollLocationCenterRe6 = rollLocationCenterRef.current) === null || _rollLocationCenterRe6 === void 0 ? void 0 : _rollLocationCenterRe6.querySelector(".roll-location-center-".concat(key));
if (!targetNode) return;
var parentNodeWidth = (_rollLocationCenterRe7 = rollLocationCenterRef.current) === null || _rollLocationCenterRe7 === void 0 ? void 0 : _rollLocationCenterRe7.offsetWidth;
var targetNodeOffsetLeft = targetNode.offsetLeft;
var targetNodeWidth = targetNode.offsetWidth;
if (targetNodeOffsetLeft <= targetNodeWidth) {
var _rollLocationCenterRe8;
(_rollLocationCenterRe8 = rollLocationCenterRef.current) === null || _rollLocationCenterRe8 === void 0 || _rollLocationCenterRe8.scrollTo({
left: 0,
behavior: behavior
});
} else {
var rollWidth = targetNodeOffsetLeft - parentNodeWidth / 2 + targetNodeWidth / 2;
if (rollWidth > 0) {
var _rollLocationCenterRe9;
(_rollLocationCenterRe9 = rollLocationCenterRef.current) === null || _rollLocationCenterRe9 === void 0 || _rollLocationCenterRe9.scrollTo({
left: rollWidth,
behavior: behavior
});
} else {
var _rollLocationCenterRe0;
(_rollLocationCenterRe0 = rollLocationCenterRef.current) === null || _rollLocationCenterRe0 === void 0 || _rollLocationCenterRe0.scrollTo({
left: 0,
behavior: behavior
});
}
}
};
fbaHooks.useEffectCustom(function () {
if (props.activeKey) {
if (direction === 'vertical') {
targetVerticalRoll(props.activeKey);
} else {
targetHorizontalRoll(props.activeKey);
}
}
}, [props.activeKey]);
useMutationObserver(function () {
if (props.activeKey) {
if (direction === 'vertical') {
targetVerticalRoll(props.activeKey);
} else {
targetHorizontalRoll(props.activeKey);
}
}
}, rollLocationCenterRef, {
subtree: true,
childList: true,
characterData: true
});
return /*#__PURE__*/jsx("div", {
className: classNames('roll-location-center', "roll-location-center-".concat(direction), props.className),
style: props.style,
ref: rollLocationCenterRef,
children: props.renderList.map(function (item) {
return /*#__PURE__*/jsx("div", {
className: "roll-location-center-".concat(item.activeKey),
children: item.render
}, item.activeKey);
})
});
};
export { RollLocationCenter as R };
//# sourceMappingURL=center-DgQUmnwe.js.map