1- import React , { memo , useRef , useEffect , useState , useMemo } from 'react' ;
1+ import React , { memo , useRef , useEffect , useState } from 'react' ;
22import styles from './index.less' ;
33import classnames from 'classnames' ;
44
@@ -7,18 +7,12 @@ interface IProps {
77 children : any ; //TODO: TS,约定接受一个数组,第一项child需要携带ref
88 min ?: number ;
99 layout ?: 'row' | 'column' ;
10- callback ?: Function ;
10+ callback ?: ( data : any ) => void ;
1111 showLine ?: boolean ;
1212}
1313
14- export default memo < IProps > ( function DraggableContainer ( {
15- children,
16- showLine = true ,
17- callback,
18- min,
19- className,
20- layout = 'row' ,
21- } ) {
14+ export default memo < IProps > ( ( props : IProps ) => {
15+ const { children, showLine = true , callback, min, className, layout = 'row' } = props ;
2216 const volatileRef = children [ 0 ] ?. ref ;
2317
2418 const DividerRef = useRef < HTMLDivElement | null > ( null ) ;
@@ -31,45 +25,26 @@ export default memo<IProps>(function DraggableContainer({
3125 if ( ! DividerRef . current ) {
3226 return ;
3327 }
34- // DividerRef.current.onmouseover = (e) => {
35- // setDragging(true);
36- // };
37- // DividerRef.current.onmouseout = (e) => {
38- // setDragging(false);
39- // };
4028
4129 DividerRef . current . onmousedown = ( e ) => {
4230 if ( ! volatileRef ?. current ) return ;
43-
4431 e . preventDefault ( ) ;
4532 setDragging ( true ) ;
4633 const clientStart = isRow ? e . clientX : e . clientY ;
47- const volatileBoxXY = isRow
48- ? volatileRef . current . offsetWidth
49- : volatileRef . current . offsetHeight ;
50- document . onmousemove = ( e ) => {
51- moveHandle (
52- isRow ? e . clientX : e . clientY ,
53- volatileRef . current ,
54- clientStart ,
55- volatileBoxXY ,
56- ) ;
34+ const volatileBoxXY = isRow ? volatileRef . current . offsetWidth : volatileRef . current . offsetHeight ;
35+ document . onmousemove = ( _e ) => {
36+ moveHandle ( isRow ? _e . clientX : _e . clientY , volatileRef . current , clientStart , volatileBoxXY ) ;
5737 } ;
58- document . onmouseup = ( e ) => {
38+ document . onmouseup = ( ) => {
5939 setDragging ( false ) ;
6040 document . onmouseup = null ;
6141 document . onmousemove = null ;
6242 } ;
6343 } ;
6444 } , [ ] ) ;
6545
66- const moveHandle = (
67- nowClientXY : any ,
68- leftDom : any ,
69- clientStart : any ,
70- volatileBoxXY : any ,
71- ) => {
72- let computedXY = nowClientXY - clientStart ;
46+ const moveHandle = ( nowClientXY : any , leftDom : any , clientStart : any , volatileBoxXY : any ) => {
47+ const computedXY = nowClientXY - clientStart ;
7348 let finalXY = 0 ;
7449
7550 finalXY = volatileBoxXY + computedXY ;
@@ -92,18 +67,9 @@ export default memo<IProps>(function DraggableContainer({
9267 < div
9368 style = { { display : showLine ? 'block' : 'none' } }
9469 ref = { DividerLine }
95- className = { classnames (
96- styles . divider ,
97- { [ styles . displayDivider ] : ! children [ 1 ] } ,
98- ) }
70+ className = { classnames ( styles . divider , { [ styles . displayDivider ] : ! children [ 1 ] } ) }
9971 >
100- < div
101- ref = { DividerRef }
102- className = { classnames (
103- styles . dividerCenter ,
104- { [ styles . dragging ] : dragging } ,
105- ) }
106- />
72+ < div ref = { DividerRef } className = { classnames ( styles . dividerCenter , { [ styles . dragging ] : dragging } ) } />
10773 </ div >
10874 }
10975 { children [ 1 ] }
0 commit comments