File size: 8,285 Bytes
c9d0005 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | ---
order: 0
toc: content
mobile: false
group:
title: 高级用法
order: 1
---
# 表单布局
- `displayType` 标签排列方式
- `column` 表单一行能展示的表单项个数
- `labelWidth` 标签固定宽度
- `cellSpan` 表单项跨列
- `span` 自定义宽度
- `maxWidth` 输入控件最长宽度
- `labelCol`、`FieldCol` 表单项内部布局
- `footer` 内置按钮
## displayType
- displayType(标签排列方式):`row`(水平)| `column`(垂直)| `inline`(紧凑)
- 默认值:`column`
```jsx
import React, { useState } from 'react';
import { Button, Space, Form, Radio } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/basic';
export default () => {
const form = useForm();
const [displayType, setDisplay] = useState('row');
const handRadioChange = (ev) => {
const value = ev.target.value;
schema.displayType = value;
form.setSchema(schema, true);
setDisplay(value);
};
return (
<div>
<Form.Item label='displayType' style={{ marginBottom: '50px' }}>
<Radio.Group value={displayType} onChange={handRadioChange}>
<Radio.Button value='row'>row</Radio.Button>
<Radio.Button value='column'>column</Radio.Button>
<Radio.Button value='inline'>Inline</Radio.Button>
</Radio.Group>
</Form.Item>
<FormRender
schema={schema}
form={form}
labelCol={6}
fieldCol={17}
/>
</div>
);
}
```
## column
一行可以展示多少个表单项,默认值: 1
```jsx
import React, { useState } from 'react';
import { Button, Space, Form, Radio } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/basic';
export default () => {
const form = useForm();
const [column, setColumn] = useState(3);
const handRadioChange = (ev) => {
const value = ev.target.value;
schema.column = value;
form.setSchema(schema, true);
setColumn(value);
};
return (
<>
<Form.Item label='column' style={{ marginBottom: '50px' }}>
<Radio.Group value={column} onChange={handRadioChange}>
<Radio.Button value={1}>一列</Radio.Button>
<Radio.Button value={2}>两列</Radio.Button>
<Radio.Button value={3}>三列</Radio.Button>
<Radio.Button value={4}>四列</Radio.Button>
</Radio.Group>
</Form.Item>
<FormRender
form={form}
schema={schema}
labelCol={6}
fieldCol={17}
/>
</>
);
}
```
## lableWidth
设置标签固定宽度
```jsx
import React, { useState } from 'react';
import { InputNumber } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/basic';
export default () => {
const form = useForm();
const [labelWidth, setLabelWidth] = useState(60);
return (
<>
<div style={{ marginBottom: '50px' }}>
labelWidth:
<InputNumber onChange={setLabelWidth} value={labelWidth} />
</div>
<FormRender
form={form}
schema={schema}
labelWidth={labelWidth}
column={3}
/>
</>
);
};
```
## cellSpan
设置表单项跨列展示,目前需配合 `lableWidth` 来使用,否则无法与其他表单项在样式上对齐,通过配置单个表单项的 `labelCol`、`fieldCol` 勉强能改善
```jsx
import React, { useState } from 'react';
import { InputNumber } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/cellSpan';
export default () => {
const form = useForm();
const [labelWidth, setLabelWidth] = useState(60);
return (
<FormRender
form={form}
schema={schema}
labelWidth={60}
column={3}
/>
);
};
```
## span
设置表单项列宽度,表单布局会被切割成 24 等份,可以通过设置 span 来自定义表单项所占的宽度
```jsx
import React, { useState } from 'react';
import { InputNumber } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/span';
export default () => {
const form = useForm();
const [labelWidth, setLabelWidth] = useState(60);
return (
<FormRender
form={form}
schema={schema}
labelWidth={80}
maxWidth={300}
/>
);
};
```
## maxWidth
表单项控件的最大宽度
```jsx
import React, { useState } from 'react';
import { InputNumber } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/basic';
export default () => {
const form = useForm();
const [maxWidth, setMaxWidth] = useState(320);
return (
<>
<div style={{ marginBottom: '50px' }}>
maxWidth:
<InputNumber onChange={setMaxWidth} value={maxWidth} />
</div>
<FormRender
form={form}
schema={{
...schema,
column: 1
}}
maxWidth={maxWidth}
labelWidth={60}
/>
</>
);
};
```
## labelCol & fieldCol
- `labelCol`(标签占位格数),`fieldCol`(控件占位格数)
- 默认配置:
```js
// 当表单一行 一列 展示时
labelCol: 5
fieldCol: 9
// 当表单一行 两列 展示时
labelCol: 6
fieldCol: 14
// 当表单一行 两列以上 展示时
labelCol: 7
fieldCol: 16
```
实际业务中标签可能会比较长,默认配置无法满足布局,可以通过配置 `labelCol`、`fieldCol` 进行调整,两者加起来不超过 `24` 格数即可。
`labelCol`、`fieldCol` 也可以是复杂对象,具体配置规则参照 Antd Col 组件
```jsx
import React, { useState } from 'react';
import { InputNumber, Space } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/basic';
export default () => {
const form = useForm();
const [labelCol, setLabelCol] = useState(6);
const [fieldCol, setFieldcol] = useState(17);
return (
<>
<Space style={{ marginBottom: '50px' }}>
<span>
labelCol:
<InputNumber onChange={setLabelCol} value={labelCol} />
</span>
<span>
fieldCol:
<InputNumber onChange={setFieldcol} value={fieldCol} />
</span>
</Space>
<FormRender
form={form}
schema={schema}
labelCol={labelCol}
fieldCol={fieldCol}
/>
</>
)
};
```
## footer
- `footer`:true,显示默认配置
```jsx
import React, { useState } from 'react';
import { Button, Space, Form, Radio } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/simple';
export default () => {
const form = useForm();
return (
<FormRender
schema={schema}
form={form}
maxWidth={360}
footer={true}
/>
);
}
```
- 按钮属性配置
```jsx
import React, { useState } from 'react';
import { Button, Space, Form, Radio } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/simple';
export default () => {
const form = useForm();
return (
<FormRender
schema={schema}
form={form}
maxWidth={360}
footer={{
submit: {
text: '确定',
// loading: true
// hide: true
// ...btnProps
},
reset: {
text: '清空',
// hide: true
// ...btnProps
}
}}
/>
);
}
```
- `footer` 自定义
```jsx
import React, { useState } from 'react';
import { Button, Space, Form, Radio } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/simple';
export default () => {
const form = useForm();
return (
<FormRender
schema={schema}
form={form}
maxWidth={360}
footer={() => (
<Button>自定义</Button>
)}
/>
);
}
```
- `footer` dom 元素透传
```jsx
import React, { useState } from 'react';
import { Button, Space, Form, Radio } from 'antd';
import FormRender, { useForm } from 'form-render';
import schema from './schema/simple';
export default () => {
const form = useForm();
return (
<FormRender
schema={schema}
form={form}
maxWidth={360}
footer={(dom) => (
<Space>{dom}</Space>
)}
/>
);
}
```
|