Skip to content

Commit 4b80e00

Browse files
committed
Updated Documentation
1 parent 14fc466 commit 4b80e00

File tree

11 files changed

+373
-76
lines changed

11 files changed

+373
-76
lines changed

README.md

Lines changed: 327 additions & 37 deletions
Large diffs are not rendered by default.

example/src/GridBoardExample.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ export default function GridBoardExamplePage() {
155155
</Text>
156156

157157
<FlexGrid
158-
maxWidthRatio={60}
159-
tileSize={60}
158+
maxColumnRatioUnits={60}
159+
itemSizeUnit={60}
160160
data={data}
161161
virtualizedBufferFactor={2}
162162
renderItem={renderItem}

example/src/InstagramExploreExample.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export default function InstagramExploreExample() {
7272
maxItemsPerColumn={3}
7373
data={repeatedData}
7474
renderItem={renderItem}
75-
virtualization={true}
7675
showScrollIndicator={false}
7776
style={{
7877
padding: 5,

example/src/PinterestHomeExample.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default function PinterestExample() {
6363
maxItemsPerColumn={2}
6464
data={repeatedData}
6565
renderItem={renderItem}
66-
tileHeight={80} // set tileHeight to control uneven tiles
66+
itemUnitHeight={80} // set itemUnitHeight to control uneven tiles
6767
style={{
6868
padding: 5,
6969
}}

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@
3434
"react-native",
3535
"ios",
3636
"android",
37-
"flexible-grid",
3837
"grid",
39-
"grid-system",
40-
"expo"
38+
"grid-layout",
39+
"masonry"
4140
],
4241
"repository": {
4342
"type": "git",
@@ -128,4 +127,4 @@
128127
"directories": {
129128
"example": "example"
130129
}
131-
}
130+
}

src/flex-grid/calc-flex-grid.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import type { FlexGridItem, FlexGridTile } from './types';
22

33
export const calcFlexGrid = (
44
data: FlexGridTile[],
5-
maxWidthRatio: number,
6-
tileSize: number
5+
maxColumnRatioUnits: number,
6+
itemSizeUnit: number
77
): {
88
gridItems: FlexGridItem[];
99
totalHeight: number;
1010
totalWidth: number;
1111
} => {
1212
const gridItems: FlexGridItem[] = [];
13-
let columnHeights = new Array(maxWidthRatio).fill(0); // Track the height of each column.
13+
let columnHeights = new Array(maxColumnRatioUnits).fill(0); // Track the height of each column.
1414

1515
data.forEach((item) => {
1616
const widthRatio = item.widthRatio || 1;
@@ -19,7 +19,7 @@ export const calcFlexGrid = (
1919
// Find the column with the minimum height to start placing the current item.
2020
let columnIndex = columnHeights.indexOf(Math.min(...columnHeights));
2121
// If the item doesn't fit in the remaining columns, reset the row.
22-
if (widthRatio + columnIndex > maxWidthRatio) {
22+
if (widthRatio + columnIndex > maxColumnRatioUnits) {
2323
columnIndex = 0;
2424
const maxHeight = Math.max(...columnHeights);
2525
columnHeights.fill(maxHeight); // Align all columns to the height of the tallest column.
@@ -29,18 +29,22 @@ export const calcFlexGrid = (
2929
gridItems.push({
3030
...item,
3131
top: columnHeights[columnIndex],
32-
left: columnIndex * tileSize,
32+
left: columnIndex * itemSizeUnit,
3333
});
3434

3535
// Update the heights of the columns spanned by this item.
3636
for (let i = columnIndex; i < columnIndex + widthRatio; i++) {
37-
columnHeights[i] += heightRatio * tileSize;
37+
columnHeights[i] += heightRatio * itemSizeUnit;
3838
}
3939
});
4040

4141
// After positioning all data, calculate the total height of the grid.
4242
const totalHeight = Math.max(...columnHeights);
4343

4444
// Return the positioned data and the total height of the grid.
45-
return { gridItems, totalHeight, totalWidth: maxWidthRatio * tileSize };
45+
return {
46+
gridItems,
47+
totalHeight,
48+
totalWidth: maxColumnRatioUnits * itemSizeUnit,
49+
};
4650
};

src/flex-grid/index.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { calcFlexGrid } from './calc-flex-grid';
1010
export const FlexGrid: React.FC<FlexGridProps> = ({
1111
data = [],
1212
virtualization = true,
13-
maxWidthRatio = 1,
14-
tileSize = 50,
13+
maxColumnRatioUnits = 1,
14+
itemSizeUnit = 50,
1515
scrollEventInterval = 200, // milliseconds
1616
virtualizedBufferFactor = 2,
1717
showScrollIndicator = true,
18-
renderItem,
18+
renderItem = () => null,
1919
style = {},
2020
}) => {
2121
const [visibleItems, setVisibleItems] = useState<FlexGridTile[]>([]);
@@ -33,8 +33,8 @@ export const FlexGrid: React.FC<FlexGridProps> = ({
3333
);
3434

3535
const { totalHeight, totalWidth, gridItems } = useMemo(() => {
36-
return calcFlexGrid(data, maxWidthRatio, tileSize);
37-
}, [data, maxWidthRatio, tileSize]);
36+
return calcFlexGrid(data, maxColumnRatioUnits, itemSizeUnit);
37+
}, [data, maxColumnRatioUnits, itemSizeUnit]);
3838

3939
const renderedList = virtualization ? visibleItems : gridItems;
4040

@@ -51,8 +51,8 @@ export const FlexGrid: React.FC<FlexGridProps> = ({
5151
const visibleEndY = scrollPosition.current.y + containerHeight + bufferY;
5252

5353
const vItems = gridItems.filter((item) => {
54-
const itemRight = item.left + (item.widthRatio || 1) * tileSize;
55-
const itemBottom = item.top + (item.heightRatio || 1) * tileSize;
54+
const itemRight = item.left + (item.widthRatio || 1) * itemSizeUnit;
55+
const itemBottom = item.top + (item.heightRatio || 1) * itemSizeUnit;
5656
return (
5757
item.left < visibleEndX &&
5858
itemRight > visibleStartX &&
@@ -109,13 +109,13 @@ export const FlexGrid: React.FC<FlexGridProps> = ({
109109
};
110110

111111
useEffect(() => {
112-
//recalculate visible items when tileSize or maxWidthRatio or data changes
112+
//recalculate visible items when itemSizeUnit or maxColumnRatioUnits or data changes
113113
if (virtualization) {
114114
updateVisibleItems();
115115
}
116116
}, [
117-
tileSize,
118-
maxWidthRatio,
117+
itemSizeUnit,
118+
maxColumnRatioUnits,
119119
data,
120120
virtualization,
121121
containerHeight,
@@ -160,8 +160,8 @@ export const FlexGrid: React.FC<FlexGridProps> = ({
160160
position: 'absolute',
161161
top: item.top,
162162
left: item.left,
163-
width: (item.widthRatio || 1) * tileSize,
164-
height: (item.heightRatio || 1) * tileSize,
163+
width: (item.widthRatio || 1) * itemSizeUnit,
164+
height: (item.heightRatio || 1) * itemSizeUnit,
165165
}}
166166
>
167167
{renderItem(item, index)}

src/flex-grid/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { ReactNode } from 'react';
22
import type { StyleProp, ViewStyle } from 'react-native';
33

44
export interface FlexGridProps {
5-
maxWidthRatio: number;
5+
maxColumnRatioUnits: number;
66
virtualization?: boolean;
7-
tileSize: number;
7+
itemSizeUnit: number;
88
renderItem: (item: FlexGridTile, index: number) => ReactNode;
99
data: FlexGridTile[];
1010
virtualizedBufferFactor?: number;

src/responsive-grid/calc-responsive-grid.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ export const calcResponsiveGrid = (
44
data: TileItem[],
55
maxItemsPerColumn: number,
66
containerWidth: number,
7-
tileHeight?: number
7+
itemUnitHeight?: number
88
): {
99
gridItems: GridItem[];
1010
gridViewHeight: number;
1111
} => {
1212
const gridItems: GridItem[] = [];
13-
const tileSize = containerWidth / maxItemsPerColumn; // Determine TileSize based on container width and max number of columns
13+
const itemSizeUnit = containerWidth / maxItemsPerColumn; // Determine TileSize based on container width and max number of columns
1414
let columnHeights: number[] = new Array(maxItemsPerColumn).fill(0); // Track the height of each column end.
1515

1616
data.forEach((item) => {
1717
const widthRatio = item.widthRatio || 1;
1818
const heightRatio = item.heightRatio || 1;
1919

20-
const itemWidth = widthRatio * tileSize;
20+
const itemWidth = widthRatio * itemSizeUnit;
2121

22-
const itemHeight = tileHeight
23-
? tileHeight * heightRatio
24-
: heightRatio * tileSize; // Use tileHeight if provided, else fallback to tileSize
22+
const itemHeight = itemUnitHeight
23+
? itemUnitHeight * heightRatio
24+
: heightRatio * itemSizeUnit; // Use itemUnitHeight if provided, else fallback to itemSizeUnit
2525

2626
// Find the column where the item should be placed.
2727
let columnIndex = findColumnForItem(
@@ -32,7 +32,7 @@ export const calcResponsiveGrid = (
3232

3333
// Calculate item's top and left positions.
3434
const top = columnHeights[columnIndex]!;
35-
const left = columnIndex * tileSize;
35+
const left = columnIndex * itemSizeUnit;
3636

3737
// Place the item.
3838
gridItems.push({

src/responsive-grid/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const ResponsiveGrid: React.FC<FlexGridProps> = ({
1616
virtualization = true,
1717
showScrollIndicator = true,
1818
style = {},
19-
tileHeight,
19+
itemUnitHeight,
2020
}) => {
2121
const [visibleItems, setVisibleItems] = useState<TileItem[]>([]);
2222
const throttleScrollTimeout = useRef<ReturnType<typeof setTimeout> | null>(
@@ -30,7 +30,12 @@ export const ResponsiveGrid: React.FC<FlexGridProps> = ({
3030

3131
const { gridViewHeight, gridItems } = useMemo(
3232
() =>
33-
calcResponsiveGrid(data, maxItemsPerColumn, containerWidth, tileHeight),
33+
calcResponsiveGrid(
34+
data,
35+
maxItemsPerColumn,
36+
containerWidth,
37+
itemUnitHeight
38+
),
3439
[data, maxItemsPerColumn, containerWidth]
3540
);
3641

0 commit comments

Comments
 (0)