Skip to content

抽屉 Drawer

触发命令后,从屏幕一侧滑出的抽屉式的面板。

基本用法

点击触发按钮抽屉从右侧滑出,点击遮罩区关闭。


代码事例
vue
<template>
  <fk-button type="primary" @click="handleClick">Open Drawer</fk-button>
  <fk-drawer :width="340" :visible="visible" unmount-on-close @ok="handleOk" @cancel="handleCancel">
    <template #title>
      Title
    </template>
    <div>You can customize modal body text by the current situation. This modal will be closed immediately once you
      press the OK button.
    </div>
  </fk-drawer>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      handleClick,
      handleOk,
      handleCancel
    }
  },
};
</script>

抽屉位置

自定义位置,点击触发按钮抽屉从相应的位置滑出。


代码事例
vue
<template>
  <fk-radio-group v-model="position">
    <fk-radio value="top">Top</fk-radio>
    <fk-radio value="right">Right</fk-radio>
    <fk-radio value="bottom">Bottom</fk-radio>
    <fk-radio value="left">Left</fk-radio>
  </fk-radio-group>
  <div :style="{marginTop: '20px'}">
    <fk-button type="primary" @click="handleClick">Open Drawer</fk-button>
  </div>
  <fk-drawer
    :width="340"
    :height="340"
    :visible="visible"
    :placement="position"
    unmount-on-close
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <template #title>
      Title
    </template>
    <div>You can customize modal body text by the current situation. This modal will be closed immediately once you press the OK button.</div>
  </fk-drawer>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);
    const position = ref('right');

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      position,
      handleClick,
      handleOk,
      handleCancel
    }
  },
};
</script>

自定义节点

通过插槽自定义内容,或者设置相应属性来控制显示或隐藏。


代码事例
vue
<template>
  <fk-checkbox-group v-model="custom" :options="['hide header', 'hide footer', 'hide cancel']"/>
  <div :style="{marginTop: '20px'}">
    <fk-button type="primary" @click="handleClick">Open Drawer</fk-button>
  </div>
  <fk-drawer
    :width="340"
    :header="!custom.includes('hide header')"
    :footer="!custom.includes('hide footer')"
    :hide-cancel="custom.includes('hide cancel')"
    :visible="visible"
    unmount-on-close
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <template #header>
      <span>Header and title</span>
    </template>
    <div>
      You can customize modal body text by the current situation. This modal will be closed immediately once you
      press the OK button.
    </div>
  </fk-drawer>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);
    const custom = ref([])

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      custom,
      visible,
      handleClick,
      handleOk,
      handleCancel
    }
  },
};
</script>

嵌套抽屉

在抽屉内打开新的抽屉。


代码事例
vue
<template>
  <fk-button type="primary" @click="handleClick">Open Drawer</fk-button>
  <fk-drawer :visible="visible" :width="500" unmount-on-close @ok="handleOk" @cancel="handleCancel">
    <template #title>
      Title
    </template>
    <div :style="{marginBottom: '20px'}">You can customize modal body text by the current situation. This modal will be closed immediately once you press the OK button.</div>
    <fk-button type="primary" @click="handleNestedClick">Open Nested Drawer</fk-button>
  </fk-drawer>
  <fk-drawer :visible="nestedVisible" unmount-on-close @ok="handleNestedOk" @cancel="handleNestedCancel">
    <template #title>
      Title
    </template>
    <div>You can customize modal body text by the current situation. This modal will be closed immediately once you press the OK button.</div>
  </fk-drawer>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);
    const nestedVisible = ref(false);

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }
    const handleNestedClick = () => {
      nestedVisible.value = true;
    };
    const handleNestedOk = () => {
      nestedVisible.value = false;
    };
    const handleNestedCancel = () => {
      nestedVisible.value = false;
    }

    return {
      visible,
      nestedVisible,
      handleClick,
      handleOk,
      handleCancel,
      handleNestedClick,
      handleNestedOk,
      handleNestedCancel
    }
  },
};
</script>

挂载位置

通过 popup-container 可以设置弹出层节点的挂载位置


代码事例
vue
<template>
  <div>
    <div
      id="parentNode"
      style="width: 100%; height: 300px; background-color: var(--color-fill-2); position: relative; overflow: hidden; line-height: 300px; text-align: center;"
    >
      <fk-button type="primary" @click="handleClick">Open Drawer</fk-button>
    </div>
  </div>
  <fk-drawer
    popup-container="#parentNode"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <template #title> Title </template>
    <div
      >You can customize modal body text by the current situation. This modal
      will be closed immediately once you press the OK button.</div
    >
  </fk-drawer>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const visible = ref(false);

    const handleClick = () => {
      visible.value = true;
    };
    const handleOk = () => {
      visible.value = false;
    };
    const handleCancel = () => {
      visible.value = false;
    }

    return {
      visible,
      handleClick,
      handleOk,
      handleCancel
    }
  },
};
</script>

函数调用

通过函数的方式使用抽屉。


代码事例
vue
<template>
  <fk-button type="primary" @click="handleClick">Open Drawer</fk-button>
</template>

<script>
import { Drawer } from '@erp/common';

export default {
  setup() {
    const handleClick = () => {
      Drawer.open({
        title: 'Info Title',
        content: 'This is an info message',
        width: 340
      });
    };

    return {
      handleClick,
    }
  },
}
</script>

API

<drawer> Props

参数名描述类型默认值版本
visible (v-model)抽屉是否可见booleanfalse
default-visible抽屉默认是否可见(非受控模式)booleanfalse
placement抽屉放置的位置'top' | 'right' | 'bottom' | 'left''right'
title标题string-
mask是否显示遮罩层booleantrue
mask-closable点击遮罩层是否可以关闭booleantrue
closable是否展示关闭按钮booleantrue
ok-text确认按钮的内容string-
cancel-text取消按钮的内容string-
ok-loading确认按钮是否为加载中状态booleanfalse
ok-button-props确认按钮的PropsButtonProps-1.0.0
cancel-button-props取消按钮的PropsButtonProps-1.0.0
unmount-on-close关闭时是否卸载节点booleanfalse1.0.0
width抽屉的宽度(仅在placement为right,left时可用)number|string250
height抽屉的高度(仅在placement为top,bottom时可用)number|string250
popup-container弹出框的挂载容器string | HTMLElement'body'
drawer-style抽屉的样式CSSProperties-
on-before-ok触发 ok 事件前的回调函数。如果返回 false 则不会触发后续事件,也可使用 done 进行异步关闭。(done: (closed: boolean) => void) => void | boolean | Promise<void | boolean>-
on-before-cancel触发 cancel 事件前的回调函数。如果返回 false 则不会触发后续事件。() => boolean-
esc-to-close是否支持 ESC 键关闭抽屉booleantrue1.0.0
render-to-body抽屉是否挂载在 body 元素下booleantrue
header是否展示头部内容booleantrue1.0.0
footer是否展示底部内容booleantrue1.0.0
hide-cancel是否隐藏取消按钮booleanfalse1.0.0
drawer-cls样式名称string | string[]-

<drawer> Events

事件名描述参数版本
ok点击确定按钮时触发ev: MouseEvent
cancel点击取消、关闭按钮时触发ev: MouseEvent | KeyboardEvent
open抽屉打开后(动画结束)触发-
close抽屉关闭后(动画结束)触发-
before-open对话框打开前触发-1.0.0
before-close对话框关闭前触发-1.0.0

<drawer> Slots

插槽名描述参数版本
header页眉-1.0.0
title标题-
footer页脚-

<drawer> 全局方法

Drawer 提供的全局方法,可以通过以下三种方法使用:

  1. 通过 this.$drawer 调用
  2. 在 Composition API 中,通过 getCurrentInstance().appContext.config.globalProperties.$drawer 调用
  3. 导入 Drawer,通过 Drawer 本身调用

当通过 import 方式使用时,组件没有办法获取当前的 Vue Context,如 i18n 或 route 等注入在 AppContext 上的内容无法在内部使用,需要在调用时手动传入 AppContext,或者为组件全局指定 AppContext

ts
import { createApp } from 'vue'
import { Drawer } from '@erp/common';

const app = createApp(App);
Drawer._context = app._context;

基于 MIT 许可发布