ActionBar 批量操作欄
選擇記錄後顯示操作入口與已選數量。fixed 固定在視口底部,sticky 留在所在容器。工具列出現時不移動焦點、不鎖定捲動;取消選擇由業務響應 clear 完成。
引入
import { HuiActionBar } from '@nscode/handy-ui'
import type {
ActionBarProps,
ActionBarPosition,
ActionBarEmits,
ActionBarInfoSlotProps,
ActionBarSlots
} from '@nscode/handy-ui'
示例
表格勾選與視口底部操作欄
勾選表格,或點擊“選擇兩項”顯示工具列。Tab 移入工具列後按 Esc,與點擊“取消選擇”一樣觸發 clear;頁面記錄事件次數,並顯示訊息。批量審批更新狀態,元件本身不修改記錄或選擇。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiActionBar, HuiButton, HuiMessage, HuiSpace, HuiTable } from '@nscode/handy-ui'
const rows = ref([
{ id: 1, name: '需求评审', status: '待审批' },
{ id: 2, name: '设计确认', status: '待审批' },
{ id: 3, name: '测试验收', status: '待审批' }
])
const columns = [
{ prop: 'name', label: '任務' },
{ prop: 'status', label: '狀態' }
]
const keys = ref<Array<string | number>>([])
const clears = ref(0)
const result = ref('勾選記錄後,在視口底部操作')
const clear = () => {
keys.value = []
clears.value++
result.value = `收到 clear 事件 ${clears.value} 次,已取消選擇`
HuiMessage.info(result.value)
}
const approve = () => {
const count = keys.value.length
rows.value = rows.value.map((row) =>
keys.value.includes(row.id) ? { ...row, status: '已审批' } : row
)
keys.value = []
result.value = `已審批 ${count} 項`
HuiMessage.success(result.value)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="keys = [1, 3]">選擇兩項</HuiButton>
<HuiTable
v-model:selected-keys="keys"
:data="rows"
:columns="columns"
selectable
row-key="id"
/>
<span>{{ result }}</span>
<HuiActionBar :count="keys.length" :total="rows.length" @clear="clear">
<HuiButton type="primary" @click="approve">批量審批</HuiButton>
</HuiActionBar>
</HuiSpace>
</template>
容器內 sticky
放在捲動卡片的最後,預設 bottom: 0。捲動容器觀察工具列的位置;取消後可重新選擇。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiActionBar, HuiButton, HuiSpace } from '@nscode/handy-ui'
const count = ref(3)
const result = ref('捲動卡片,工具列留在容器底部')
const clear = () => {
count.value = 0
result.value = '收到 clear,已取消選擇'
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="count = 3">重新選擇三項</HuiButton>
<div class="scroll-card">
<p v-for="index in 14" :key="index">記錄 {{ index }}:捲動查看卡片內容。</p>
<HuiActionBar position="sticky" :count="count" :total="14" @clear="clear">
<HuiButton @click="result = `已導出 ${count} 項`">導出所選</HuiButton>
</HuiActionBar>
</div>
<span>{{ result }}</span>
</HuiSpace>
</template>
<style scoped>
.scroll-card {
height: 240px;
overflow: auto;
padding: var(--hui-spacing-4);
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.scroll-card p {
min-height: 36px;
}
</style>
顯式 visible 與響應式 count
visible 未傳時由 count > 0 決定;傳入後完全按它顯示。因此 visible=true、count=0 也會顯示,visible=false 時已有選擇也會隱藏。clear 不會自行把 visible 設為 false。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiActionBar, HuiButton, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const visible = ref(true)
const count = ref(0)
const result = ref('目前顯式 visible=true,count=0')
const clear = () => {
count.value = 0
result.value = '收到 clear;visible 仍為 true,所以工具列保留'
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace>
<HuiSwitch v-model="visible" aria-label="控制操作欄顯示" />
<span>visible: {{ visible }},count: {{ count }}</span>
<HuiButton @click="count++">增加已選數量</HuiButton>
</HuiSpace>
<HuiActionBar position="sticky" :visible="visible" :count="count" @clear="clear">
<HuiButton @click="result = `處理 ${count} 項`">處理所選</HuiButton>
</HuiActionBar>
<span>{{ result }}</span>
</HuiSpace>
</template>
clearable 開關
clearable=false 同時隱藏取消按鈕、停用工具列的 Esc 取消。業務操作按鈕繼續可用。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiActionBar, HuiButton, HuiSpace } from '@nscode/handy-ui'
const flags = [true, false]
const counts = ref([2, 2])
const clears = ref([0, 0])
const clear = (index: number) => {
counts.value[index] = 0
clears.value[index]++
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="(flag, index) in flags" :key="String(flag)">
<HuiSpace>
<span>clearable={{ flag }},clear 次數:{{ clears[index] }}</span>
<HuiButton @click="counts[index] = 2">重新選擇兩項</HuiButton>
</HuiSpace>
<HuiActionBar
position="sticky"
:count="counts[index]"
:clearable="flag"
@clear="clear(index)"
>
<HuiButton @click="counts[index]++">增加已選數量</HuiButton>
</HuiActionBar>
</div>
</HuiSpace>
</template>
數字與 CSS 距離
數字和數字字串按 px;其他字串作為 CSS 長度使用。下面用 sticky 對比距離;fixed 也接受同樣的寫法。未傳 offset 時,切換 position 會重新採用對應預設值。
<script setup lang="ts">
import { HuiActionBar, HuiButton, HuiSpace } from '@nscode/handy-ui'
const offsets: Array<number | string> = [0, 16, '16', '1rem', 'calc(1rem + 4px)']
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="(offset, index) in offsets" :key="index" class="offset-stage">
<p>offset: {{ JSON.stringify(offset) }}</p>
<div class="filler">向下捲動查看距離</div>
<HuiActionBar position="sticky" :count="1" :offset="offset" :clearable="false">
<HuiButton>Tab 可聚焦的操作</HuiButton>
</HuiActionBar>
</div>
</HuiSpace>
</template>
<style scoped>
.offset-stage {
height: 150px;
overflow: auto;
padding: var(--hui-spacing-3);
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
}
.filler {
height: 160px;
color: var(--hui-text-color-secondary);
}
</style>
自訂 info 與操作區
info 接收目前 count、total,替換預設數量文案。預設插槽放業務按鈕,工具列不會替這些按鈕執行批量操作。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiActionBar, HuiButton, HuiSpace, HuiTag } from '@nscode/handy-ui'
const count = ref(2)
const result = ref('等待處理')
const clear = () => {
count.value = 0
result.value = '收到 clear'
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="count = 2">重新選擇兩項</HuiButton>
<HuiActionBar position="sticky" :count="count" :total="8" @clear="clear">
<template #info="{ count: selected, total }">
<HuiTag type="warning">待處理 {{ selected }} / {{ total }} 項</HuiTag>
</template>
<HuiButton type="primary" @click="result = `已安排處理 ${count} 項`">安排處理</HuiButton>
<HuiButton @click="result = `已導出 ${count} 項`">導出</HuiButton>
</HuiActionBar>
<span>{{ result }}</span>
</HuiSpace>
</template>
Esc 先關閉內部彈層
打開選擇面板或氣泡,再按 Esc,先關閉內部面板。焦點留在普通業務按鈕時按 Esc 才請求取消選擇。工具列外的 Esc 不影響選擇;輸入法組合期間也不會取消。
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiActionBar,
HuiButton,
HuiMessage,
HuiPopover,
HuiSelect,
HuiSpace
} from '@nscode/handy-ui'
const count = ref(2)
const mode = ref('review')
const result = ref('打開面板後按 Esc,選擇應保留')
const clear = () => {
count.value = 0
result.value = '工具列收到 clear,已取消選擇'
HuiMessage.info(result.value)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="count = 2">重新選擇兩項</HuiButton>
<HuiActionBar position="sticky" :count="count" @clear="clear">
<HuiSelect
v-model="mode"
:options="[
{ value: 'review', label: '審核' },
{ value: 'export', label: '匯出' }
]"
style="width: 140px"
/>
<HuiPopover trigger="click" content="氣泡打開時,Esc 僅關閉氣泡">
<template #reference><HuiButton>打開說明</HuiButton></template>
</HuiPopover>
<HuiButton @click="result = '焦點在普通按鈕上,可以按 Esc 取消選擇'">普通操作</HuiButton>
</HuiActionBar>
<span>{{ result }},目前方式:{{ mode }}</span>
</HuiSpace>
</template>
語言、局部樣式與 total=0
內建文案跟隨 ConfigProvider 的語言。sticky 繼承所在區域的主題變數;fixed Teleport 到 body,繼承全域主題。局部祖先的 CSS 變數不會跨 Teleport,可透過工具列自身的 class/style 指定。
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiActionBar,
HuiButton,
HuiConfigProvider,
HuiSpace,
localeEn,
localeZhCn
} from '@nscode/handy-ui'
const english = ref(false)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="english = !english">切換中英文</HuiButton>
<HuiConfigProvider :locale="english ? localeEn : localeZhCn">
<HuiActionBar
position="sticky"
visible
:count="0"
:total="0"
:clearable="false"
style="--hui-bg-color: var(--hui-fill-color-light)"
>
<HuiButton @click="english = !english">{{
english ? 'Change language' : '切換語言'
}}</HuiButton>
</HuiActionBar>
</HuiConfigProvider>
</HuiSpace>
</template>
clear 方法與請求邊界
透過 ActionBarExpose 型別的 ref 呼叫 clear(),與內建取消按鈕共用請求入口。實際不可見、clearable=false 或卸載後呼叫無效;顯式 visible=true、count=0 時仍可請求。同步監聽器再次呼叫 clear() 不會重複觸發,返回後的獨立呼叫仍各發出一次事件。本例只統計請求,不修改選擇資料。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiActionBar, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { ActionBarExpose } from '@nscode/handy-ui'
const actionBarRef = ref<ActionBarExpose>()
const count = ref(2)
const visible = ref(true)
const clearable = ref(true)
const requests = ref(0)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @click="actionBarRef?.clear()">呼叫 clear()</HuiButton>
<HuiButton @click="visible = !visible">visible={{ visible }}</HuiButton>
<HuiButton @click="clearable = !clearable">clearable={{ clearable }}</HuiButton>
<HuiButton @click="count = count ? 0 : 2">count={{ count }}</HuiButton>
</HuiSpace>
<HuiActionBar
ref="actionBarRef"
position="sticky"
:visible="visible"
:count="count"
:clearable="clearable"
@clear="requests++"
>
<span>本例只統計請求,不修改選擇資料</span>
</HuiActionBar>
<p role="status">clear 請求 {{ requests }} 次;count={{ count }},visible={{ visible }}</p>
</HuiSpace>
</template>
Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| count | 已選數量;未顯式傳 visible 時,大於 0 顯示 | number | 0 |
| total | 總數;傳入時顯示已選數與總數,0 有效 | number | — |
| visible | 顯式控制顯示,優先於 count;不傳時自動計算 | boolean | — |
| position | 視口底部固定,或容器內 sticky | ActionBarPosition | 'fixed' |
| offset | 距底部距離;數字和數字字串按 px,支援 CSS 長度 | number | string | fixed 為 24,sticky 為 0 |
| clearable | 顯示取消選擇按鈕,允許工具列內 Esc 取消 | boolean | true |
Events
| 事件 | 參數 | 說明 |
|---|---|---|
| clear | 無 | 呼叫 clear()、點擊取消按鈕,或工具列內未被巢狀控制項處理的 Esc;業務自行更新選擇 |
Slots
| 插槽 | 參數 | 說明 |
|---|---|---|
| default | 無 | 批量操作按鈕 |
| info | { count: number, total: number | undefined } | 替換可見數量文案;獨立播報區仍按語言包播報數量及總數 |
方法
| 方法 | 參數 | 回傳值 | 說明 |
|---|---|---|---|
| clear | 無 | void | 請求取消選擇,與取消按鈕一致;實際隱藏、clearable=false 或卸載後無效 |
此方法只發出無參數的 clear 事件,不修改 count、visible 或業務資料,也不主動移動焦點。顯式 visible=true 時不因 count=0 攔截;未傳 visible 且 count<=0 時,因工具列隱藏而不發出請求。
類型
type ActionBarPosition = 'fixed' | 'sticky'
interface ActionBarProps {
count?: number
total?: number
visible?: boolean
position?: ActionBarPosition
offset?: number | string
clearable?: boolean
}
interface ActionBarEmits {
clear: []
}
interface ActionBarExpose {
clear: () => void
}
interface ActionBarInfoSlotProps {
count: number
total: number | undefined
}
interface ActionBarSlots {
default?: () => unknown
info?: (props: ActionBarInfoSlotProps) => unknown
}