FormSection
Divides a long form into sections. Each section has a title marked with a primary-color bar, a divider and optional collapsing. Place it inside HuiForm and add HuiFormItem components as usual.
Import
import { HuiFormSection } from '@nscode/handy-ui'
Internal State and Controlled Updates
When expanded is omitted, internal state starts at true. Providing the prop makes it controlled, with undefined displayed as true. Toggling does not recreate the form content. Omitting the prop or providing only an update listener keeps internal state, which unrelated parent redraws do not reset. A supplied prop, including undefined, takes precedence; if the parent rejects an update, the displayed state remains the parent's value. Switching modes produces a development diagnostic. No additional prop such as defaultValue is introduced.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiFormSection, HuiButton, HuiSpace, HuiInput } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No request yet')
const q1Model = ref(true)
</script>
<template>
<div class="q1-state-demo form-section-q1-demo">
<HuiButton class="q1-redraw" @click="q1Redraw++">Parent redraw {{ q1Redraw }}</HuiButton>
<HuiSpace direction="vertical" fill>
<div>
<p>Prop omitted; internal state</p>
<div class="q1-local">
<HuiFormSection title="Details" collapsible
><HuiInput placeholder="Input is preserved when collapsed"
/></HuiFormSection>
</div>
</div>
<div>
<p>Update listener only; still internal state</p>
<div class="q1-listener">
<HuiFormSection title="Details" collapsible @update:expanded="q1Last = String($event)"
><HuiInput placeholder="Input is preserved when collapsed"
/></HuiFormSection>
</div>
</div>
<div>
<p>Explicit undefined; parent value takes precedence</p>
<div class="q1-empty">
<HuiFormSection title="Details" collapsible :expanded="undefined"
><HuiInput placeholder="Input is preserved when collapsed"
/></HuiFormSection>
</div>
</div>
<div>
<p>Fixed controlled value; parent rejects updates</p>
<div class="q1-fixed">
<HuiFormSection
title="Details"
collapsible
:expanded="true"
@update:expanded="q1Last = String($event)"
><HuiInput placeholder="Input is preserved when collapsed"
/></HuiFormSection>
</div>
</div>
<div>
<p>Two-way binding</p>
<div class="q1-bound">
<HuiFormSection title="Details" collapsible v-model:expanded="q1Model"
><HuiInput placeholder="Input is preserved when collapsed"
/></HuiFormSection>
</div>
</div>
<p class="q1-notification">Bound value: {{ q1Model }}; latest request: {{ q1Last }}</p>
</HuiSpace>
</div>
</template>
Basic Usage
Set the heading with title and place form items in the default slot. Adjacent sections receive spacing automatically; the first section has no outer top spacing.
<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="Basic Information">
<HuiFormItem label="Name" prop="name">
<HuiInput v-model="form.name" />
</HuiFormItem>
</HuiFormSection>
<HuiFormSection title="Contact Details">
<HuiFormItem label="Mobile" prop="phone">
<HuiInput v-model="form.phone" />
</HuiFormItem>
<HuiFormItem label="Email" prop="email">
<HuiInput v-model="form.email" />
</HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
Collapsible Sections
collapsible adds a toggle on the right side of the heading. Its localized label is Collapse while expanded and Expand while collapsed, with an arrow that rotates accordingly. Expanding and collapsing animate the content height. Use v-model:expanded to read and control expansion; sections are expanded by default.
<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="Advanced Settings" collapsible>
<HuiFormItem label="Code">
<HuiInput />
</HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
v-model:expanded works in both directions: clicking the toggle updates the variable, and changing the variable expands or collapses the section. The external buttons below control two sections:
<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)">Expand all</HuiButton>
<HuiButton size="small" @click="setAll(false)">Collapse all</HuiButton>
<span
>Basic Information: {{ baseExpanded ? 'Expanded' : 'Collapsed' }}, Advanced Settings:
{{ advancedExpanded ? 'Expanded' : 'Collapsed' }}</span
>
</HuiSpace>
<HuiForm label-width="80px" style="margin-top: 16px">
<HuiFormSection v-model:expanded="baseExpanded" title="Basic Information" collapsible>
<HuiFormItem label="Name">
<HuiInput />
</HuiFormItem>
</HuiFormSection>
<HuiFormSection v-model:expanded="advancedExpanded" title="Advanced Settings" collapsible>
<HuiFormItem label="Code">
<HuiInput />
</HuiFormItem>
<HuiFormItem label="Sort order">
<HuiInput />
</HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
Use collapse-text and expand-text to change this section's toggle labels. See Internationalization to change the application's language:
<template>
<HuiFormSection title="Advanced" collapsible collapse-text="Collapse" expand-text="Expand">
...
</HuiFormSection>
</template>
A directly runnable version:
<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 and Extra Actions
- The
titleslot replaces the title text and can include a required marker. - The
tipslot appears after the title in smaller, secondary-colored text. Use it for an explanation or help icon. - The
extraslot appears at the right of the heading, before the collapse toggle. Use it for actions such as Add.
<script setup lang="ts">
import { HuiFormSection, HuiButton, HuiIcon, HuiTooltip } from '@nscode/handy-ui'
</script>
<template>
<HuiFormSection title="Contact" collapsible>
<template #tip>Provide at least one contact</template>
<template #extra>
<HuiButton size="small" type="primary" icon="plus">Add</HuiButton>
</template>
...
</HuiFormSection>
<HuiFormSection>
<template #title
>Billing Information <span style="color: var(--hui-color-danger)">*</span></template
>
<template #tip>
<HuiTooltip content="The financial system is the source of truth for invoicing details">
<HuiIcon name="help" />
</HuiTooltip>
</template>
...
</HuiFormSection>
</template>
This complete form example adds a contact with Add and allows removing a row when more than one contact exists. Deletion first confirms the removal of that contact's name and phone number. Cancel, closing and Esc preserve the data. Changes to the list, contact content or read-only state, or unmounting the example, invalidate an outstanding confirmation. The Billing Information heading uses a slot to add a red required marker, and its tip is an icon with a tooltip.
<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(
`Delete contact ${index + 1} (${contact.name || 'Unnamed'})? This removes this contact's name and phone number. Other contacts and billing information are preserved.`,
'Delete contact',
{
type: 'warning',
confirmButtonType: 'danger',
confirmButtonText: 'Delete',
cancelButtonText: 'Cancel'
}
)
if (
!active ||
readonly.value ||
revision !== requestedRevision ||
form.contacts[index] !== contact ||
form.contacts.length <= 1
)
return
form.contacts.splice(index, 1)
} catch {
// Preserve contacts and billing information on cancellation, closing or Esc.
} finally {
removing.value = false
}
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCheckbox v-model="readonly">Read only</HuiCheckbox>
<HuiForm :model="form" :readonly="readonly" label-width="80px">
<HuiFormSection title="Contact" collapsible>
<template #tip>Provide at least one contact</template>
<template #extra>
<HuiButton
size="small"
type="primary"
icon="plus"
:disabled="readonly"
@click="addContact"
>Add</HuiButton
>
</template>
<div v-for="(contact, index) in form.contacts" :key="contact.id" class="contact-row">
<HuiFormItem :label="`Contact ${index + 1}`" :prop="`contacts.${index}.name`">
<HuiInput v-model="contact.name" placeholder="Name" />
</HuiFormItem>
<HuiFormItem label="Phone" :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="Delete this contact"
:disabled="readonly || removing"
@click="removeContact(contact)"
/>
</div>
</HuiFormSection>
<HuiFormSection>
<template #title
>Billing Information <span style="color: var(--hui-color-danger)">*</span></template
>
<template #tip>
<HuiTooltip content="The financial system is the source of truth for invoicing details">
<HuiIcon name="help" />
</HuiTooltip>
</template>
<HuiFormItem label="Tax ID" 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>
Sizes
size accepts large, default or small:
| Size | Title | Tip and Toggle Text | Arrow | Minimum Heading Height | Heading-to-Content and Section Gap |
|---|---|---|---|---|---|
| large | 18px | 16px | 18px | 40px | 20px |
| default | 16px | 14px | 16px | 32px | 16px |
| small | 14px | 12px | 14px | 24px | 12px |
The minimum heading height matches a button of the same size. A matching button in extra does not make the heading taller. When omitted, size follows the enclosing HuiForm, then HuiConfigProvider, and finally default.
These three sections set size explicitly to compare the title, tip, toggle and matching action button:
<template>
<HuiForm label-width="80px">
<HuiFormSection title="Large (large)" size="large" collapsible>
<template #tip>Help text</template>
<template #extra>
<HuiButton size="large" icon="plus">Add</HuiButton>
</template>
<HuiFormItem label="Name" size="large"><HuiInput /></HuiFormItem>
</HuiFormSection>
<HuiFormSection title="Default (default)" size="default" collapsible>
<template #tip>Help text</template>
<template #extra>
<HuiButton icon="plus">Add</HuiButton>
</template>
<HuiFormItem label="Name"><HuiInput /></HuiFormItem>
</HuiFormSection>
<HuiFormSection title="Small (small)" size="small" collapsible>
<template #tip>Help text</template>
<template #extra>
<HuiButton size="small" icon="plus">Add</HuiButton>
</template>
<HuiFormItem label="Name" size="small"><HuiInput /></HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
A section without size follows the form. For example, sections inside <HuiForm size="large"> are large. Changing the form's size updates sections, form items and controls together:
<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 (large)', value: 'large' },
{ label: 'Default (default)', value: 'default' },
{ label: 'Small (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="Basic Information" collapsible>
<template #tip>Follows the form's size</template>
<HuiFormItem label="Name"><HuiInput /></HuiFormItem>
</HuiFormSection>
<HuiFormSection title="Contact Details">
<HuiFormItem label="Mobile"><HuiInput /></HuiFormItem>
</HuiFormSection>
</HuiForm>
</template>
Grid Layout Within a Section
When the form sets columns or item-min-width, a section automatically spans a full row and its content inherits the form's column settings. Set span on each HuiFormItem directly. See Form's section column inheritance for a complete example. The HuiRow and HuiCol approach below remains available for manual layout.
The section's content is an ordinary container, so it can contain HuiRow and HuiCol for multiple columns.
<template>
<HuiForm label-width="80px">
<HuiFormSection title="Basic Information">
<HuiRow :gutter="24">
<HuiCol :span="12"
><HuiFormItem label="Name"><HuiInput /></HuiFormItem
></HuiCol>
<HuiCol :span="12"
><HuiFormItem label="Code"><HuiInput /></HuiFormItem
></HuiCol>
</HuiRow>
</HuiFormSection>
</HuiForm>
</template>
Expanding a Section After Validation Fails
Fields in a collapsed section still participate in validation, but their error messages are hidden. After validate(), determine which section contains an invalid field and set that section's expanded back to true. Advanced Settings starts collapsed below, and its Code field is required. Click Save without filling it in:
<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: 'Enter a name' }],
code: [{ required: true, message: 'Enter a code' }]
}
// Fields in the Advanced Settings section
const advancedFields = ['code']
const save = async () => {
const { valid, errors } = await formRef.value!.validate()
if (valid) {
HuiMessage.success('Saved successfully')
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="Basic Information">
<HuiFormItem label="Name" prop="name">
<HuiInput v-model="form.name" />
</HuiFormItem>
</HuiFormSection>
<HuiFormSection v-model:expanded="advancedExpanded" title="Advanced Settings" collapsible>
<HuiFormItem label="Code" prop="code">
<HuiInput v-model="form.code" />
</HuiFormItem>
</HuiFormSection>
<HuiFormItem>
<HuiButton type="primary" @click="save">Save</HuiButton>
</HuiFormItem>
</HuiForm>
</template>
Ancestor Variable Overrides
Public variables can be set on the section itself or any ancestor to override font sizes, minimum heading height and spacing. Size classes set only private defaults. Nested sections inherit public settings while resetting their own size defaults. The first section still has no outer top spacing, and a section without a heading adds no extra heading-to-content gap.
| Variable | Default | Description |
|---|---|---|
--hui-form-section-title-font-size | 18/16/14px for large/default/small | Title font size |
--hui-form-section-text-font-size | 16/14/12px for large/default/small | Tip and toggle font size |
--hui-form-section-header-height | Control height for the same size | Minimum heading height |
--hui-form-section-gap | 20/16/12px for large/default/small | Outer section spacing and heading-to-content gap |
The old --hui-form-section-title-size and --hui-form-section-text-size are renamed to the -font-size variables above, without fallback aliases.
<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="Large defaults" size="large" collapsible>
<template #tip>Help text</template>
<span>Section content is preserved; collapse and expand to view it again.</span>
</HuiFormSection>
<HuiFormSection title="Small defaults" size="small" collapsible>
<template #tip>Help text</template>
<span
>Public variables override font sizes, height and spacing; private defaults remain
independent for each size.</span
>
</HuiFormSection>
</div>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Section title; also available through the title slot |
| collapsible | boolean | false | Whether the section can collapse. When false, content is always visible and no toggle is shown |
| expanded | boolean | undefined | Expansion state, supporting v-model:expanded; effective only when collapsible. Internal state starts true when omitted. Providing it makes it controlled, with undefined displayed as true |
| collapseText | string | Localized Collapse | Toggle label while expanded |
| expandText | string | Localized Expand | Toggle label while collapsed |
| size | 'large' | 'default' | 'small' | - | Follows the enclosing HuiForm, then HuiConfigProvider, and finally 'default' when omitted |
Events
| Event | Callback Arguments | Description |
|---|---|---|
| update:expanded | (expanded: boolean) | Emitted when the collapse toggle requests a state change. Used by v-model:expanded |
Slots
| Slot | Scope Arguments | Description |
|---|---|---|
| default | - | Section content, usually HuiFormItem |
| title | - | Custom heading, replacing the title prop |
| tip | - | Content after the title, smaller and secondary-colored |
| extra | - | Right-side heading content before the collapse toggle |
Methods
No methods are exposed. Use v-model:expanded to control expansion.
Types
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]
}