Vant4 ActionSheet 動(dòng)作面板

2023-02-16 17:55 更新

介紹

底部彈起的模態(tài)面板,包含與當(dāng)前情境相關(guān)的多個(gè)選項(xiàng)。

引入

通過以下方式來(lái)全局注冊(cè)組件,更多注冊(cè)方式請(qǐng)參考組件注冊(cè)

import { createApp } from 'vue';
import { ActionSheet } from 'vant';

const app = createApp();
app.use(ActionSheet);

代碼演示

基礎(chǔ)用法

動(dòng)作面板通過 ?actions? 屬性來(lái)定義選項(xiàng),?actions? 屬性是一個(gè)由對(duì)象構(gòu)成的數(shù)組,數(shù)組中的每個(gè)對(duì)象配置一列,對(duì)象格式見文檔下方表格。

<van-cell is-link title="基礎(chǔ)用法" @click="show = true" />
<van-action-sheet v-model:show="show" :actions="actions" @select="onSelect" />
import { ref } from 'vue';
import { showToast } from 'vant';

export default {
  setup() {
    const show = ref(false);
    const actions = [
      { name: '選項(xiàng)一' },
      { name: '選項(xiàng)二' },
      { name: '選項(xiàng)三' },
    ];
    const onSelect = (item) => {
      // 默認(rèn)情況下點(diǎn)擊選項(xiàng)時(shí)不會(huì)自動(dòng)收起
      // 可以通過 close-on-click-action 屬性開啟自動(dòng)收起
      show.value = false;
      showToast(item.name);
    };

    return {
      show,
      actions,
      onSelect,
    };
  },
};

展示取消按鈕

設(shè)置 ?cancel-text? 屬性后,會(huì)在底部展示取消按鈕,點(diǎn)擊后關(guān)閉當(dāng)前面板并觸發(fā) ?cancel? 事件。

<van-action-sheet
  v-model:show="show"
  :actions="actions"
  cancel-text="取消"
  close-on-click-action
  @cancel="onCancel"
/>
import { ref } from 'vue';
import { showToast } from 'vant';

export default {
  setup() {
    const show = ref(false);
    const actions = [
      { name: '選項(xiàng)一' },
      { name: '選項(xiàng)二' },
      { name: '選項(xiàng)三' },
    ];
    const onCancel = () => showToast('取消');

    return {
      show,
      actions,
      onCancel,
    };
  },
};

展示描述信息

通過 ?description? 可以在菜單頂部顯示描述信息,通過選項(xiàng)的 ?subname? 屬性可以在選項(xiàng)文字的右側(cè)展示描述信息。

<van-action-sheet
  v-model:show="show"
  :actions="actions"
  cancel-text="取消"
  description="這是一段描述信息"
  close-on-click-action
/>
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);
    const actions = [
      { name: '選項(xiàng)一' },
      { name: '選項(xiàng)二' },
      { name: '選項(xiàng)三', subname: '描述信息' },
    ];

    return {
      show,
      actions,
    };
  },
};

選項(xiàng)狀態(tài)

可以通過 ?loading? 和 ?disabled? 將選項(xiàng)設(shè)置為加載狀態(tài)或禁用狀態(tài),或者通過?color?設(shè)置選項(xiàng)的顏色

<van-action-sheet
  v-model:show="show"
  :actions="actions"
  cancel-text="取消"
  close-on-click-action
/>
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);
    const actions = [
      { name: '著色選項(xiàng)', color: '#ee0a24' },
      { name: '禁用選項(xiàng)', disabled: true },
      { name: '加載選項(xiàng)', loading: true },
    ];

    return {
      show,
      actions,
    };
  },
};

自定義面板

通過插槽可以自定義面板的展示內(nèi)容,同時(shí)可以使用?title?屬性展示標(biāo)題欄

<van-action-sheet v-model:show="show" title="標(biāo)題">
  <div class="content">內(nèi)容</div>
</van-action-sheet>

<style>
  .content {
    padding: 16px 16px 160px;
  }
</style>

API

Props

參數(shù) 說明 類型 默認(rèn)值
v-model:show 是否顯示動(dòng)作面板 boolean false
actions 面板選項(xiàng)列表 ActionSheetAction[] []
title 頂部標(biāo)題 string -
cancel-text 取消按鈕文字 string -
description 選項(xiàng)上方的描述信息 string -
closeable 是否顯示關(guān)閉圖標(biāo) boolean true
close-icon 關(guān)閉圖標(biāo)名稱或圖片鏈接,等同于 Icon 組件的 name 屬性 string cross
duration 動(dòng)畫時(shí)長(zhǎng),單位秒,設(shè)置為 0 可以禁用動(dòng)畫 number | string 0.3
z-index 將面板的 z-index 層級(jí)設(shè)置為一個(gè)固定值 number | string 2000+
round 是否顯示圓角 boolean true
overlay 是否顯示遮罩層 boolean true
overlay-class 自定義遮罩層類名 string | Array | object -
overlay-style 自定義遮罩層樣式 object -
lock-scroll 是否鎖定背景滾動(dòng) boolean true
lazy-render 是否在顯示彈層時(shí)才渲染節(jié)點(diǎn) boolean true
close-on-popstate 是否在頁(yè)面回退時(shí)自動(dòng)關(guān)閉 boolean true
close-on-click-action 是否在點(diǎn)擊選項(xiàng)后關(guān)閉 boolean false
close-on-click-overlay 是否在點(diǎn)擊遮罩層后關(guān)閉 boolean true
safe-area-inset-bottom 是否開啟底部安全區(qū)適配 boolean true
teleport 指定掛載的節(jié)點(diǎn),等同于 Teleport 組件的 to 屬性 string | Element -
before-close v3.1.4 關(guān)閉前的回調(diào)函數(shù),返回 false 可阻止關(guān)閉,支持返回 Promise (action: string) => boolean | Promise<boolean> -

Action 數(shù)據(jù)結(jié)構(gòu)

?actions? 屬性是一個(gè)由對(duì)象構(gòu)成的數(shù)組,數(shù)組中的每個(gè)對(duì)象配置一列,對(duì)象可以包含以下值:

鍵名 說明 類型
name 標(biāo)題 string
subname 二級(jí)標(biāo)題 string
color 選項(xiàng)文字顏色 string
className 為對(duì)應(yīng)列添加額外的 class string | Array | object
loading 是否為加載狀態(tài) boolean
disabled 是否為禁用狀態(tài) boolean
callback 點(diǎn)擊時(shí)觸發(fā)的回調(diào)函數(shù) action: ActionSheetAction

Events

事件名 說明 回調(diào)參數(shù)
select 點(diǎn)擊選項(xiàng)時(shí)觸發(fā),禁用或加載狀態(tài)下不會(huì)觸發(fā) action: ActionSheetAction, index: number
cancel 點(diǎn)擊取消按鈕時(shí)觸發(fā) -
open 打開面板時(shí)觸發(fā) -
close 關(guān)閉面板時(shí)觸發(fā) -
opened 打開面板且動(dòng)畫結(jié)束后觸發(fā) -
closed 關(guān)閉面板且動(dòng)畫結(jié)束后觸發(fā) -
click-overlay 點(diǎn)擊遮罩層時(shí)觸發(fā) event: MouseEvent

Slots

名稱 說明 參數(shù)
default 自定義面板的展示內(nèi)容 -
description 自定義描述文案 -
cancel v3.0.10 自定義取消按鈕內(nèi)容 -
action v3.4.0 自定義選項(xiàng)內(nèi)容 { action: ActionSheetAction, index: number }

類型定義

組件導(dǎo)出以下類型定義:

import type { ActionSheetProps, ActionSheetAction } from 'vant';

主題定制

樣式變量

組件提供了下列 CSS 變量,可用于自定義樣式,使用方法請(qǐng)參考 ConfigProvider 組件。

名稱 默認(rèn)值 描述
--van-action-sheet-max-height 80% -
--van-action-sheet-header-height 48px -
--van-action-sheet-header-font-size var(--van-font-size-lg) -
--van-action-sheet-description-color var(--van-text-color-2) -
--van-action-sheet-description-font-size var(--van-font-size-md) -
--van-action-sheet-description-line-height var(--van-line-height-md) -
--van-action-sheet-item-background var(--van-background-2) -
--van-action-sheet-item-font-size var(--van-font-size-lg) -
--van-action-sheet-item-line-height var(--van-line-height-lg) -
--van-action-sheet-item-text-color var(--van-text-color) -
--van-action-sheet-item-disabled-text-color var(--van-text-color-3) -
--van-action-sheet-subname-color var(--van-text-color-2) -
--van-action-sheet-subname-font-size var(--van-font-size-sm) -
--van-action-sheet-subname-line-height var(--van-line-height-sm) -
--van-action-sheet-close-icon-size 22px -
--van-action-sheet-close-icon-color var(--van-gray-5) -
--van-action-sheet-close-icon-padding 0 var(--van-padding-md) -
--van-action-sheet-cancel-text-color var(--van-gray-7) -
--van-action-sheet-cancel-padding-top var(--van-padding-xs) -
--van-action-sheet-cancel-padding-color var(--van-background) -
--van-action-sheet-loading-icon-size 22px -


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)