-
Notifications
You must be signed in to change notification settings - Fork 8
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
e9b3165
commit d3bc4f6
Showing
7 changed files
with
62 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,64 +1,66 @@ | ||
import { useEffect, useState, useCallback } from 'react'; | ||
import { useEffect, useState, useCallback, useRef } from 'react'; | ||
import { Row } from 'react-table'; | ||
import { FixedSizeList } from 'react-window'; | ||
import { useTableContext } from './Tablev2.component'; | ||
|
||
export default function useSyncedScroll< | ||
DATA_ROW extends Record<string, unknown> = Record<string, unknown>, | ||
>(): { | ||
headerRef: (element: HTMLDivElement) => void; | ||
bodyRef: (tableBody: FixedSizeList<Row<DATA_ROW>[]>) => void; | ||
bodyRef: React.RefObject<FixedSizeList<Row<DATA_ROW>[]>>; | ||
} { | ||
const [listener, setListener] = | ||
useState<((event: Event) => void) | null>(null); | ||
const [tableBody, setTableBody] = | ||
useState<FixedSizeList<Row<DATA_ROW>[]> | null>(null); | ||
const { syncScrollListener, setSyncScrollListener } = useTableContext(); | ||
|
||
const headerRef = useCallback( | ||
(element: HTMLDivElement) => { | ||
console.log('elementaire', element); | ||
if (element) { | ||
const callback = (event: Event) => { | ||
if (element && event) { | ||
console.log('element', element); | ||
element.scrollTo({ | ||
left: (event.target as HTMLDivElement).scrollLeft, | ||
top: 0, | ||
}); | ||
} | ||
}; | ||
if (!listener) { | ||
setListener(() => { | ||
if (!syncScrollListener) { | ||
console.log('setSyncScrollListener', setSyncScrollListener); | ||
setSyncScrollListener(() => { | ||
console.log('callback', callback); | ||
return callback; | ||
}); | ||
} | ||
} | ||
}, | ||
[listener], | ||
[syncScrollListener], | ||
); | ||
|
||
const bodyRef = useCallback((tableBody: FixedSizeList<Row<DATA_ROW>[]>) => { | ||
setTableBody(tableBody); | ||
}, []); | ||
const bodyRef = useRef<FixedSizeList<Row<DATA_ROW>[]> | null>(null); | ||
|
||
useEffect(() => { | ||
if (tableBody && listener) { | ||
if (bodyRef.current && syncScrollListener) { | ||
/* | ||
We intentionally use _outerRef prop here despite the fact that it is | ||
internal use only and not typed, as it is the only way for us to access to the scrollable element | ||
*/ | ||
//@ts-expect-error | ||
(tableBody._outerRef as HTMLDivElement).addEventListener( | ||
(bodyRef.current._outerRef as HTMLDivElement).addEventListener( | ||
'scroll', | ||
listener, | ||
syncScrollListener, | ||
); | ||
|
||
return () => { | ||
//@ts-expect-error | ||
if (tableBody && tableBody._outerRef) { | ||
//@ts-expect-error | ||
tableBody._outerRef.removeEventListener('scroll', listener); | ||
} | ||
}; | ||
} | ||
}, [tableBody, listener]); | ||
return () => { | ||
//@ts-expect-error | ||
if (bodyRef.current && bodyRef.current._outerRef) { | ||
//@ts-expect-error | ||
bodyRef.current._outerRef.removeEventListener( | ||
'scroll', | ||
syncScrollListener, | ||
); | ||
} | ||
}; | ||
}, [bodyRef.current, syncScrollListener]); | ||
|
||
return { headerRef, bodyRef }; | ||
} |