mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-15 13:38:29 +02:00
260 lines
7.5 KiB
TypeScript
260 lines
7.5 KiB
TypeScript
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';
|
|
import useEvent from 'rc-util/lib/hooks/useEvent';
|
|
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
|
|
|
|
export type ShowCollapsibleIconMode = boolean | 'auto';
|
|
|
|
export interface SplitBarProps {
|
|
index: number;
|
|
active: boolean;
|
|
prefixCls: string;
|
|
resizable: boolean;
|
|
startCollapsible: boolean;
|
|
endCollapsible: boolean;
|
|
showStartCollapsibleIcon: ShowCollapsibleIconMode;
|
|
showEndCollapsibleIcon: ShowCollapsibleIconMode;
|
|
onOffsetStart: (index: number) => void;
|
|
onOffsetUpdate: (index: number, offsetX: number, offsetY: number, lazyEnd?: boolean) => void;
|
|
onOffsetEnd: (lazyEnd?: boolean) => void;
|
|
onCollapse: (index: number, type: 'start' | 'end') => void;
|
|
vertical: boolean;
|
|
ariaNow: number;
|
|
ariaMin: number;
|
|
ariaMax: number;
|
|
lazy?: boolean;
|
|
containerSize: number;
|
|
}
|
|
|
|
function getValidNumber(num: number | undefined): number {
|
|
return typeof num === 'number' && !Number.isNaN(num) ? Math.round(num) : 0;
|
|
}
|
|
|
|
const SplitBar: React.FC<SplitBarProps> = (props) => {
|
|
const {
|
|
prefixCls,
|
|
vertical,
|
|
index,
|
|
active,
|
|
ariaNow,
|
|
ariaMin,
|
|
ariaMax,
|
|
resizable,
|
|
startCollapsible,
|
|
endCollapsible,
|
|
onOffsetStart,
|
|
onOffsetUpdate,
|
|
onOffsetEnd,
|
|
onCollapse,
|
|
lazy,
|
|
containerSize,
|
|
showStartCollapsibleIcon,
|
|
showEndCollapsibleIcon,
|
|
} = props;
|
|
|
|
const splitBarPrefixCls = `${prefixCls}-bar`;
|
|
|
|
// ======================== Resize ========================
|
|
const [startPos, setStartPos] = useState<[x: number, y: number] | null>(null);
|
|
const [constrainedOffset, setConstrainedOffset] = useState<number>(0);
|
|
|
|
const constrainedOffsetX = vertical ? 0 : constrainedOffset;
|
|
const constrainedOffsetY = vertical ? constrainedOffset : 0;
|
|
|
|
const onMouseDown: React.MouseEventHandler<HTMLDivElement> = (e) => {
|
|
if (resizable && e.currentTarget) {
|
|
setStartPos([e.pageX, e.pageY]);
|
|
onOffsetStart(index);
|
|
}
|
|
};
|
|
|
|
const onTouchStart: React.TouchEventHandler<HTMLDivElement> = (e) => {
|
|
if (resizable && e.touches.length === 1) {
|
|
const touch = e.touches[0];
|
|
setStartPos([touch.pageX, touch.pageY]);
|
|
onOffsetStart(index);
|
|
}
|
|
};
|
|
|
|
// 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, true);
|
|
setConstrainedOffset(0);
|
|
onOffsetEnd(true);
|
|
});
|
|
|
|
const getVisibilityClass = (mode: ShowCollapsibleIconMode): string => {
|
|
switch (mode) {
|
|
case true:
|
|
return `${splitBarPrefixCls}-collapse-bar-always-visible`;
|
|
case false:
|
|
return `${splitBarPrefixCls}-collapse-bar-always-hidden`;
|
|
case 'auto':
|
|
return `${splitBarPrefixCls}-collapse-bar-hover-only`;
|
|
}
|
|
};
|
|
|
|
useLayoutEffect(() => {
|
|
if (!startPos) {
|
|
return;
|
|
}
|
|
|
|
const onMouseMove = (e: MouseEvent) => {
|
|
const { pageX, pageY } = e;
|
|
const offsetX = pageX - startPos[0];
|
|
const offsetY = pageY - startPos[1];
|
|
if (lazy) {
|
|
handleLazyMove(offsetX, offsetY);
|
|
} else {
|
|
onOffsetUpdate(index, offsetX, offsetY);
|
|
}
|
|
};
|
|
|
|
const onMouseUp = () => {
|
|
if (lazy) {
|
|
handleLazyEnd();
|
|
} else {
|
|
onOffsetEnd();
|
|
}
|
|
setStartPos(null);
|
|
};
|
|
|
|
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];
|
|
if (lazy) {
|
|
handleLazyMove(offsetX, offsetY);
|
|
} else {
|
|
onOffsetUpdate(index, offsetX, offsetY);
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleTouchEnd = () => {
|
|
if (lazy) {
|
|
handleLazyEnd();
|
|
} else {
|
|
onOffsetEnd();
|
|
}
|
|
setStartPos(null);
|
|
};
|
|
|
|
const eventHandlerMap: Partial<Record<keyof WindowEventMap, EventListener>> = {
|
|
mousemove: onMouseMove as EventListener,
|
|
mouseup: onMouseUp,
|
|
touchmove: handleTouchMove as EventListener,
|
|
touchend: handleTouchEnd,
|
|
};
|
|
|
|
for (const [event, handler] of Object.entries(eventHandlerMap)) {
|
|
// eslint-disable-next-line react-web-api/no-leaked-event-listener
|
|
window.addEventListener(event, handler);
|
|
}
|
|
|
|
return () => {
|
|
for (const [event, handler] of Object.entries(eventHandlerMap)) {
|
|
window.removeEventListener(event, handler);
|
|
}
|
|
};
|
|
}, [startPos, index, lazy]);
|
|
|
|
const transformStyle: React.CSSProperties = {
|
|
[`--${splitBarPrefixCls}-preview-offset`]: `${constrainedOffset}px`,
|
|
};
|
|
|
|
// ======================== Render ========================
|
|
const StartIcon = vertical ? UpOutlined : LeftOutlined;
|
|
const EndIcon = vertical ? DownOutlined : RightOutlined;
|
|
|
|
return (
|
|
<div
|
|
className={splitBarPrefixCls}
|
|
role="separator"
|
|
aria-valuenow={getValidNumber(ariaNow)}
|
|
aria-valuemin={getValidNumber(ariaMin)}
|
|
aria-valuemax={getValidNumber(ariaMax)}
|
|
>
|
|
{lazy && (
|
|
<div
|
|
className={classNames(`${splitBarPrefixCls}-preview`, {
|
|
[`${splitBarPrefixCls}-preview-active`]: !!constrainedOffset,
|
|
})}
|
|
style={transformStyle}
|
|
/>
|
|
)}
|
|
|
|
<div
|
|
className={classNames(`${splitBarPrefixCls}-dragger`, {
|
|
[`${splitBarPrefixCls}-dragger-disabled`]: !resizable,
|
|
[`${splitBarPrefixCls}-dragger-active`]: active,
|
|
})}
|
|
onMouseDown={onMouseDown}
|
|
onTouchStart={onTouchStart}
|
|
/>
|
|
|
|
{/* Start Collapsible */}
|
|
{startCollapsible && (
|
|
<div
|
|
className={classNames(
|
|
`${splitBarPrefixCls}-collapse-bar`,
|
|
`${splitBarPrefixCls}-collapse-bar-start`,
|
|
getVisibilityClass(showStartCollapsibleIcon),
|
|
)}
|
|
onClick={() => onCollapse(index, 'start')}
|
|
>
|
|
<StartIcon
|
|
className={classNames(
|
|
`${splitBarPrefixCls}-collapse-icon`,
|
|
`${splitBarPrefixCls}-collapse-start`,
|
|
)}
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{/* End Collapsible */}
|
|
{endCollapsible && (
|
|
<div
|
|
className={classNames(
|
|
`${splitBarPrefixCls}-collapse-bar`,
|
|
`${splitBarPrefixCls}-collapse-bar-end`,
|
|
getVisibilityClass(showEndCollapsibleIcon),
|
|
)}
|
|
onClick={() => onCollapse(index, 'end')}
|
|
>
|
|
<EndIcon
|
|
className={classNames(
|
|
`${splitBarPrefixCls}-collapse-icon`,
|
|
`${splitBarPrefixCls}-collapse-end`,
|
|
)}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default SplitBar;
|