# RenderForm
对 form 表单的封装,提供常用的展开和收起功能
# 功能演示
type 支持: input、textarea、input-multiple、input-multiple-auto、year、month、date、dates、week、datetime、datetimerange、datetimerange、daterange、monthrange、inputMoney、select、autocomplete、radio、checkboxToggle、checkbox、switch、text、textMoney、cascader、tip-title
optionProps 属性可以指定 select 选择器或者 radio 选择器对应的 key 和 value
提示
select 使用了 :clearable="item.clearable ?? true" 语法
保持默认值的同时 做到可控
<template>
<jp-render-form :rules="baseRule" :list="renderList" :formData.sync="formValues"></jp-render-form>
</template>
<script>
export default {
data() {
return {
baseRule: {
name: [{ required: true, message: '请选择项目', trigger: 'change' }]
},
formValues: {
name: '',
region: '',
date: '',
resource: '',
text: '我是文本',
textMoney: 1234567,
reason: []
},
renderList: [
{
span: 24,
type: 'input',
label: '项目名称',
prop: 'name',
content: '有的特殊字段,场景需要这个提示,只有我有值的时候会显示'
},
{
span: 24,
type: 'select',
label: '项目区域',
prop: 'region',
optionList: [
{
dictValue: 'sh',
dictLabel: '上海'
},
{
dictValue: 'bj',
dictLabel: '北京'
}
]
},
{
span: 24,
type: 'select',
label: '项目区域',
prop: 'region',
optionList: [
{
dictValue: 'sh',
dictLabel: '上海'
},
{
dictValue: 'bj',
dictLabel: '北京'
}
],
on: {
change: (val) => {
console.log(val)
},
clear: () => {
console.log('clear')
}
},
clearable: false
},
{
span: 24,
type: 'radio',
label: '特殊资源',
prop: 'resource',
content: '只有我有值的时候会显示',
radioList: [
{
dictValue: 'value1',
dictLabel: '线上品牌商赞助'
},
{
dictValue: 'value2',
dictLabel: '线下场地免费'
}
]
},
{
span: 24,
type: 'radio',
label: '特殊资源2',
prop: 'resource',
radioList: [
{
id: 'value1',
typeName: '线上品牌商赞助'
},
{
id: 'value2',
typeName: '线下场地免费'
}
],
optionProps: {
label: 'typeName',
value: 'id'
}
},
{
span: 12,
type: 'daterange',
label: '项目时间',
prop: 'date'
},
{
span: 24,
type: 'text',
label: '测试text',
prop: 'text'
},
{
span: 24,
type: 'textMoney',
label: '测试textMoney',
prop: 'textMoney'
},
{
span: 24,
type: 'inputMoney',
label: '价格',
prop: 'inputMoney',
append: true,
appendContent: '月'
},
{
span: 24,
type: 'checkbox',
label: '选择题',
prop: 'reason',
checkboxList: [
{
label: '上海'
},
{
label: '北京'
}
],
on: {
change: (val) => {
console.log(val)
}
}
},
{
span: 24,
type: 'switch',
label: 'switch',
prop: 'switch',
inactiveColor: 'blue',
activeColor: 'red',
on: {
change: (val) => {
console.log(val)
}
}
},
{
span: 24,
type: 'cascader',
label: '省市区',
prop: 'reason',
options: [
{
value: 'zhinan',
label: '指南',
disabled: true,
children: [
{
value: 'shejiyuanze',
label: '设计原则',
children: [
{
value: 'yizhi',
label: '一致'
},
{
value: 'fankui',
label: '反馈'
},
{
value: 'xiaolv',
label: '效率'
},
{
value: 'kekong',
label: '可控'
}
]
},
{
value: 'daohang',
label: '导航',
children: [
{
value: 'cexiangdaohang',
label: '侧向导航'
},
{
value: 'dingbudaohang',
label: '顶部导航'
}
]
}
]
},
{
value: 'zujian',
label: '组件',
children: [
{
value: 'basic',
label: 'Basic',
children: [
{
value: 'layout',
label: 'Layout 布局'
},
{
value: 'color',
label: 'Color 色彩'
},
{
value: 'typography',
label: 'Typography 字体'
},
{
value: 'icon',
label: 'Icon 图标'
},
{
value: 'button',
label: 'Button 按钮'
}
]
},
{
value: 'form',
label: 'Form',
children: [
{
value: 'radio',
label: 'Radio 单选框'
},
{
value: 'checkbox',
label: 'Checkbox 多选框'
},
{
value: 'input',
label: 'Input 输入框'
},
{
value: 'input-number',
label: 'InputNumber 计数器'
},
{
value: 'select',
label: 'Select 选择器'
},
{
value: 'cascader',
label: 'Cascader 级联选择器'
},
{
value: 'switch',
label: 'Switch 开关'
},
{
value: 'slider',
label: 'Slider 滑块'
},
{
value: 'time-picker',
label: 'TimePicker 时间选择器'
},
{
value: 'date-picker',
label: 'DatePicker 日期选择器'
},
{
value: 'datetime-picker',
label: 'DateTimePicker 日期时间选择器'
},
{
value: 'upload',
label: 'Upload 上传'
},
{
value: 'rate',
label: 'Rate 评分'
},
{
value: 'form',
label: 'Form 表单'
}
]
},
{
value: 'data',
label: 'Data',
children: [
{
value: 'table',
label: 'Table 表格'
},
{
value: 'tag',
label: 'Tag 标签'
},
{
value: 'progress',
label: 'Progress 进度条'
},
{
value: 'tree',
label: 'Tree 树形控件'
},
{
value: 'pagination',
label: 'Pagination 分页'
},
{
value: 'badge',
label: 'Badge 标记'
}
]
},
{
value: 'notice',
label: 'Notice',
children: [
{
value: 'alert',
label: 'Alert 警告'
},
{
value: 'loading',
label: 'Loading 加载'
},
{
value: 'message',
label: 'Message 消息提示'
},
{
value: 'message-box',
label: 'MessageBox 弹框'
},
{
value: 'notification',
label: 'Notification 通知'
}
]
},
{
value: 'navigation',
label: 'Navigation',
children: [
{
value: 'menu',
label: 'NavMenu 导航菜单'
},
{
value: 'tabs',
label: 'Tabs 标签页'
},
{
value: 'breadcrumb',
label: 'Breadcrumb 面包屑'
},
{
value: 'dropdown',
label: 'Dropdown 下拉菜单'
},
{
value: 'steps',
label: 'Steps 步骤条'
}
]
},
{
value: 'others',
label: 'Others',
children: [
{
value: 'dialog',
label: 'Dialog 对话框'
},
{
value: 'tooltip',
label: 'Tooltip 文字提示'
},
{
value: 'popover',
label: 'Popover 弹出框'
},
{
value: 'card',
label: 'Card 卡片'
},
{
value: 'carousel',
label: 'Carousel 走马灯'
},
{
value: 'collapse',
label: 'Collapse 折叠面板'
}
]
}
]
},
{
value: 'ziyuan',
label: '资源',
children: [
{
value: 'axure',
label: 'Axure Components'
},
{
value: 'sketch',
label: 'Sketch Templates'
},
{
value: 'jiaohu',
label: '组件交互文档'
}
]
}
],
on: {
change: (val) => {
console.log(val)
}
}
}
]
}
}
}
</script>
显示代码
# 常见布局
该组件内部是通过 row 和 col 来实现每行展示多少个组件的,根据不同的情况,可能有一些布局值得关注。
一般情况是一行最多 4 个控件,特殊情况可以设置低于 4 个,保证美观情况下,根据实际情况布局。
主要原则:保持对齐
示例1:通过设置form-item的labelWidth来保证label对齐,字数较多的尽量往后放置
示例2:有日期选择器的情况, 较长的控件通过span设置占2个空间位置,或者单独一行
<template>
<div class="layout-wrapper">
<div class="tip">示例1:通过设置form-item的labelWidth来保证label对齐,字数较多的尽量往后放置</div>
<jp-render-form :list="renderList" :formData.sync="formValues"></jp-render-form>
<div class="tip">示例2:有日期选择器的情况, 较长的控件通过span设置占2个空间位置,或者单独一行</div>
<jp-render-form :list="renderList2" :formData.sync="formValues"></jp-render-form>
</div>
</template>
<script>
export default {
data() {
return {
formValues: {
name: '',
region: '',
date: '',
resource: '',
username: '',
phone: ''
},
renderList: [
{
span: 6,
type: 'input',
label: '项目名称',
prop: 'name'
},
{
span: 6,
type: 'select',
label: '项目区域',
prop: 'region',
optionList: [
{
value: 'sh',
label: '上海'
},
{
value: 'bj',
label: '北京'
}
]
},
{
span: 6,
type: 'input',
label: '来源',
prop: 'resource',
labelWidth: '68px'
},
{
span: 6,
type: 'input',
label: '用户名用户名',
prop: 'username'
},
{
span: 6,
type: 'input',
label: '手机号码',
prop: 'phone'
},
{
span: 6,
type: 'input',
label: '来源',
prop: 'resource',
labelWidth: '68px'
},
{
span: 6,
ml: 30,
slot: 'opration'
}
],
renderList2: [
{
span: 6,
type: 'input',
label: '项目名称',
prop: 'name'
},
{
span: 6,
type: 'select',
label: '项目区域',
prop: 'region',
optionList: [
{
dictValue: 'sh',
dictLabel: '上海'
},
{
dictValue: 'bj',
dictLabel: '北京'
}
]
},
{
span: 6,
type: 'input',
label: '来源',
prop: 'resource',
labelWidth: '68px'
},
{
span: 6,
type: 'input',
label: '用户名用户名',
prop: 'username'
},
{
span: 12,
type: 'daterange',
label: '项目时间',
prop: 'date'
},
{
span: 6,
ml: 30,
slot: 'opration'
}
]
}
}
}
</script>
<style scoped>
.layout-wrapper {
background: #f8f8f8;
}
.tip {
font-size: 14px;
padding: 12px 12px 12px 0;
}
</style>
显示代码
# 展开和收起
默认大于8个筛选条件显示展开
等于8个筛选条件
设置大于5个显示展开foldNum="5"
一直隐藏展开和收起功能,forceHidden=true
<template>
<div class="layout-wrapper">
<div class="tip">默认大于8个筛选条件显示展开</div>
<jp-render-form labelWidth="100px" :list="renderList" :formData.sync="formValues"></jp-render-form>
<div class="tip">等于8个筛选条件</div>
<jp-render-form labelWidth="100px" :list="renderList2" :formData.sync="formValues"></jp-render-form>
<div class="tip">设置大于5个显示展开foldNum="5"</div>
<jp-render-form labelWidth="100px" :foldNum="5" :list="renderList" :formData.sync="formValues" @query="_query" @reset="_reset"></jp-render-form>
<div class="tip">一直隐藏展开和收起功能,forceHidden=true</div>
<jp-render-form labelWidth="100px" :forceHidden="true" :list="renderList" :formData.sync="formValues"></jp-render-form>
</div>
</template>
<script>
export default {
methods: {
_query() {
console.log('query')
},
_reset() {
console.log('reset')
}
},
data() {
return {
formValues: {
name: '',
region: '',
date: '',
resource: '',
username: '',
phone: ''
},
renderList: [
{
type: 'tip-title',
title: '基本信息',
prop: 'section1',
theme: '#409EFF'
},
{
span: 6,
type: 'input-multiple',
label: '项目名称',
prop: 'name'
},
{
span: 6,
type: 'select',
label: '项目区域',
prop: 'region',
optionList: [
{
dictValue1: 'sh',
dictLabel1: '上海'
},
{
dictValue1: 'bj',
dictLabel1: '北京'
}
],
optionProps: {
label: 'dictLabel1',
value: 'dictValue1'
}
},
{
span: 6,
type: 'input-multiple-auto',
label: '来源',
prop: 'resource',
},
{
span: 6,
type: 'input',
label: '用户名用户名',
prop: 'username'
},
{
span: 6,
type: 'input',
label: '手机号码',
prop: 'phone'
},
{
span: 6,
type: 'input',
label: '来源',
prop: 'resource',
},
{
span: 6,
type: 'input',
label: '手机号码',
prop: 'phone'
},
{
span: 6,
type: 'input',
label: '来源1',
prop: 'resource',
},
{
span: 6,
type: 'input',
label: '手机号码',
prop: 'phone'
},
{
span: 6,
type: 'input',
label: '来源2',
prop: 'resource',
},
{ slot: 'opration', span: 6 }
],
renderList2: [
{ span: 6, type: 'input', label: '项目名称', prop: 'name' },
{ span: 6, type: 'input', label: '项目名称', prop: 'name' },
{ span: 6, type: 'input', label: '项目名称', prop: 'name' },
{ span: 6, type: 'input', label: '项目名称', prop: 'name' },
{ span: 6, type: 'input', label: '项目名称', prop: 'name' },
{ span: 6, type: 'input', label: '项目名称', prop: 'name' },
{ span: 6, type: 'input', label: '项目名称', prop: 'name' },
{ span: 6, type: 'input', label: '项目名称', prop: 'name' }
]
}
}
}
</script>
<style scoped>
.layout-wrapper {
background: #f8f8f8;
}
.tip {
font-size: 14px;
padding: 12px 12px 12px 0;
}
</style>
显示代码
# 多行输入自动展开 (input-multiple-auto)
多行输入框支持自动展开和收起功能,获得焦点时自动展开为多行,失去焦点时收起为单行。
特性说明
- 自动展开:获得焦点时自动展开为多行(默认5行)
- 自动收起:失去焦点时自动收起为单行
- 浮层效果:展开时使用绝对定位,浮在上层显示
- 智能清除:支持可配置的清除按钮(通过
mulClear属性控制) - 数据处理:支持通过
splitProp、strProp、splitFlag、strFlag等属性进行数据转换
使用示例:
{
type: 'input-multiple-auto',
prop: 'keywords',
label: '关键词',
placeholder: '请输入关键词,支持多行输入',
splitProp: 'keywordArray', // 转换为数组的属性名
splitFlag: '\n', // 分割符(默认回车)
clearable: true
}
# 标题提示 (tip-title)
在表单中插入标题提示,用于分组或说明,不需要 form-item 包裹。
特性说明
- 两种样式:支持
default(竖条装饰)和radio(圆形装饰)两种类型 - 三种尺寸:提供
default、small、mini三种尺寸规格 - 自定义主题:支持通过
theme属性自定义颜色 - 动画效果:鼠标悬停时有精美的动画效果
- 插槽支持:支持通过插槽添加额外内容
使用示例:
{
type: 'tip-title',
label: '基本信息',
title: '基本信息', // 标题文字
tipType: 'default', // 类型:default 或 radio
tipSize: 'default', // 尺寸:default、small、mini
theme: '#409EFF', // 自定义主题颜色(可选)
span: 24, // 占满一行
slot: 'customSlot' // 插槽名称(可选)
}
# Attributes
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| space | 每行的间距 | number | 14 |
| foldNum | 大于几个显示展开和收起 | number | 7 |
| forceHidden | 一直不显示展开和收起功能 | bool | false |
| mulClear | 多行输入是否显示清除按钮 | bool | true |
# List Item 配置项
表单项配置对象支持的特殊属性:
| 属性 | 说明 | 类型 | 适用类型 |
|---|---|---|---|
| type | 表单项类型 | string | 必填 |
| prop | 绑定的数据属性 | string | 必填(tip-title除外) |
| label | 标签文字 | string | 必填 |
| span | 栅格占据的列数(共24列) | number | 默认6 |
| content | 标签提示信息(显示info图标) | string | 所有类型 |
| placement | 提示信息位置 | string | 默认'top' |
| splitProp | 多行输入转数组的目标属性 | string | input-multiple/input-multiple-auto |
| strProp | 多行输入转字符串的目标属性 | string | input-multiple/input-multiple-auto |
| splitFlag | 多行输入分割符 | string | input-multiple/input-multiple-auto |
| strFlag | 多行输入转字符串的连接符 | string | input-multiple/input-multiple-auto |
| title | 标题文字 | string | tip-title |
| tipType | 标题样式类型(default/radio) | string | tip-title,默认'default' |
| tipSize | 标题尺寸(default/small/mini) | string | tip-title,默认'default' |
| theme | 自定义主题颜色 | string | tip-title |
| slot | 插槽名称 | string | tip-title |
# Events
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| query | 查询 | - |
| reset | 重置 | - |