Collapse
Controls expansion across collapse items, with accordion mode and asynchronous guards.
Import
Use <hui-collapse> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<template>
<view>
<hui-space direction="vertical" fill>
<hui-collapse
v-model="opened"
custom-class="hui-space__item"
:before-change="beforeChange"
@change="onChange"
>
<hui-collapse-item name="details" title="详情 / Details"
>可展开的正文 / Expandable content</hui-collapse-item
>
<hui-collapse-item name="locked" title="受保护 / Guarded"
>不可展开 / Blocked</hui-collapse-item
>
<hui-collapse-item name="disabled" title="禁用 / Disabled" disabled />
</hui-collapse>
<hui-collapse custom-class="hui-space__item" accordion
><hui-collapse-item name="one" title="单开一 / One">甲 / A</hui-collapse-item
><hui-collapse-item name="two" title="单开二 / Two">乙 / B</hui-collapse-item></hui-collapse
>
<text class="hui-space__item">{{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { CollapseModelValue, CollapseChangeContext } from '@nscode/handy-uni'
const lastEvent = ref('尚无事件 / No event')
const opened = ref<(string | number)[]>([])
const onChange = (value: CollapseModelValue, context: CollapseChangeContext) => {
lastEvent.value = JSON.stringify({ value, context })
}
const beforeChange = async (name: string | number) => name !== 'locked'
</script>
Props
| Prop | Type | Default | Details |
|---|---|---|---|
modelValue | CollapseModelValue | — | Controlled current value; accept update:modelValue to change it. |
defaultValue | CollapseModelValue | [] | Initial uncontrolled value. |
accordion | boolean | false | Allows a single expanded collapse item. |
border | boolean | true | Displays separators. |
duration | number | 300 | Animation or display duration in milliseconds; see platform notes for zero. |
beforeChange | (name: CollapseName, expanded: boolean) => boolean | void | Promise<boolean | void> | — | Asynchronous state-change guard; false or rejection cancels. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
update:modelValue | (value: CollapseModelValue) |
change | (value: CollapseModelValue, context: CollapseChangeContext) |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
default | — |
Methods
| Method | Signature | Description |
|---|---|---|
toggleAll | (expanded?: boolean): void | Uses current props. |
Types
CollapseName、CollapseModelValue、CollapseChangeContext、CollapseProps、CollapseEmits、CollapseExpose.
Platform notes
Supports H5, WeChat mini-program, App-vue and ordinary Vue 3 Web through shared platform components. Unit tests do not replace device acceptance; verify touch, screen readers, deactivation and native rendering on each target.
Device smoke checklist.