FormSection 表单分组
把一张长表单按内容分成几组,每组有一个带主色竖条的标题和一条分隔线,可以折叠。放在 HuiForm 里,里面照常写 HuiFormItem。
引入
import { HuiFormSection } from '@nscode/handy-ui'
自持状态与受控回填
expanded未传时内部初始true;传入属性即受控,undefined按true显示。切换不重建表单内容。 未传属性或仅监听更新时组件自行保存,父无关重绘不重置;传入属性(含undefined)即受控,父不接受更新时显示保持父值。切换模式会输出开发诊断,不新增defaultValue等属性。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFormSection, HuiButton, HuiSpace, HuiInput } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('尚无请求')
const q1Model = ref(true)
</script>
<template>
<div class="q1-state-demo form-section-q1-demo">
<HuiButton class="q1-redraw" @click="q1Redraw++">父重绘 {{ q1Redraw }}</HuiButton>
<HuiSpace direction="vertical" fill>
<div>
<p>未传属性,自行保存</p>
<div class="q1-local">
<HuiFormSection title="资料" collapsible
><HuiInput placeholder="折叠后保留输入"
/></HuiFormSection>
</div>
</div>
<div>
<p>仅监听更新,仍自行保存</p>
<div class="q1-listener">
<HuiFormSection title="资料" collapsible @update:expanded="q1Last = String($event)"
><HuiInput placeholder="折叠后保留输入"
/></HuiFormSection>
</div>
</div>
<div>
<p>显式undefined,父值优先</p>
<div class="q1-empty">
<HuiFormSection title="资料" collapsible :expanded="undefined"
><HuiInput placeholder="折叠后保留输入"
/></HuiFormSection>
</div>
</div>
<div>
<p>固定受控,父拒绝更新</p>
<div class="q1-fixed">
<HuiFormSection
title="资料"
collapsible
:expanded="true"
@update:expanded="q1Last = String($event)"
><HuiInput placeholder="折叠后保留输入"
/></HuiFormSection>
</div>
</div>
<div>
<p>双向绑定</p>
<div class="q1-bound">
<HuiFormSection title="资料" collapsible v-model:expanded="q1Model"
><HuiInput placeholder="折叠后保留输入"
/></HuiFormSection>
</div>
</div>
<p class="q1-notification">绑定值:{{ q1Model }};最近请求:{{ q1Last }}</p>
</HuiSpace>
</div>
</template>
基础用法
title 设置标题,默认插槽放表单项。相邻两个分组之间自动留出距离,第一个分组上面不留。
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiFormSection, HuiInput } from '@nscode/handy-ui'
const form = reactive({ name: '', phone: '', email: '' })
</script>
<template>
<HuiForm :model="form" label-width="80px">
<HuiFormSection title="基础信息">
<HuiFormItem label="姓名" prop="name">
<HuiInput v-model="form.name" />
</HuiFormItem>
</HuiFormSection>
<HuiFormSection title="联系方式">
<HuiFormItem label="手机号" prop="phone">
<HuiInput v-model="form.phone" />
</HuiFormItem>
<HuiFormItem label="邮箱" prop="email">
<HuiInput v-model="form.email" />
</HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
可折叠
collapsible 在标题行右侧显示折叠按钮,展开时文字是“收起”、收起时是“展开”,后面的箭头跟着转向;收起、展开有高度动画。用 v-model:expanded 读取和控制展开状态,默认展开。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiForm, HuiFormItem, HuiFormSection, HuiInput } from '@nscode/handy-ui'
const expanded = ref(false)
</script>
<template>
<HuiForm label-width="80px">
<HuiFormSection v-model:expanded="expanded" title="高级设置" collapsible>
<HuiFormItem label="编码">
<HuiInput />
</HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
v-model:expanded 是双向的:点折叠按钮会改绑定的值,改绑定的值也会展开、收起分组。下面用外面的按钮控制两个分组:
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiButton,
HuiForm,
HuiFormItem,
HuiFormSection,
HuiInput,
HuiSpace
} from '@nscode/handy-ui'
const baseExpanded = ref(true)
const advancedExpanded = ref(false)
const setAll = (value: boolean) => {
baseExpanded.value = value
advancedExpanded.value = value
}
</script>
<template>
<HuiSpace wrap>
<HuiButton size="small" @click="setAll(true)">全部展开</HuiButton>
<HuiButton size="small" @click="setAll(false)">全部收起</HuiButton>
<span
>基础信息:{{ baseExpanded ? '展开' : '收起' }},高级设置:{{
advancedExpanded ? '展开' : '收起'
}}</span
>
</HuiSpace>
<HuiForm label-width="80px" style="margin-top: 16px">
<HuiFormSection v-model:expanded="baseExpanded" title="基础信息" collapsible>
<HuiFormItem label="名称">
<HuiInput />
</HuiFormItem>
</HuiFormSection>
<HuiFormSection v-model:expanded="advancedExpanded" title="高级设置" collapsible>
<HuiFormItem label="编码">
<HuiInput />
</HuiFormItem>
<HuiFormItem label="排序">
<HuiInput />
</HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
collapse-text、expand-text 改这一处的按钮文字(整个应用切换语言见《国际化》):
<template>
<HuiFormSection title="Advanced" collapsible collapse-text="Collapse" expand-text="Expand">
...
</HuiFormSection>
</template>
可以直接试的版本:
<template>
<HuiForm label-width="80px">
<HuiFormSection title="Advanced" collapsible collapse-text="Collapse" expand-text="Expand">
<HuiFormItem label="Code">
<HuiInput />
</HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
标题、提示和右侧按钮
title插槽替换标题文字,可以放必填星号这类内容;tip插槽显示在标题后面,是次要色的小一号文字,适合放说明或帮助图标;extra插槽放在标题行右侧、折叠按钮前面,适合放“新增”这类按钮。
<script setup lang="ts">
import { HuiFormSection, HuiButton, HuiIcon, HuiTooltip } from '@nscode/handy-ui'
</script>
<template>
<HuiFormSection title="联系人" collapsible>
<template #tip>至少填写一位联系人</template>
<template #extra>
<HuiButton size="small" type="primary" icon="plus">新增</HuiButton>
</template>
...
</HuiFormSection>
<HuiFormSection>
<template #title>结算信息 <span style="color: var(--hui-color-danger)">*</span></template>
<template #tip>
<HuiTooltip content="开票信息以财务系统为准">
<HuiIcon name="help" />
</HuiTooltip>
</template>
...
</HuiFormSection>
</template>
放进表单的完整例子:点“新增”加一位联系人,联系人超过一位时每行可以删除。删除先确认姓名和电话的移除范围,取消、关闭或按 Esc 保留数据;等待期间列表、联系人内容、只读状态变化或示例卸载,旧确认作废。“结算信息”的标题用插槽加了红色星号,提示是一个带气泡说明的图标。
<script setup lang="ts">
import { onBeforeUnmount, reactive, ref, watch } from 'vue'
import {
HuiButton,
HuiCheckbox,
HuiForm,
HuiFormItem,
HuiFormSection,
HuiIcon,
HuiInput,
HuiMessageBox,
HuiSpace,
HuiTooltip
} from '@nscode/handy-ui'
interface ContactItem {
id: number
name: string
phone: string
}
let seed = 1
let revision = 0
let active = true
const readonly = ref(false)
const removing = ref(false)
const form = reactive({
contacts: [{ id: seed, name: '', phone: '' }],
taxNo: ''
})
watch(
[() => form.contacts, readonly],
() => {
revision++
},
{ deep: true, flush: 'sync' }
)
onBeforeUnmount(() => {
active = false
revision++
})
const addContact = () => {
if (!readonly.value) form.contacts.push({ id: ++seed, name: '', phone: '' })
}
const removeContact = async (contact: ContactItem) => {
const index = form.contacts.indexOf(contact)
if (!active || readonly.value || removing.value || index < 0 || form.contacts.length <= 1)
return
const requestedRevision = revision
removing.value = true
try {
await HuiMessageBox.confirm(
`确定删除联系人 ${index + 1}(${contact.name || '未命名'})吗?将一并移除该联系人的姓名和电话,其他联系人及结算信息保留。`,
'删除联系人',
{
type: 'warning',
confirmButtonType: 'danger',
confirmButtonText: '删除',
cancelButtonText: '取消'
}
)
if (
!active ||
readonly.value ||
revision !== requestedRevision ||
form.contacts[index] !== contact ||
form.contacts.length <= 1
)
return
form.contacts.splice(index, 1)
} catch {
// 取消、关闭或按 Esc,保持联系人和结算信息。
} finally {
removing.value = false
}
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCheckbox v-model="readonly">只读</HuiCheckbox>
<HuiForm :model="form" :readonly="readonly" label-width="80px">
<HuiFormSection title="联系人" collapsible>
<template #tip>至少填写一位联系人</template>
<template #extra>
<HuiButton
size="small"
type="primary"
icon="plus"
:disabled="readonly"
@click="addContact"
>新增</HuiButton
>
</template>
<div v-for="(contact, index) in form.contacts" :key="contact.id" class="contact-row">
<HuiFormItem :label="`联系人 ${index + 1}`" :prop="`contacts.${index}.name`">
<HuiInput v-model="contact.name" placeholder="姓名" />
</HuiFormItem>
<HuiFormItem label="电话" :prop="`contacts.${index}.phone`">
<HuiInput v-model="contact.phone" />
</HuiFormItem>
<HuiButton
v-if="form.contacts.length > 1"
variant="text"
type="danger"
icon="trash"
aria-label="删除这位联系人"
:disabled="readonly || removing"
@click="removeContact(contact)"
/>
</div>
</HuiFormSection>
<HuiFormSection>
<template #title>结算信息 <span style="color: var(--hui-color-danger)">*</span></template>
<template #tip>
<HuiTooltip content="开票信息以财务系统为准">
<HuiIcon name="help" />
</HuiTooltip>
</template>
<HuiFormItem label="税号" prop="taxNo">
<HuiInput v-model="form.taxNo" />
</HuiFormItem>
</HuiFormSection>
</HuiForm>
</HuiSpace>
</template>
<style scoped>
.contact-row {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
column-gap: var(--hui-spacing-4);
}
.contact-row > :deep(.hui-form-item) {
flex: 1;
min-width: 200px;
}
</style>
不同尺寸
size 有 large、default、small 三种:
| 尺寸 | 标题 | 提示、折叠按钮文字 | 折叠箭头 | 标题行最小高度 | 标题线到内容、分组之间的距离 |
|---|---|---|---|---|---|
| large | 18px | 16px | 18px | 40px | 20px |
| default | 16px | 14px | 16px | 32px | 16px |
| small | 14px | 12px | 14px | 24px | 12px |
标题行的最小高度和同尺寸的按钮一样,extra 里放同尺寸的按钮时标题行不会变高。不写 size 时跟随所在 HuiForm 的 size,表单也没写时跟随 HuiConfigProvider,都没有时是 default。
下面三个分组分别写了 size,标题、提示、折叠按钮和右侧同尺寸的按钮一起对比:
<template>
<HuiForm label-width="80px">
<HuiFormSection title="大(large)" size="large" collapsible>
<template #tip>提示文字</template>
<template #extra>
<HuiButton size="large" icon="plus">新增</HuiButton>
</template>
<HuiFormItem label="名称" size="large"><HuiInput /></HuiFormItem>
</HuiFormSection>
<HuiFormSection title="默认(default)" size="default" collapsible>
<template #tip>提示文字</template>
<template #extra>
<HuiButton icon="plus">新增</HuiButton>
</template>
<HuiFormItem label="名称"><HuiInput /></HuiFormItem>
</HuiFormSection>
<HuiFormSection title="小(small)" size="small" collapsible>
<template #tip>提示文字</template>
<template #extra>
<HuiButton size="small" icon="plus">新增</HuiButton>
</template>
<HuiFormItem label="名称" size="small"><HuiInput /></HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
没写 size 的分组跟随表单,例如 <HuiForm size="large"> 里的分组就是 large。切换表单的 size,分组、表单项和控件一起变:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiForm, HuiFormItem, HuiFormSection, HuiInput, HuiSegmented } from '@nscode/handy-ui'
import type { FormSize } from '@nscode/handy-ui'
const size = ref<string | number>('large')
const sizes = [
{ label: '大(large)', value: 'large' },
{ label: '默认(default)', value: 'default' },
{ label: '小(small)', value: 'small' }
]
</script>
<template>
<HuiSegmented v-model="size" :options="sizes" />
<HuiForm :size="size as FormSize" label-width="80px" style="margin-top: 16px">
<HuiFormSection title="基础信息" collapsible>
<template #tip>跟随表单的 size</template>
<HuiFormItem label="名称"><HuiInput /></HuiFormItem>
</HuiFormSection>
<HuiFormSection title="联系方式">
<HuiFormItem label="手机号"><HuiInput /></HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
分组里用栅格
表单本身设置 columns 或 item-min-width 后,分组自动占满一行,内容沿用表单列设置,直接给 HuiFormItem 写 span 即可。完整示例见 Form 的分组沿用分列。下面的 HuiRow、HuiCol 写法继续适用于手动布局。
分组的内容区就是普通的容器,可以放 HuiRow、HuiCol 排多列。
<template>
<HuiForm label-width="80px">
<HuiFormSection title="基础信息">
<HuiRow :gutter="24">
<HuiCol :span="12"
><HuiFormItem label="名称"><HuiInput /></HuiFormItem
></HuiCol>
<HuiCol :span="12"
><HuiFormItem label="编码"><HuiInput /></HuiFormItem
></HuiCol>
</HuiRow>
</HuiFormSection>
</HuiForm>
</template>
校验失败时展开分组
收起的分组里的表单项照常参与校验,但提示被藏起来看不到。可以在 validate() 之后检查没通过的字段属于哪个分组,把那个分组的 expanded 设回 true。下面“高级设置”默认收起,里面的“编码”必填,直接点“保存”试试:
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiForm,
HuiFormItem,
HuiFormSection,
HuiInput,
HuiMessage
} from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const formRef = ref<InstanceType<typeof HuiForm>>()
const advancedExpanded = ref(false)
const form = reactive({ name: '默认名称', code: '' })
const rules: Record<string, FormRule[]> = {
name: [{ required: true, message: '请输入名称' }],
code: [{ required: true, message: '请输入编码' }]
}
// 高级设置分组里的字段
const advancedFields = ['code']
const save = async () => {
const { valid, errors } = await formRef.value!.validate()
if (valid) {
HuiMessage.success('保存成功')
return
}
if (Object.keys(errors).some((prop) => advancedFields.includes(prop))) {
advancedExpanded.value = true
}
}
</script>
<template>
<HuiForm ref="formRef" :model="form" :rules="rules" label-width="80px" @submit.prevent>
<HuiFormSection title="基础信息">
<HuiFormItem label="名称" prop="name">
<HuiInput v-model="form.name" />
</HuiFormItem>
</HuiFormSection>
<HuiFormSection v-model:expanded="advancedExpanded" title="高级设置" collapsible>
<HuiFormItem label="编码" prop="code">
<HuiInput v-model="form.code" />
</HuiFormItem>
</HuiFormSection>
<HuiFormItem>
<HuiButton type="primary" @click="save">保存</HuiButton>
</HuiFormItem>
</HuiForm>
</template>
祖先变量覆盖
公开变量可写在自身或任意祖先上,覆盖字号、标题行最小高度和分组间距;尺寸类只设置私有默认。嵌套分组继承祖先公开配置,各自重置尺寸默认。首个分组仍不加外侧上间距;没有标题行时不会额外增加标题到内容的间距。
| 变量 | 默认值 | 说明 |
|---|---|---|
--hui-form-section-title-font-size | large/default/small 为18/16/14px | 标题字号 |
--hui-form-section-text-font-size | large/default/small 为16/14/12px | 提示和折叠按钮字号 |
--hui-form-section-header-height | 同尺寸控件高度 | 标题行最小高度 |
--hui-form-section-gap | large/default/small 为20/16/12px | 分组外侧及标题到内容间距 |
原--hui-form-section-title-size、--hui-form-section-text-size更名为上面的-font-size变量,不保留旧名兜底。
<script setup lang="ts">
import { HuiFormSection } from '@nscode/handy-ui'
</script>
<template>
<div
class="form-section-variables-demo"
style="--hui-form-section-title-font-size: 20px; --hui-form-section-text-font-size: 14px; --hui-form-section-header-height: 48px; --hui-form-section-gap: 24px"
>
<HuiFormSection title="大号默认" size="large" collapsible>
<template #tip>提示文字</template>
<span>分组内容保留,收起再展开可继续查看。</span>
</HuiFormSection>
<HuiFormSection title="小号默认" size="small" collapsible>
<template #tip>提示文字</template>
<span>公开变量覆盖字号、高度和间距,各尺寸私有默认仍独立。</span>
</HuiFormSection>
</div>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| title | string | - | 分组标题;也可以用 title 插槽 |
| collapsible | boolean | false | 是否可折叠。为 false 时内容一直显示,不显示折叠按钮 |
| expanded | boolean | undefined | 是否展开,支持 v-model:expanded,仅在 collapsible 时起作用。未传时内部初值 true;传入属性即受控,undefined 按 true 显示。 |
| collapseText | string | 随语言(中文:收起) | 展开时折叠按钮的文字 |
| expandText | string | 随语言(中文:展开) | 收起时折叠按钮的文字 |
| size | 'large' | 'default' | 'small' | - | 尺寸。不写时跟随所在 HuiForm 的 size,再没有就跟随 HuiConfigProvider,都没有时是 'default' |
Events
| 事件 | 回调参数 | 说明 |
|---|---|---|
| update:expanded | (expanded: boolean) | 点折叠按钮切换展开状态时触发,v-model:expanded 用的就是它 |
Slots
| 插槽 | 作用域参数 | 说明 |
|---|---|---|
| default | - | 分组的内容,一般是 HuiFormItem |
| title | - | 自定义标题,替换 title 属性 |
| tip | - | 标题后面的提示,次要色、比标题小一号 |
| extra | - | 标题行右侧的内容,在折叠按钮前面 |
方法
没有对外暴露的方法。用 v-model:expanded 控制展开和收起。
类型
import type { FormSectionProps, FormSectionEmits, FormSectionSize } from '@nscode/handy-ui'
type FormSectionSize = 'large' | 'default' | 'small'
interface FormSectionProps {
title?: string
collapsible?: boolean
expanded?: boolean
collapseText?: string
expandText?: string
size?: FormSectionSize
}
interface FormSectionEmits {
'update:expanded': [expanded: boolean]
}