React-grid-layout 是一个网格布局系统,可以实现响应式的网格布局,灵活运用可以方便的实现拖拽式组件的实现
import GridLayout from 'react-grid-layout';
class MyFirstGrid extends React.Component {
render() {
// layout is an array of objects, see the demo for more complete usage
const layout = [
{i: 'a', x: 0, y: 0, w: 1, h: 2, static: true},
{i: 'b', x: 1, y: 0, w: 3, h: 2, minW: 2, maxW: 4},
{i: 'c', x: 4, y: 0, w: 1, h: 2}
];
return (
<GridLayout className="layout" layout={layout} cols={12} rowHeight={30} width={1200}>
<div key="a">a</div>
<div key="b">b</div>
<div key="c">c</div>
</GridLayout>
)
}
}
上方是一个简单的用法实现,其中有a、b、c三个项,a 设置了 static: true,因此它是不可拖拽和缩放的,b 设置了 minW: 2 和 maxW: 4,因此它在拖拽时的宽度在2个单位到4个单位,而c项未做任何限制,可以自由的拖拽和缩放
import GridLayout from 'react-grid-layout';
class MyFirstGrid extends React.Component {
render() {
return (
<GridLayout className="layout" cols={12} rowHeight={30} width={1200}>
<div key="a" data-grid={{x: 0, y: 0, w: 1, h: 2, static: true}}>a</div>
<div key="b" data-grid={{x: 1, y: 0, w: 3, h: 2, minW: 2, maxW: 4}}>b</div>
<div key="c" data-grid={{x: 4, y: 0, w: 1, h: 2}}>c</div>
</GridLayout>
)
}
}
import { Responsive as ResponsiveGridLayout } from 'react-grid-layout';
class MyResponsiveGrid extends React.Component {
render() {
// {lg: layout1, md: layout2, ...}
const layouts = getLayoutsFromSomewhere();
return (
<ResponsiveGridLayout className="layout" layouts={layouts}
breakpoints={{lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0}}
cols={{lg: 12, md: 10, sm: 6, xs: 4, xxs: 2}}>
<div key="1">1</div>
<div key="2">2</div>
<div key="3">3</div>
</ResponsiveGridLayout>
)
}
}
通过 breakpoints 和 cols 设置在不同分辨率的设备下,一行中横向有多少个单位的网格
import { Responsive, WidthProvider } from 'react-grid-layout';
const ResponsiveGridLayout = WidthProvider(Responsive);
class MyResponsiveGrid extends React.Component {
render() {
// {lg: layout1, md: layout2, ...}
var layouts = getLayoutsFromSomewhere();
return (
<ResponsiveGridLayout className="layout" layouts={layouts}
breakpoints={{lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0}}
cols={{lg: 12, md: 10, sm: 6, xs: 4, xxs: 2}}>
<div key="1">1</div>
<div key="2">2</div>
<div key="3">3</div>
</ResponsiveGridLayout>
)
}
}
通过 WidthProvider 结合 Responsive, 在窗口调整大小时就可自动调整宽度
boolean
number
string
string
boolean
'vertical' || 'horizontal'
'array
[number, number]
[number, number]
number
{ i: string, w: number, h: number }
boolean
boolean
boolean
boolean
number
boolean
boolean
Array[s' , 'w' , 'e' , 'n', 'sw', 'nw', 'se', 'ne']
function
function
function
function
function
function
string
number
number
number
number
number
number
number
number
boolean
boolean
boolean
Array['s' , 'w' , 'e' , 'n' , 'sw' , 'nw' , 'se', 'ne']
boolean