Browse Source

fix colGroupNode memoization for SimpleScrollGrid

Adam Shaw 6 years ago
parent
commit
b02953c0e0

+ 2 - 1
packages/core/src/main.ts

@@ -181,7 +181,8 @@ export {
   getChunkClassNames, ChunkContentCallbackArgs,
   computeScrollerClientWidths, computeScrollerClientHeights,
   sanitizeShrinkWidth,
-  ChunkConfigRowContent, ChunkConfigContent
+  ChunkConfigRowContent, ChunkConfigContent,
+  isColPropsEqual
 } from './scrollgrid/util'
 export { default as Scroller, ScrollerProps, OverflowValue } from './scrollgrid/Scroller'
 export { getScrollbarWidths } from './util/scrollbar-width'

+ 4 - 1
packages/core/src/scrollgrid/SimpleScrollGrid.tsx

@@ -5,6 +5,7 @@ import Scroller, { OverflowValue } from './Scroller'
 import RefMap from '../util/RefMap'
 import { ColProps, SectionConfig, renderMicroColGroup, computeShrinkWidth, getScrollGridClassNames, getSectionClassNames, getNeedsYScrolling,
   renderChunkContent, getChunkVGrow, computeForceScrollbars, ChunkConfig, hasShrinkWidth, CssDimValue, getChunkClassNames, computeScrollerClientWidths, computeScrollerClientHeights,
+  isColPropsEqual
   } from './util'
 import { memoize } from '../util/memoize'
 import { isPropsEqual } from '../util/object'
@@ -34,6 +35,7 @@ interface SimpleScrollGridState {
 
 export default class SimpleScrollGrid extends BaseComponent<SimpleScrollGridProps, SimpleScrollGridState> {
 
+  processCols = memoize((a) => a, isColPropsEqual) // so we get same `cols` props every time
   renderMicroColGroup = memoize(renderMicroColGroup) // yucky to memoize VNodes, but much more efficient for consumers
   scrollerRefs = new RefMap<Scroller>()
   scrollerElRefs = new RefMap<HTMLElement, [ChunkConfig]>(this._handleScrollerEl.bind(this))
@@ -49,10 +51,11 @@ export default class SimpleScrollGrid extends BaseComponent<SimpleScrollGridProp
 
   render(props: SimpleScrollGridProps, state: SimpleScrollGridState, context: ComponentContext) {
     let sectionConfigs = props.sections || []
+    let cols = this.processCols(props.cols)
 
     let microColGroupNode = props.forPrint ?
         <colgroup></colgroup> : // temporary
-        this.renderMicroColGroup(props.cols, state.shrinkWidth)
+        this.renderMicroColGroup(cols, state.shrinkWidth)
 
     let classNames = getScrollGridClassNames(props.vGrow, context)
     if (props.forPrint) { // temporary

+ 7 - 1
packages/core/src/scrollgrid/util.tsx

@@ -2,8 +2,9 @@ import { VNode, h, Ref } from '../vdom'
 import { findElements } from '../util/dom-manip'
 import ComponentContext from '../component/ComponentContext'
 import { computeSmallestCellWidth } from '../util/misc'
-import { mapHash } from '../util/object'
+import { mapHash, isPropsEqual } from '../util/object'
 import RefMap from '../util/RefMap'
+import { isArraysEqual } from '../util/array'
 
 
 export type CssDimValue = string | number
@@ -114,6 +115,11 @@ export function renderChunkContent(sectionConfig: SectionConfig, chunkConfig: Ch
 }
 
 
+export function isColPropsEqual(cols0: ColProps[], cols1: ColProps[]) {
+  return isArraysEqual(cols0, cols1, isPropsEqual)
+}
+
+
 export function renderMicroColGroup(cols: ColProps[], shrinkWidth?: number) {
   let colNodes: VNode[] = []