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="進階設定" collapsible collapse-text="收合設定" expand-text="展開設定">
...
</HuiFormSection>
</template>
可以直接試的版本:
<template>
<HuiForm label-width="80px">
<HuiFormSection title="進階設定" collapsible collapse-text="收合設定" expand-text="展開設定">
<HuiFormItem label="編碼">
<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]
}