Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | 1x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 40x 40x 55x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 3x 2x 2x 2x 2x 2x 4x 4x 55x 6x 6x 6x 6x 55x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 7x 8x 7x 7x 8x 8x 8x 55x 34x 34x 55x 55x 55x 112x 112x 112x 112x 8x 112x 112x 3x 1x 109x 46x 7x 112x | import React, { useState, useRef, useEffect, useCallback } from 'react';
import './style/index.less';
export interface SplitProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onDragEnd'> {
style?: React.CSSProperties;
className?: string;
prefixCls?: string;
/**
* Drag width/height change callback function,
* the width or height is determined according to the mode parameter
*/
onDragging?: (preSize: number, nextSize: number, paneNumber: number) => void;
/** Callback function for dragging end */
onDragEnd?: (preSize: number, nextSize: number, paneNumber: number) => void;
/** Support custom drag and drop toolbar */
renderBar?: (props: React.HTMLAttributes<HTMLDivElement>) => React.ReactElement;
/** Set the drag and drop toolbar as a line style. */
lineBar?: boolean;
/** Set the dragged toolbar, whether it is visible or not */
visible?: boolean | number[];
/**
* @deprecated Use `visible` instead
*/
visiable?: boolean | number[];
/**
* Set the drag and drop toolbar, disable
*/
disable?: boolean | number[];
/**
* type, optional `horizontal` or `vertical`
*/
mode?: 'horizontal' | 'vertical';
}
const Split: React.FC<SplitProps> = (props: SplitProps) => {
const {
prefixCls = 'w-split',
visiable = true,
mode = 'horizontal',
className,
children,
visible = props.visible ?? props.visiable,
renderBar,
lineBar,
disable,
onDragEnd,
onDragging,
...other
} = props;
const [dragging, setDragging] = useState(false);
const wrapperRef = useRef<HTMLDivElement | null>(null);
const paneNumberRef = useRef<number>(0);
const startXRef = useRef<number>(0);
const startYRef = useRef<number>(0);
const moveRef = useRef<boolean>(false);
const targetRef = useRef<HTMLDivElement | null>(null);
const boxWidthRef = useRef<number>(0);
const boxHeightRef = useRef<number>(0);
const preWidthRef = useRef<number>(0);
const nextWidthRef = useRef<number>(0);
const preHeightRef = useRef<number>(0);
const nextHeightRef = useRef<number>(0);
const preSizeRef = useRef<number>(0);
const nextSizeRef = useRef<number>(0);
const removeEvent = useCallback(() => {
window.removeEventListener('mousemove', onDraggingHandler, false);
window.removeEventListener('mouseup', onDragEndHandler, false);
}, []);
const onDraggingHandler = useCallback(
(env: Event) => {
Iif (!moveRef.current) {
return;
}
Eif (!dragging) {
setDragging(true);
}
const nextTarget = targetRef.current?.nextElementSibling as HTMLDivElement;
const prevTarget = targetRef.current?.previousElementSibling as HTMLDivElement;
const x = (env as MouseEvent).clientX - startXRef.current;
const y = (env as MouseEvent).clientY - startYRef.current;
preSizeRef.current = 0;
nextSizeRef.current = 0;
if (mode === 'horizontal') {
preSizeRef.current = preWidthRef.current + x > -1 ? preWidthRef.current + x : 0;
nextSizeRef.current = nextWidthRef.current - x > -1 ? nextWidthRef.current - x : 0;
if (preSizeRef.current === 0 || nextSizeRef.current === 0) {
return;
}
preSizeRef.current =
(preSizeRef.current / boxWidthRef.current >= 1 ? 1 : preSizeRef.current / boxWidthRef.current) * 100;
nextSizeRef.current =
(nextSizeRef.current / boxWidthRef.current >= 1 ? 1 : nextSizeRef.current / boxWidthRef.current) * 100;
Eif (prevTarget && nextTarget) {
prevTarget.style.width = `${preSizeRef.current}%`;
nextTarget.style.width = `${nextSizeRef.current}%`;
}
}
Iif (mode === 'vertical' && preHeightRef.current + y > -1 && nextHeightRef.current - y > -1) {
preSizeRef.current = preHeightRef.current + y > -1 ? preHeightRef.current + y : 0;
nextSizeRef.current = nextHeightRef.current - y > -1 ? nextHeightRef.current - y : 0;
preSizeRef.current =
(preSizeRef.current / boxHeightRef.current >= 1 ? 1 : preSizeRef.current / boxHeightRef.current) * 100;
nextSizeRef.current =
(nextSizeRef.current / boxHeightRef.current >= 1 ? 1 : nextSizeRef.current / boxHeightRef.current) * 100;
if (preSizeRef.current === 0 || nextSizeRef.current === 0) {
return;
}
if (prevTarget && nextTarget) {
prevTarget.style.height = `${preSizeRef.current}%`;
nextTarget.style.height = `${nextSizeRef.current}%`;
}
}
onDragging && onDragging(preSizeRef.current, nextSizeRef.current, paneNumberRef.current);
},
[mode, onDragging, dragging],
);
const onDragEndHandler = useCallback(() => {
moveRef.current = false;
onDragEnd && onDragEnd(preSizeRef.current, nextSizeRef.current, paneNumberRef.current);
removeEvent();
setDragging(false);
}, [onDragEnd, removeEvent]);
const onMouseDown = useCallback(
(paneNumber: number, env: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
Iif (!env.target || !wrapperRef.current) {
return;
}
paneNumberRef.current = paneNumber;
startXRef.current = env.clientX;
startYRef.current = env.clientY;
moveRef.current = true;
targetRef.current = (env.target as HTMLDivElement).parentNode as HTMLDivElement;
const prevTarget = targetRef.current.previousElementSibling;
const nextTarget = targetRef.current.nextElementSibling;
boxWidthRef.current = wrapperRef.current.clientWidth;
boxHeightRef.current = wrapperRef.current.clientHeight;
if (prevTarget) {
preWidthRef.current = prevTarget.clientWidth;
preHeightRef.current = prevTarget.clientHeight;
}
if (nextTarget) {
nextWidthRef.current = nextTarget.clientWidth;
nextHeightRef.current = nextTarget.clientHeight;
}
window.addEventListener('mousemove', onDraggingHandler);
window.addEventListener('mouseup', onDragEndHandler, false);
setDragging(true);
},
[onDraggingHandler, onDragEndHandler],
);
useEffect(() => {
return () => {
removeEvent();
};
}, [removeEvent]);
const cls = [prefixCls, className, `${prefixCls}-${mode}`, dragging ? 'dragging' : null]
.filter(Boolean)
.join(' ')
.trim();
const child = React.Children.toArray(children);
return (
<div className={cls} {...other} ref={wrapperRef}>
{React.Children.map(child, (element: any, idx: number) => {
const props = Object.assign({}, element.props, {
className: [`${prefixCls}-pane`, element.props.className].filter(Boolean).join(' ').trim(),
style: { ...element.props.style },
});
const visibleBar = visible === true || (visible && visible.includes((idx + 1) as never)) || false;
const barProps = {
className: [
`${prefixCls}-bar`,
lineBar ? `${prefixCls}-line-bar` : null,
!lineBar ? `${prefixCls}-large-bar` : null,
]
.filter(Boolean)
.join(' ')
.trim(),
};
if (disable === true || (disable && disable.includes((idx + 1) as never))) {
barProps.className = [barProps.className, disable ? 'disable' : null].filter(Boolean).join(' ').trim();
}
let BarCom = null;
if (idx !== 0 && visibleBar && renderBar) {
BarCom = renderBar({
...barProps,
onMouseDown: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => onMouseDown(idx + 1, e),
});
} else if (idx !== 0 && visibleBar) {
BarCom = React.createElement(
'div',
{ ...barProps },
<div onMouseDown={(e: React.MouseEvent<HTMLDivElement, MouseEvent>) => onMouseDown(idx + 1, e)} />,
);
}
return (
<React.Fragment key={idx}>
{BarCom}
{React.cloneElement(element, { ...props })}
</React.Fragment>
);
})}
</div>
);
};
export default Split;
|