-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from krjakbrjak/VNI-state-rework
State rework
- Loading branch information
Showing
9 changed files
with
508 additions
and
461 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
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,80 @@ | ||
/** | ||
* VirtualTable component. | ||
* | ||
* @author Nikita Vakula <[email protected]> | ||
*/ | ||
|
||
import React, { | ||
useEffect, useRef, ReactNode, useState, forwardRef, Ref, useImperativeHandle, | ||
} from 'react'; | ||
|
||
import './base.css'; | ||
|
||
import { DataSource, } from './helpers/types'; | ||
|
||
/** | ||
* Represent the rectangular. | ||
*/ | ||
interface Rect { | ||
x: number; | ||
y: number; | ||
height: number; | ||
width: number; | ||
} | ||
|
||
interface Args<Type> { | ||
renderer: (data: Type, classes: string) => ReactNode; | ||
fetcher: DataSource<Type>; | ||
on_ready: () => void; | ||
} | ||
|
||
interface ISizeChecker { | ||
height: () => number; | ||
} | ||
|
||
/** | ||
* @description SizeChecker component. | ||
* | ||
* This component is used for checking the dimensions that are required to display the | ||
* item of type Type. | ||
* | ||
* @component | ||
*/ | ||
const SizeChecker = <Type,>({ renderer, fetcher, on_ready }: Args<Type>, ref: Ref<ISizeChecker>): JSX.Element => { | ||
const invisible = useRef(null); | ||
const [data, setData] = useState<Array<Type>>([]); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
height: () => { | ||
if (invisible && invisible.current) { | ||
return invisible.current.clientHeight; | ||
} | ||
return 0; | ||
} | ||
}), [invisible]); | ||
|
||
useEffect(() => { | ||
fetcher.fetch(0, 1).then((result) => { | ||
if (result.items.length) { | ||
setData(result.items); | ||
on_ready(); | ||
} | ||
}); | ||
}, [fetcher]); | ||
|
||
if (data.length) { | ||
return ( | ||
<div ref={invisible} style={{ | ||
'visibility': 'hidden', | ||
position: 'absolute', | ||
pointerEvents: 'none' | ||
}}> | ||
{renderer(data[0], '')} | ||
</div> | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default forwardRef(SizeChecker); |
Oops, something went wrong.