-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Brian Vaughn
committed
Jan 12, 2017
1 parent
9b2f552
commit bd77c3c
Showing
6 changed files
with
154 additions
and
9 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
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,72 @@ | ||
/** @flow */ | ||
import DefaultCellSizeCache from './defaultCellSizeCache' | ||
|
||
type IdCellSizeCacheConstructorParams = { | ||
indexToIdMap : Function, | ||
uniformRowHeight ?: boolean, | ||
uniformColumnWidth ?: boolean | ||
}; | ||
|
||
/** | ||
* Alternate CellMeasurer `cellSizeCache` implementation. | ||
* Similar to `defaultCellSizeCache` except that sizes are tied to data id rather than index. | ||
* Requires an index-to-id map function (passed in externally) to operate. | ||
*/ | ||
export default function idCellSizeCache ({ | ||
indexToIdMap, | ||
uniformColumnWidth = false, | ||
uniformRowHeight = false | ||
} : IdCellSizeCacheConstructorParams) { | ||
const cellSizeCache = new DefaultCellSizeCache({ | ||
uniformColumnWidth, | ||
uniformRowHeight | ||
}) | ||
|
||
return { | ||
clearAllColumnWidths () { | ||
cellSizeCache.clearAllColumnWidths() | ||
}, | ||
|
||
clearAllRowHeights () { | ||
cellSizeCache.clearAllRowHeights() | ||
}, | ||
|
||
clearColumnWidth (index: number) { | ||
cellSizeCache.clearColumnWidth( | ||
indexToIdMap(index) | ||
) | ||
}, | ||
|
||
clearRowHeight (index: number) { | ||
cellSizeCache.clearRowHeight( | ||
indexToIdMap(index) | ||
) | ||
}, | ||
|
||
getColumnWidth (index: number): ?number { | ||
return cellSizeCache.getColumnWidth( | ||
indexToIdMap(index) | ||
) | ||
}, | ||
|
||
getRowHeight (index: number): ?number { | ||
return cellSizeCache.getRowHeight( | ||
indexToIdMap(index) | ||
) | ||
}, | ||
|
||
setColumnWidth (index: number, width: number) { | ||
cellSizeCache.setColumnWidth( | ||
indexToIdMap(index), | ||
width | ||
) | ||
}, | ||
|
||
setRowHeight (index: number, height: number) { | ||
cellSizeCache.setRowHeight( | ||
indexToIdMap(index), | ||
height | ||
) | ||
} | ||
} | ||
} |
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,68 @@ | ||
import idCellSizeCache from './idCellSizeCache' | ||
|
||
describe('idCellSizeCache', () => { | ||
it('should track width and height using id instead of index', () => { | ||
const indexToIdMap = { | ||
0: 'foo', | ||
1: 'bar', | ||
2: 'baz' | ||
} | ||
const indexToIdMapCalls = [] | ||
const cache = idCellSizeCache({ | ||
indexToIdMap: (index) => { | ||
indexToIdMapCalls.push(index) | ||
return indexToIdMap[index] | ||
} | ||
}) | ||
|
||
// Set values with initial indices | ||
cache.setColumnWidth(0, 100) | ||
cache.setRowHeight(0, 25) | ||
cache.setColumnWidth(1, 200) | ||
cache.setRowHeight(1, 50) | ||
cache.setColumnWidth(2, 300) | ||
cache.setRowHeight(2, 75) | ||
|
||
// Mimic a sort operation | ||
indexToIdMap[0] = 'baz' | ||
indexToIdMap[1] = 'bar' | ||
indexToIdMap[2] = 'foo' | ||
|
||
// Previous widths and heights should still be available | ||
expect(cache.getColumnWidth(0)).toBe(300) | ||
expect(cache.getRowHeight(0)).toBe(75) | ||
expect(cache.getColumnWidth(1)).toBe(200) | ||
expect(cache.getRowHeight(1)).toBe(50) | ||
expect(cache.getColumnWidth(2)).toBe(100) | ||
expect(cache.getRowHeight(2)).toBe(25) | ||
|
||
// Clear a specific item | ||
cache.clearColumnWidth(0) | ||
cache.clearRowHeight(0) | ||
|
||
// Mimic a sort operation | ||
indexToIdMap[0] = 'bar' | ||
indexToIdMap[1] = 'foo' | ||
indexToIdMap[2] = 'baz' | ||
|
||
// Previous widths and heights should still be available | ||
expect(cache.getColumnWidth(0)).toBe(200) | ||
expect(cache.getRowHeight(0)).toBe(50) | ||
expect(cache.getColumnWidth(1)).toBe(100) | ||
expect(cache.getRowHeight(1)).toBe(25) | ||
expect(cache.getColumnWidth(2)).toBeUndefined() | ||
expect(cache.getRowHeight(2)).toBeUndefined() | ||
|
||
// Clear all items | ||
cache.clearAllColumnWidths() | ||
cache.clearAllRowHeights() | ||
|
||
// No sizes should be available | ||
expect(cache.getColumnWidth(0)).toBeUndefined() | ||
expect(cache.getRowHeight(0)).toBeUndefined() | ||
expect(cache.getColumnWidth(1)).toBeUndefined() | ||
expect(cache.getRowHeight(1)).toBeUndefined() | ||
expect(cache.getColumnWidth(2)).toBeUndefined() | ||
expect(cache.getRowHeight(2)).toBeUndefined() | ||
}) | ||
}) |
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,3 +1,4 @@ | ||
export default from './CellMeasurer' | ||
export CellMeasurer from './CellMeasurer' | ||
export defaultCellSizeCache from './defaultCellSizeCache' | ||
export idCellSizeCache from './idCellSizeCache' |
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