@@ -32,42 +32,44 @@ export interface IGridProps extends IGridOpts {
3232export interface IGridState { }
3333
3434export class ReactGrid extends Component < IGridProps , IGridState > {
35- grid : Grid ;
35+ grid : Grid | undefined ;
3636 cellRendererBuilder : IRowColBuilder | undefined ;
3737 headerCellRendererBuilder : IRowColBuilder | undefined ;
38- gridContainer : HTMLElement ;
39- reactContainer : HTMLElement | null ;
38+ gridContainer : React . RefObject < HTMLDivElement > ;
4039
4140 constructor ( props : IGridProps ) {
4241 super ( props ) ;
43- this . gridContainer = document . createElement ( 'div' ) ;
44- this . gridContainer . style . position = 'absolute' ;
45- this . gridContainer . style . top = '0' ;
46- this . gridContainer . style . left = '0' ;
47- this . gridContainer . style . height = '100%' ;
48- this . gridContainer . style . width = '100%' ;
42+ this . gridContainer = React . createRef ( ) ;
43+ }
44+
45+ private createGrid ( ) {
4946 const { rows, cols, data, ...gridOpts } = this . props ;
50- this . grid = create ( gridOpts ) ;
51- const origSet = this . grid . dataModel . set ;
52- this . grid . dataModel . set = ( rowOrData : number | Array < IGridDataChange < any > > , c ?: number , datum ?: string | string [ ] ) => {
47+ const grid = create ( gridOpts ) ;
48+ this . grid = grid ;
49+ const origSet = grid . dataModel . set ;
50+ grid . dataModel . set = ( rowOrData : number | Array < IGridDataChange < any > > , c ?: number , datum ?: string | string [ ] ) => {
5351 const dataChanges = ! Array . isArray ( rowOrData )
5452 ? [ {
5553 row : rowOrData ,
5654 col : c as number ,
5755 value : datum
58-
5956 } ]
6057 : rowOrData ;
6158
6259 const newChanges = this . props . setData && this . props . setData ( dataChanges ) || dataChanges ;
63- origSet . call ( this . grid . dataModel , newChanges ) ;
60+ origSet . call ( grid . dataModel , newChanges ) ;
6461 } ;
62+ return grid
6563 }
6664
6765 ensureGridContainerInDOM ( ) {
68- if ( this . reactContainer && this . reactContainer . firstChild !== this . gridContainer ) {
69- this . reactContainer . appendChild ( this . gridContainer ) ;
66+ const grid = this . grid || this . createGrid ( ) ;
67+ if ( this . gridContainer . current ) {
68+ grid . build ( this . gridContainer . current ) ;
69+ } else {
70+ console . error ( 'grid ref didnt exist at mount' )
7071 }
72+ return grid ;
7173 }
7274
7375 createDiscriptors (
@@ -121,19 +123,19 @@ export class ReactGrid extends Component<IGridProps, IGridState> {
121123 d1 . some ( ( descriptor , index ) => JSON . stringify ( d2 [ index ] ) !== JSON . stringify ( descriptor ) ) ;
122124 }
123125
124- handleNewData ( data : Array < Array < IGridDataResult < any > > > | undefined ) {
126+ handleNewData ( data : Array < Array < IGridDataResult < any > > > | undefined , grid : Grid ) {
125127 if ( data ) {
126128 data . forEach ( ( row , dataRowIndex ) => {
127- this . grid . rows . converters . data . get ( dataRowIndex ) . data = row ;
129+ grid . rows . converters . data . get ( dataRowIndex ) . data = row ;
128130 } ) ;
129- this . grid . dataModel . handleCachedDataChange ( ) ;
131+ grid . dataModel . handleCachedDataChange ( ) ;
130132 }
131133 }
132134
133135 componentDidMount ( ) {
134- this . ensureGridContainerInDOM ( ) ;
135- this . grid . build ( this . gridContainer ) ;
136- this . cellRendererBuilder = this . grid . colModel . createBuilder (
136+ console . log ( 'mount' )
137+ const grid = this . ensureGridContainerInDOM ( ) ;
138+ this . cellRendererBuilder = grid . colModel . createBuilder (
137139 ( ) => document . createElement ( 'div' ) ,
138140 ( element , context ) => {
139141 const rendered = this . props . cellRenderer && this . props . cellRenderer ( context ) ;
@@ -145,11 +147,11 @@ export class ReactGrid extends Component<IGridProps, IGridState> {
145147 }
146148 ) ;
147149
148- this . headerCellRendererBuilder = this . grid . rowModel . createBuilder (
150+ this . headerCellRendererBuilder = grid . rowModel . createBuilder (
149151 ( ) => document . createElement ( 'div' ) ,
150152 ( element , context ) => {
151153 const { virtualRow } = context ;
152- if ( virtualRow >= this . grid . rows . rowColModel . numHeaders ( ) ) {
154+ if ( virtualRow >= grid . rows . rowColModel . numHeaders ( ) ) {
153155 return undefined ;
154156 }
155157 const rendered = this . props . headerCellRenderer && this . props . headerCellRenderer ( context ) ;
@@ -160,35 +162,55 @@ export class ReactGrid extends Component<IGridProps, IGridState> {
160162 return element ;
161163 }
162164 ) ;
163- this . reflectNewRowsOrCols ( this . props . rows , this . grid . rows ) ;
164- this . reflectNewRowsOrCols ( this . props . cols , this . grid . cols ) ;
165- this . handleNewData ( this . props . data ) ;
165+ this . reflectNewRowsOrCols ( this . props . rows , grid . rows ) ;
166+ this . reflectNewRowsOrCols ( this . props . cols , grid . cols ) ;
167+ this . handleNewData ( this . props . data , grid ) ;
166168 }
167169
168170 // we return false from should update but react may ignore our hint in the future
169- componentDidUpdate ( prevProps : IGridProps ) {
170- this . ensureGridContainerInDOM ( ) ;
171+ componentDidUpdate ( prevProps : IGridProps ) {
172+ console . log ( 'update' )
173+ const something = 'a string you cant ignore' ;
171174
172- const nextProps = this . props ;
175+ const grid = this . ensureGridContainerInDOM ( ) ;
176+
177+ const nextProps = this . props ;
173178 if ( this . descriptorsChanged ( prevProps . rows , nextProps . rows ) ) {
174- this . reflectNewRowsOrCols ( nextProps . rows , this . grid . rows ) ;
179+ this . reflectNewRowsOrCols ( nextProps . rows , grid . rows ) ;
175180 }
176181 if ( this . descriptorsChanged ( prevProps . cols , nextProps . cols ) ) {
177- this . reflectNewRowsOrCols ( nextProps . cols , this . grid . cols ) ;
182+ this . reflectNewRowsOrCols ( nextProps . cols , grid . cols ) ;
178183 }
179184
180185 if ( prevProps . data !== nextProps . data ) {
181- this . handleNewData ( nextProps . data ) ;
186+ this . handleNewData ( nextProps . data , grid ) ;
182187 }
183188 }
184-
185- componentWillUnmount ( ) {
186- this . grid . destroy ( ) ;
189+
190+ componentWillUnmount ( ) {
191+ console . log ( 'unmount' )
192+ if ( this . grid ) {
193+ this . grid . destroy ( ) ;
194+ this . grid . destroyed = true ;
195+ this . grid = undefined ;
196+ }
187197 }
188198
189199 render ( ) {
200+
201+
190202 return (
191- < div ref = { ( elem ) => { this . reactContainer = elem ; } } />
203+ < div >
204+ < div ref = { this . gridContainer } style = { gridStyle } />
205+ </ div >
192206 ) ;
193207 }
194- }
208+ }
209+
210+ const gridStyle = {
211+ position : 'absolute' ,
212+ top : '0' ,
213+ left : '0' ,
214+ height : '100%' ,
215+ width : '100%' ,
216+ } as const
0 commit comments