Skip to content

Commit 597be88

Browse files
authored
Merge pull request #51 from forabi/patch-1
Support RTL in ResponsiveGrid
2 parents 16fde69 + b4485f1 commit 597be88

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ A lower value results in more frequent updates, offering smoother visual updates
383383
<td><code>false</code></td>
384384
<td> Defines the base unit height for items within the grid. This value serves as a foundational measure to determine the actual height of each grid item. The item's final height is calculated by multiplying this base unit height (<code>itemUnitHeight</code>) by the item's heightRatio, allowing for proportional scaling of items based on their content or design requirements. While <code>widthRatio</code> affects the item's width in relation to the column width, <code>itemUnitHeight</code> and <code>heightRatio</code> together define the item's vertical dimension, enabling dynamic grid layouts that adapt seamlessly to varying content sizes.</td>
385385
</tr>
386+
<tr>
387+
<td><code>direction</code></td>
388+
<td><code>string</code></td>
389+
<td><code>ltr</code></td>
390+
<td><code>false</code></td>
391+
<td> Determines the direction for arranging grid items in the layout: left-to-right (ltr) or right-to-left (rtl). </td>
392+
</tr>
386393
<tr>
387394
<td><code>virtualization</code></td>
388395
<td><code>Boolean</code></td>

src/responsive-grid/index.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
2626
keyExtractor = (_, index) => String(index), // default to item index if no keyExtractor is provided
2727
HeaderComponent = null,
2828
FooterComponent = null,
29+
direction = 'ltr',
2930
}) => {
3031
const [visibleItems, setVisibleItems] = useState<TileItem[]>([]);
3132

@@ -120,6 +121,20 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
120121
onEndReachedCalled.current = false;
121122
}, [gridItems, containerSize, virtualization]);
122123

124+
const getItemPositionStyle = (item: TileItem) => {
125+
const baseStyle = {
126+
position: 'absolute' as const,
127+
top: item.top,
128+
width: item.width,
129+
height: item.height,
130+
};
131+
132+
return {
133+
...baseStyle,
134+
...(direction === 'rtl' ? { right: item.left } : { left: item.left }),
135+
};
136+
};
137+
123138
return (
124139
<View
125140
style={[{ flexGrow: 1 }, style]}
@@ -154,16 +169,7 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
154169
{renderedItems.map((item, index) => (
155170
<View
156171
key={keyExtractor(item, index)}
157-
style={[
158-
{
159-
position: 'absolute',
160-
top: item.top,
161-
left: item.left,
162-
width: item.width,
163-
height: item.height,
164-
},
165-
itemContainerStyle,
166-
]}
172+
style={[getItemPositionStyle(item), itemContainerStyle]}
167173
>
168174
{renderItem({ item, index })}
169175
</View>

src/responsive-grid/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ export interface ResponsiveGridProps {
6868
| React.ReactElement<any, string | React.JSXElementConstructor<any>>
6969
| null
7070
| undefined;
71+
72+
/**
73+
* Determines the direction for arranging grid items in the layout: left-to-right (ltr) or right-to-left (rtl).
74+
* @default ltr
75+
*/
76+
direction?: 'rtl' | 'ltr';
7177
}
7278

7379
export interface TileItem {

0 commit comments

Comments
 (0)