ant-design/components/grid/demo/playground.md

144 lines
3.5 KiB
Markdown
Raw Normal View History

2016-12-25 21:09:49 +08:00
---
order: 10
2016-12-25 21:09:49 +08:00
title:
zh-CN: 栅格配置器
en-US: Playground
---
## zh-CN
可以简单配置几种等分栅格和间距。
## en-US
A simple playground for column count and gutter.
2019-05-07 14:57:32 +08:00
```jsx
2016-12-25 21:09:49 +08:00
import { Row, Col, Slider } from 'antd';
class App extends React.Component {
gutters = {};
2018-06-27 15:55:04 +08:00
vgutters = {};
2016-12-25 21:09:49 +08:00
colCounts = {};
2018-06-27 15:55:04 +08:00
2016-12-25 21:09:49 +08:00
constructor() {
super();
this.state = {
gutterKey: 1,
vgutterKey: 1,
2016-12-25 21:09:49 +08:00
colCountKey: 2,
};
2019-05-07 14:57:32 +08:00
[8, 16, 24, 32, 40, 48].forEach((value, i) => {
this.gutters[i] = value;
});
[8, 16, 24, 32, 40, 48].forEach((value, i) => {
this.vgutters[i] = value;
});
2019-05-07 14:57:32 +08:00
[2, 3, 4, 6, 8, 12].forEach((value, i) => {
this.colCounts[i] = value;
});
2016-12-25 21:09:49 +08:00
}
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +08:00
onGutterChange = gutterKey => {
2016-12-25 21:09:49 +08:00
this.setState({ gutterKey });
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
onVGutterChange = vgutterKey => {
this.setState({ vgutterKey });
};
2019-05-07 14:57:32 +08:00
onColCountChange = colCountKey => {
2016-12-25 21:09:49 +08:00
this.setState({ colCountKey });
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2016-12-25 21:09:49 +08:00
render() {
const { gutterKey, vgutterKey, colCountKey } = this.state;
2016-12-25 21:09:49 +08:00
const cols = [];
const colCount = this.colCounts[colCountKey];
2016-12-25 21:31:33 +08:00
let colCode = '';
2016-12-25 21:09:49 +08:00
for (let i = 0; i < colCount; i++) {
cols.push(
<Col key={i.toString()} span={24 / colCount}>
<div>Column</div>
2019-05-07 14:57:32 +08:00
</Col>,
2016-12-25 21:09:49 +08:00
);
2016-12-25 21:31:33 +08:00
colCode += ` <Col span={${24 / colCount}} />\n`;
2016-12-25 21:09:49 +08:00
}
return (
<>
<span style={{ marginRight: 6 }}>Horizontal Gutter (px): </span>
<div style={{ width: '50%' }}>
<Slider
min={0}
max={Object.keys(this.gutters).length - 1}
value={gutterKey}
onChange={this.onGutterChange}
marks={this.gutters}
step={null}
/>
</div>
<span style={{ marginRight: 6 }}>Vertical Gutter (px): </span>
<div style={{ width: '50%' }}>
<Slider
min={0}
max={Object.keys(this.vgutters).length - 1}
value={vgutterKey}
onChange={this.onVGutterChange}
marks={this.vgutters}
step={null}
/>
</div>
<span style={{ marginRight: 6 }}>Column Count:</span>
<div style={{ width: '50%', marginBottom: 48 }}>
<Slider
min={0}
max={Object.keys(this.colCounts).length - 1}
value={colCountKey}
onChange={this.onColCountChange}
marks={this.colCounts}
step={null}
/>
2016-12-25 21:09:49 +08:00
</div>
<Row gutter={[this.gutters[gutterKey], this.vgutters[vgutterKey]]}>{cols}</Row>
<Row gutter={[this.gutters[gutterKey], this.vgutters[vgutterKey]]}>{cols}</Row>
<pre className="demo-code">{`<Row gutter={[${this.gutters[gutterKey]}, ${this.vgutters[vgutterKey]}]}>\n${colCode}</Row>`}</pre>
<pre className="demo-code">{`<Row gutter={[${this.gutters[gutterKey]}, ${this.vgutters[vgutterKey]}]}>\n${colCode}</Row>`}</pre>
</>
2016-12-25 21:09:49 +08:00
);
}
}
ReactDOM.render(<App />, mountNode);
2019-05-07 14:57:32 +08:00
```
2016-12-25 21:09:49 +08:00
2019-05-07 14:57:32 +08:00
```css
#components-grid-demo-playground [class~='ant-col'] {
2016-12-25 21:09:49 +08:00
background: transparent;
border: 0;
}
2019-05-07 14:57:32 +08:00
#components-grid-demo-playground [class~='ant-col'] > div {
2016-12-25 21:09:49 +08:00
height: 120px;
font-size: 14px;
2016-12-25 21:09:49 +08:00
line-height: 120px;
background: #0092ff;
border-radius: 4px;
2016-12-25 21:09:49 +08:00
}
2016-12-25 21:31:33 +08:00
#components-grid-demo-playground pre {
padding: 8px 16px;
font-size: 13px;
2016-12-25 21:31:33 +08:00
background: #f9f9f9;
border-radius: 6px;
}
#components-grid-demo-playground pre.demo-code {
direction: ltr;
}
2019-05-07 14:57:32 +08:00
```
2019-12-24 15:01:03 +08:00
<style>
[data-theme="dark"] #components-grid-demo-playground [class~='ant-col'] > div {
background: #028ac8;
}
</style>