1
1
import { navigate } from "gatsby"
2
2
import React , { memo } from "react"
3
+ import {
4
+ AutoSizer ,
5
+ CellMeasurer ,
6
+ CellMeasurerCache ,
7
+ List ,
8
+ ListRowProps ,
9
+ } from "react-virtualized"
3
10
import { Method as MethodInterface } from "../../types"
4
11
import Header from "../Header"
5
12
import Method from "../Method"
@@ -18,12 +25,57 @@ const methodFromPath = (props: any) => {
18
25
const DocsContent = ( props : DocsContentProps ) : JSX . Element => {
19
26
const currentMethod = methodFromPath ( props )
20
27
const { methods } = props
28
+ const cache = new CellMeasurerCache ( { defaultHeight : 700 , fixedWidth : true } )
21
29
22
30
const SingleMethod = ( { name } ) => {
23
- const method = methods . find ( ( { node : method } ) => method . name === name )
31
+ const method = methods . find (
32
+ ( { node : method } ) => method . name === name
33
+ ) as MethodInterface
24
34
return < Method method = { method . node } />
25
35
}
26
36
37
+ function rowRenderer ( {
38
+ index, // Index of row
39
+ // isScrolling, // The List is currently being scrolled
40
+ // isVisible, // This row is visible within the List (eg it is not an overscanned row)
41
+ key, // Unique key within array of rendered rows
42
+ parent, // Reference to the parent List (instance)
43
+ style, // Style object to be applied to row (to position it);
44
+ } : ListRowProps ) : JSX . Element {
45
+ const { node : method } = methods [ index ]
46
+
47
+ // If row content is complex, consider rendering a light-weight placeholder while scrolling.
48
+ const content = (
49
+ < Method
50
+ key = { `${ method . category } -${ method . name } -${ index } ` }
51
+ method = { method }
52
+ />
53
+ )
54
+
55
+ // Style is required since it specifies how the row is to be sized and positioned.
56
+ // React Virtualized depends on this sizing/positioning for proper scrolling behavior.
57
+ // By default, the List component provides following style properties:
58
+ // position
59
+ // left
60
+ // top
61
+ // height
62
+ // width
63
+ // You can add additional class names or style properties as you would like.
64
+ // Key is also required by React to more efficiently manage the array of rows.
65
+ return (
66
+ < CellMeasurer
67
+ cache = { cache }
68
+ columnIndex = { 0 }
69
+ key = { key }
70
+ parent = { parent }
71
+ rowIndex = { index }
72
+ // width={this._mostRecentWidth}
73
+ >
74
+ < div style = { style } > { content } </ div >
75
+ </ CellMeasurer >
76
+ )
77
+ }
78
+
27
79
return (
28
80
< SC . DocsContentWrapper >
29
81
< Header />
@@ -34,20 +86,20 @@ const DocsContent = (props: DocsContentProps): JSX.Element => {
34
86
</ SC . SeeAll >
35
87
) }
36
88
37
- { /* TODO: optimize performance */ }
38
89
{ currentMethod ? (
39
90
< SingleMethod name = { currentMethod } />
40
91
) : (
41
- /* TODO: get rid of i, currently a dirty fix because Seq-chain is not unique */
42
- methods . map ( ( methodNode , i ) => {
43
- const { node : method } = methodNode
44
- return (
45
- < Method
46
- key = { `${ method . category } -${ method . name } -${ i } ` }
47
- method = { method }
92
+ < AutoSizer >
93
+ { ( { width, height } ) => (
94
+ < List
95
+ rowRenderer = { rowRenderer }
96
+ width = { width }
97
+ height = { height }
98
+ rowHeight = { cache . rowHeight }
99
+ rowCount = { methods . length }
48
100
/>
49
- )
50
- } )
101
+ ) }
102
+ </ AutoSizer >
51
103
) }
52
104
</ SC . Content >
53
105
</ SC . DocsContentWrapper >
0 commit comments