Skip to content

Commit 323be10

Browse files
WarrenGreenSTRML
andauthored
Example for horizontal compaction (react-grid-layout#1922)
* Example for horizontal compaction * Fix label * Rename 20-horizontal.jsx to 21-horizontal.jsx --------- Co-authored-by: Samuel Reed <[email protected]>
1 parent 350b950 commit 323be10

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ RGL is React-only and does not require jQuery.
5858
1. [Scaled Containers](https://react-grid-layout.github.io/react-grid-layout/examples/18-scale.html)
5959
1. [Allow Overlap](https://react-grid-layout.github.io/react-grid-layout/examples/19-allow-overlap.html)
6060
1. [All Resizable Handles](https://react-grid-layout.github.io/react-grid-layout/examples/20-resizable-handles.html)
61+
1. [Single Row Horizontal](https://react-grid-layout.github.io/react-grid-layout/examples/21-horizontal.html)
6162

6263
#### Projects Using React-Grid-Layout
6364

examples/vars.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,12 @@ module.exports = [
174174
paragraphs: [
175175
"This shows a grid with all resizable handles enabled. See the prop `resizableHandles` on the grid and grid items in the README."
176176
]
177-
}
177+
},
178+
{
179+
title: "Single Row Horizontal",
180+
source: "horizontal",
181+
paragraphs: [
182+
"This demonstrates how to constrain the elements to a single row."
183+
]
184+
},
178185
];

test/examples/21-horizontal.jsx

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React from "react";
2+
import _ from "lodash";
3+
import RGL, { WidthProvider } from "react-grid-layout";
4+
5+
const ReactGridLayout = WidthProvider(RGL);
6+
7+
export default class Horizontal extends React.PureComponent {
8+
static defaultProps = {
9+
className: "layout",
10+
items: 5,
11+
rowHeight: 5,
12+
onLayoutChange: function() {},
13+
cols: 12,
14+
compactType: "horizontal",
15+
maxRows: 1,
16+
allowOverlap: false
17+
};
18+
19+
constructor(props) {
20+
super(props);
21+
22+
const layout = this.generateLayout();
23+
this.state = { layout };
24+
}
25+
26+
generateDOM() {
27+
return _.map(_.range(this.props.items), function(i) {
28+
return (
29+
<div key={i}>
30+
<span className="text">{i}</span>
31+
</div>
32+
);
33+
});
34+
}
35+
36+
generateLayout() {
37+
const p = this.props;
38+
return _.map(new Array(p.items), function(item, i) {
39+
const y = 5;
40+
return {
41+
x: (i * 2) % 12,
42+
y: Math.floor(i / 6) * y,
43+
w: 2,
44+
h: y,
45+
i: i.toString()
46+
};
47+
});
48+
}
49+
50+
onLayoutChange(layout) {
51+
this.props.onLayoutChange(layout);
52+
}
53+
54+
render() {
55+
return (
56+
<ReactGridLayout
57+
layout={this.state.layout}
58+
onLayoutChange={this.onLayoutChange}
59+
useCSSTransforms={true}
60+
allowOverlap={true}
61+
{...this.props}
62+
>
63+
{this.generateDOM()}
64+
</ReactGridLayout>
65+
);
66+
}
67+
}
68+
69+
if (process.env.STATIC_EXAMPLES === true) {
70+
import("../test-hook.jsx").then(fn => fn.default(Horizontal));
71+
}

0 commit comments

Comments
 (0)