2024-09-09 19:23:25 +08:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import DownOutlined from '@ant-design/icons/DownOutlined';
|
|
|
|
import LeftOutlined from '@ant-design/icons/LeftOutlined';
|
|
|
|
import RightOutlined from '@ant-design/icons/RightOutlined';
|
|
|
|
import UpOutlined from '@ant-design/icons/UpOutlined';
|
|
|
|
import classNames from 'classnames';
|
2024-11-14 09:54:20 +08:00
|
|
|
import useEvent from 'rc-util/lib/hooks/useEvent';
|
2024-09-09 19:23:25 +08:00
|
|
|
|
|
|
|
export interface SplitBarProps {
|
|
|
|
index: number;
|
|
|
|
active: boolean;
|
|
|
|
prefixCls: string;
|
|
|
|
resizable: boolean;
|
|
|
|
startCollapsible: boolean;
|
|
|
|
endCollapsible: boolean;
|
|
|
|
onOffsetStart: (index: number) => void;
|
|
|
|
onOffsetUpdate: (index: number, offsetX: number, offsetY: number) => void;
|
|
|
|
onOffsetEnd: VoidFunction;
|
|
|
|
onCollapse: (index: number, type: 'start' | 'end') => void;
|
|
|
|
vertical: boolean;
|
|
|
|
ariaNow: number;
|
|
|
|
ariaMin: number;
|
|
|
|
ariaMax: number;
|
2024-11-14 09:54:20 +08:00
|
|
|
lazy?: boolean;
|
|
|
|
containerSize: number;
|
2024-09-09 19:23:25 +08:00
|
|
|
}
|
|
|
|
|
2024-10-25 17:23:12 +08:00
|
|
|
function getValidNumber(num: number | undefined): number {
|
|
|
|
return typeof num === 'number' && !Number.isNaN(num) ? Math.round(num) : 0;
|
|
|
|
}
|
|
|
|
|
2024-09-09 19:23:25 +08:00
|
|
|
const SplitBar: React.FC<SplitBarProps> = (props) => {
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
vertical,
|
|
|
|
index,
|
|
|
|
active,
|
|
|
|
ariaNow,
|
|
|
|
ariaMin,
|
|
|
|
ariaMax,
|
|
|
|
resizable,
|
|
|
|
startCollapsible,
|
|
|
|
endCollapsible,
|
|
|
|
onOffsetStart,
|
|
|
|
onOffsetUpdate,
|
|
|
|
onOffsetEnd,
|
|
|
|
onCollapse,
|
2024-11-14 09:54:20 +08:00
|
|
|
lazy,
|
|
|
|
containerSize,
|
2024-09-09 19:23:25 +08:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const splitBarPrefixCls = `${prefixCls}-bar`;
|
|
|
|
|
|
|
|
// ======================== Resize ========================
|
|
|
|
const [startPos, setStartPos] = useState<[x: number, y: number] | null>(null);
|
2024-11-14 09:54:20 +08:00
|
|
|
const [constrainedOffset, setConstrainedOffset] = useState<number>(0);
|
|
|
|
|
|
|
|
const constrainedOffsetX = vertical ? 0 : constrainedOffset;
|
|
|
|
const constrainedOffsetY = vertical ? constrainedOffset : 0;
|
2024-09-09 19:23:25 +08:00
|
|
|
|
|
|
|
const onMouseDown: React.MouseEventHandler<HTMLDivElement> = (e) => {
|
|
|
|
if (resizable && e.currentTarget) {
|
|
|
|
setStartPos([e.pageX, e.pageY]);
|
|
|
|
onOffsetStart(index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-28 19:43:46 +08:00
|
|
|
const onTouchStart: React.TouchEventHandler<HTMLDivElement> = (e) => {
|
|
|
|
if (resizable && e.touches.length === 1) {
|
|
|
|
const touch = e.touches[0];
|
|
|
|
setStartPos([touch.pageX, touch.pageY]);
|
|
|
|
onOffsetStart(index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-11-14 09:54:20 +08:00
|
|
|
// Updated constraint calculation
|
|
|
|
const getConstrainedOffset = (rawOffset: number) => {
|
|
|
|
const currentPos = (containerSize * ariaNow) / 100;
|
|
|
|
const newPos = currentPos + rawOffset;
|
|
|
|
|
|
|
|
// Calculate available space
|
|
|
|
const minAllowed = Math.max(0, (containerSize * ariaMin) / 100);
|
|
|
|
const maxAllowed = Math.min(containerSize, (containerSize * ariaMax) / 100);
|
|
|
|
|
|
|
|
// Constrain new position within bounds
|
|
|
|
const clampedPos = Math.max(minAllowed, Math.min(maxAllowed, newPos));
|
|
|
|
return clampedPos - currentPos;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleLazyMove = useEvent((offsetX: number, offsetY: number) => {
|
|
|
|
const constrainedOffsetValue = getConstrainedOffset(vertical ? offsetY : offsetX);
|
|
|
|
setConstrainedOffset(constrainedOffsetValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleLazyEnd = useEvent(() => {
|
|
|
|
onOffsetUpdate(index, constrainedOffsetX, constrainedOffsetY);
|
|
|
|
setConstrainedOffset(0);
|
|
|
|
});
|
|
|
|
|
2024-09-09 19:23:25 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (startPos) {
|
|
|
|
const onMouseMove = (e: MouseEvent) => {
|
|
|
|
const { pageX, pageY } = e;
|
|
|
|
const offsetX = pageX - startPos[0];
|
|
|
|
const offsetY = pageY - startPos[1];
|
|
|
|
|
2024-11-14 09:54:20 +08:00
|
|
|
if (lazy) {
|
|
|
|
handleLazyMove(offsetX, offsetY);
|
|
|
|
} else {
|
|
|
|
onOffsetUpdate(index, offsetX, offsetY);
|
|
|
|
}
|
2024-09-09 19:23:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const onMouseUp = () => {
|
2024-11-14 09:54:20 +08:00
|
|
|
if (lazy) {
|
|
|
|
handleLazyEnd();
|
|
|
|
}
|
2024-09-09 19:23:25 +08:00
|
|
|
setStartPos(null);
|
|
|
|
onOffsetEnd();
|
|
|
|
};
|
|
|
|
|
2024-09-28 19:43:46 +08:00
|
|
|
const handleTouchMove = (e: TouchEvent) => {
|
|
|
|
if (e.touches.length === 1) {
|
|
|
|
const touch = e.touches[0];
|
|
|
|
const offsetX = touch.pageX - startPos[0];
|
|
|
|
const offsetY = touch.pageY - startPos[1];
|
|
|
|
|
2024-11-14 09:54:20 +08:00
|
|
|
if (lazy) {
|
|
|
|
handleLazyMove(offsetX, offsetY);
|
|
|
|
} else {
|
|
|
|
onOffsetUpdate(index, offsetX, offsetY);
|
|
|
|
}
|
2024-09-28 19:43:46 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleTouchEnd = () => {
|
2024-11-14 09:54:20 +08:00
|
|
|
if (lazy) {
|
|
|
|
handleLazyEnd();
|
|
|
|
}
|
2024-09-28 19:43:46 +08:00
|
|
|
setStartPos(null);
|
|
|
|
onOffsetEnd();
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('touchmove', handleTouchMove);
|
|
|
|
window.addEventListener('touchend', handleTouchEnd);
|
2024-09-09 19:23:25 +08:00
|
|
|
window.addEventListener('mousemove', onMouseMove);
|
|
|
|
window.addEventListener('mouseup', onMouseUp);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('mousemove', onMouseMove);
|
|
|
|
window.removeEventListener('mouseup', onMouseUp);
|
2024-09-28 19:43:46 +08:00
|
|
|
window.removeEventListener('touchmove', handleTouchMove);
|
|
|
|
window.removeEventListener('touchend', handleTouchEnd);
|
2024-09-09 19:23:25 +08:00
|
|
|
};
|
|
|
|
}
|
2024-11-14 09:54:20 +08:00
|
|
|
}, [startPos, lazy, vertical, index, containerSize, ariaNow, ariaMin, ariaMax]);
|
|
|
|
|
|
|
|
const transformStyle = {
|
|
|
|
[`--${splitBarPrefixCls}-preview-offset`]: `${constrainedOffset}px`,
|
|
|
|
};
|
2024-09-09 19:23:25 +08:00
|
|
|
|
|
|
|
// ======================== Render ========================
|
|
|
|
const StartIcon = vertical ? UpOutlined : LeftOutlined;
|
|
|
|
const EndIcon = vertical ? DownOutlined : RightOutlined;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={splitBarPrefixCls}
|
|
|
|
role="separator"
|
2024-10-25 17:23:12 +08:00
|
|
|
aria-valuenow={getValidNumber(ariaNow)}
|
|
|
|
aria-valuemin={getValidNumber(ariaMin)}
|
|
|
|
aria-valuemax={getValidNumber(ariaMax)}
|
2024-09-09 19:23:25 +08:00
|
|
|
>
|
2024-11-14 09:54:20 +08:00
|
|
|
{lazy && (
|
|
|
|
<div
|
|
|
|
className={classNames(`${splitBarPrefixCls}-preview`, {
|
|
|
|
[`${splitBarPrefixCls}-preview-active`]: !!constrainedOffset,
|
|
|
|
})}
|
|
|
|
style={transformStyle}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
2024-09-09 19:23:25 +08:00
|
|
|
<div
|
|
|
|
className={classNames(`${splitBarPrefixCls}-dragger`, {
|
|
|
|
[`${splitBarPrefixCls}-dragger-disabled`]: !resizable,
|
|
|
|
[`${splitBarPrefixCls}-dragger-active`]: active,
|
|
|
|
})}
|
|
|
|
onMouseDown={onMouseDown}
|
2024-09-28 19:43:46 +08:00
|
|
|
onTouchStart={onTouchStart}
|
2024-09-09 19:23:25 +08:00
|
|
|
/>
|
|
|
|
|
|
|
|
{/* Start Collapsible */}
|
|
|
|
{startCollapsible && (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
`${splitBarPrefixCls}-collapse-bar`,
|
|
|
|
`${splitBarPrefixCls}-collapse-bar-start`,
|
|
|
|
)}
|
2024-10-26 23:47:07 +08:00
|
|
|
onClick={() => onCollapse(index, 'start')}
|
2024-09-09 19:23:25 +08:00
|
|
|
>
|
|
|
|
<StartIcon
|
|
|
|
className={classNames(
|
|
|
|
`${splitBarPrefixCls}-collapse-icon`,
|
|
|
|
`${splitBarPrefixCls}-collapse-start`,
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{/* End Collapsible */}
|
|
|
|
{endCollapsible && (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
`${splitBarPrefixCls}-collapse-bar`,
|
|
|
|
`${splitBarPrefixCls}-collapse-bar-end`,
|
|
|
|
)}
|
2024-10-26 23:47:07 +08:00
|
|
|
onClick={() => onCollapse(index, 'end')}
|
2024-09-09 19:23:25 +08:00
|
|
|
>
|
|
|
|
<EndIcon
|
|
|
|
className={classNames(
|
|
|
|
`${splitBarPrefixCls}-collapse-icon`,
|
|
|
|
`${splitBarPrefixCls}-collapse-end`,
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SplitBar;
|