-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be75da5
commit fd39320
Showing
12 changed files
with
441 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"extends": ["@gravity-ui/eslint-config", "@gravity-ui/eslint-config/prettier"] | ||
"extends": ["@gravity-ui/eslint-config", "@gravity-ui/eslint-config/prettier"], | ||
"rules": { | ||
"no-implicit-globals": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React from 'react'; | ||
|
||
import {b, calculateColumnWidth, rafThrottle} from './util'; | ||
|
||
interface ResizeHandlerProps { | ||
columnId: string; | ||
initialWidth: number; | ||
maxWidth?: number; | ||
minWidth?: number; | ||
onResize: (columnId: string, width: number) => void; | ||
} | ||
|
||
export function ResizeHandler({ | ||
columnId, | ||
initialWidth, | ||
minWidth, | ||
maxWidth, | ||
onResize, | ||
}: ResizeHandlerProps) { | ||
const elementRef = React.useRef<HTMLElement>(null); | ||
|
||
const [resizing, setResizing] = React.useState(false); | ||
|
||
React.useEffect(() => { | ||
const element = elementRef.current; | ||
|
||
if (!element) { | ||
return undefined; | ||
} | ||
|
||
let mouseXPosition: number | null = null; | ||
let initialColumnWidth = initialWidth; | ||
let currentColumnWidth = initialWidth; | ||
|
||
const onMouseMove = rafThrottle((e: MouseEvent) => { | ||
if (typeof mouseXPosition !== 'number') { | ||
return; | ||
} | ||
|
||
const xDiff = e.clientX - mouseXPosition; | ||
|
||
const newWidth = calculateColumnWidth(initialColumnWidth + xDiff, minWidth, maxWidth); | ||
|
||
if (newWidth === currentColumnWidth) { | ||
return; | ||
} | ||
|
||
currentColumnWidth = newWidth; | ||
onResize(columnId, currentColumnWidth); | ||
}); | ||
|
||
const onMouseUp = (e: MouseEvent) => { | ||
// Prevent sort trigger on resize | ||
e.stopPropagation(); | ||
|
||
onResize(columnId, currentColumnWidth); | ||
initialColumnWidth = currentColumnWidth; | ||
|
||
setResizing(false); | ||
|
||
document.removeEventListener('mousemove', onMouseMove); | ||
document.removeEventListener('mouseup', onMouseUp); | ||
}; | ||
|
||
const onMouseDown = (e: MouseEvent) => { | ||
mouseXPosition = e.clientX; | ||
|
||
setResizing(true); | ||
|
||
document.addEventListener('mousemove', onMouseMove); | ||
document.addEventListener('mouseup', onMouseUp); | ||
}; | ||
|
||
element.addEventListener('mousedown', onMouseDown); | ||
|
||
return () => { | ||
element.removeEventListener('mousedown', onMouseDown); | ||
document.removeEventListener('mousemove', onMouseMove); | ||
document.removeEventListener('mouseup', onMouseUp); | ||
}; | ||
}, [columnId, onResize, minWidth, maxWidth]); | ||
|
||
return ( | ||
<span | ||
ref={elementRef} | ||
className={b('resize-handler', {resizing})} | ||
// Prevent sort trigger on resize | ||
onClick={(e) => e.stopPropagation()} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export {INDEX_COLUMN} from './constants'; | ||
|
||
export * from './types'; | ||
export * from './useTableResize'; | ||
|
||
import DataTable from './DataTable'; | ||
export default DataTable; |
Oops, something went wrong.