JXL组件:基于Vue生态编写jxl-dialog对话框

JxlDialog 对话框

在保留当前页面状态的情况下,告知用户并承载相关操作。

基于 Element UI 的 el-dialog 组件封装。

<template>
  <el-dialog
    :visible="visible"
    :width="width"
    :top="top"
    :center="center"
    :modal="modal"
    :fullscreen="fullscreen"
    :lock-scroll="lockScroll"
    :modal-append-to-body="modalAppendToBody"
    :append-to-body="appendToBody"
    :custom-class="customClass"
    :close-on-click-modal="closeOnClickModal"
    :close-on-press-escape="closeOnPressEscape"
    :show-close="showClose"
    :destroy-on-close="destroyOnClose"
    v-bind="$attrs"
    v-on="$listeners"
  >
    <template v-if="$scopedSlots['title'] || title" v-slot:title>
      <div class="jxl-dialog__header" :style="headerStyle" :class="headerCustomClass">
        <slot name="title">{{ title }}</slot>
      </div>
    </template>
    <div class="jxl-dialog__body" :style="bodyStyle" :class="bodyCustomClass">
      <slot />
    </div>
    <template v-if="$scopedSlots['footer'] || footer" v-slot:footer>
      <div class="jxl-dialog__footer" :style="footerStyle" :class="footerCustomClass">
        <slot name="footer">
          <el-button v-if="showCancel" @click="cancelHandle">{{ cancelText }}</el-button>
          <el-button type="primary" @click="confirmHandle">{{ confirmText }}</el-button>
        </slot>
      </div>
    </template>
  </el-dialog>
</template>

<script>
/**
 * JxlDialog 对话框
 * 在保留当前页面状态的情况下,告知用户并承载相关操作
 */
export default {
  name: 'JxlDialog',
  model: {
    prop: 'visible'
  },
  props: {
    /**
     * 是否显示 Dialog
     * 支持 .sync 修饰符
     * 可用 v-model 绑定值
     */
    visible: {
      type: Boolean,
      default: false
    },
    /**
     * Dialog 的标题
     * 也可通过具名 slot(见下表)传入
     */
    title: {
      type: String,
      default: ''
    },
    /**
     * 是否显示 Dialog 的底部
     * 可通过具名 slot(见下表)传入
     */
    footer: {
      type: [String, Boolean],
      default: true
    },
    /**
     * 显示取消的文本
     */
    showCancel: {
      type: Boolean,
      default: true
    },
    /**
     * 取消的文本
     */
    cancelText: {
      type: String,
      default: '取消'
    },
    /**
     * 确定的文本
     */
    confirmText: {
      type: String,
      default: '确定'
    },
    /**
     * Dialog 的宽度
     */
    width: {
      type: String,
      default: '50%'
    },
    /**
     * 是否为全屏 Dialog
     */
    fullscreen: {
      type: Boolean,
      default: false
    },
    /**
     * Dialog CSS 中的 margin-top 值
     */
    top: {
      type: String,
      default: '15vh'
    },
    /**
     * 是否对头部和底部采用居中布局
     */
    center: {
      type: Boolean,
      default: false
    },
    /**
     * 是否需要遮罩层
     */
    modal: {
      type: Boolean,
      default: true
    },
    /**
     * 遮罩层是否插入至 body 元素上,若为 false,则遮罩层会插入至 Dialog 的父元素上
     */
    modalAppendToBody: {
      type: Boolean,
      default: true
    },
    /**
     * Dialog 自身是否插入至 body 元素上。嵌套的 Dialog 必须指定该属性并赋值为 true
     */
    appendToBody: {
      type: Boolean,
      default: true
    },
    /**
     * 是否在 Dialog 出现时将 body 滚动锁定
     */
    lockScroll: {
      type: Boolean,
      default: true
    },
    /**
     * Dialog 的自定义类名
     */
    customClass: {
      type: String,
      default: ''
    },
    /**
     * Dialog Header 的自定义类名
     */
    headerCustomClass: {
      type: String,
      default: ''
    },
    /**
     * Dialog Body 的自定义类名
     */
    bodyCustomClass: {
      type: String,
      default: ''
    },
    /**
     * Dialog Footer 的自定义类名
     */
    footerCustomClass: {
      type: String,
      default: ''
    },
    /**
     * 是否可以通过点击 modal 关闭 Dialog
     */
    closeOnClickModal: {
      type: Boolean,
      default: true
    },
    /**
     * 是否可以通过按下 ESC 关闭 Dialog
     */
    closeOnPressEscape: {
      type: Boolean,
      default: true
    },
    /**
     * 是否显示关闭按钮
     */
    showClose: {
      type: Boolean,
      default: true
    },
    /**
     * 关闭时销毁 Dialog 中的元素
     */
    destroyOnClose: {
      type: Boolean,
      default: false
    },
    /**
     * 设置 header 的样式
     */
    headerStyle: {
      type: Object,
      default: () => {
        return {}
      }
    },
    /**
     * 设置 body 的样式
     */
    bodyStyle: {
      type: Object,
      default: () => {
        return {}
      }
    },
    /**
     * 设置 footer 的样式
     */
    footerStyle: {
      type: Object,
      default: () => {
        return {}
      }
    },
    /**
     * 关闭前的回调,会暂停 Dialog 的关闭
     */
    beforeClose: {
      type: Function,
      default: () => {}
    },
    /**
     * 设置 弹框取消的回调
     */
    cancel: {
      type: Function,
      default: () => {}
    },
    /**
     * 设置 弹框确认的回调
     */
    confirm: {
      type: Function,
      default: () => {}
    },
    /**
     * Dialog 打开的回调
     */
    open: {
      type: Function,
      default: () => {}
    },
    /**
     * Dialog 打开动画结束时的回调
     */
    opened: {
      type: Function,
      default: () => {}
    },
    /**
     * Dialog 关闭的回调
     */
    close: {
      type: Function,
      default: () => {}
    },
    /**
     * Dialog 关闭动画结束时的回调
     */
    closed: {
      type: Function,
      default: () => {}
    }
  },
  methods: {
    /**
     * 取消处理
     */
    cancelHandle() {
      this.$emit('cancel') // 取消回调
    },
    /**
     * 确定处理
     */
    confirmHandle() {
      this.$emit('confirm') // 确认回调
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-dialog {
  overflow: hidden;

  .el-dialog__header {
    padding: 0;
    color: #000000d9;
    background: transparent;
    border-bottom: none;
    border-radius: 0;

    .jxl-dialog__header {
      padding: 16px 24px;
      color: #000000d9;
      background: #fff;
      border-bottom: 1px solid rgba(0, 0, 0, .06);
      border-radius: 2px 2px 0 0;
    }
  }
  .el-dialog__body {
    padding: 0;
    background: transparent;
    box-shadow: none;

    .jxl-dialog__body {
      background-color: #fff;
      padding: 24px;
    }
  }
  .el-dialog__footer {
    padding: 0;
    background: transparent;
    border: none;

    .jxl-dialog__footer {
      text-align: right;
      padding: 10px 16px;
      background: #fff;
      border-top: 1px solid rgba(0, 0, 0, .06);
      border-radius: 0 0 2px 2px;

      .el-button {
        height: 32px;
        border-radius: 2px;
      }
      .el-button + .el-button {
        margin-left: 12px;
      }
    }
  }
  &.el-dialog--center {
    .el-dialog__header {
      .jxl-dialog__header {
        text-align: center;
      }
    }
    .el-dialog__footer {
      .jxl-dialog__footer {
        text-align: center;
      }
    }
  }
}
</style>

Dialog 属性(Attributes)

参数 说明 类型 可选值 默认值
visible 是否显示 Dialog boolean - false
title 设置标题,也可以通过 v-slot:title 传入 DOM string - -
footer 是否显示按钮操作区的内容,可以通过 v-slot:footer 传入 DOM boolean - true
custom-class Dialog 的自定义类名 string - -
header-custom-class 设置 header 的自定义类 string - -
header-style 设置 header 的样式 object - {}
body-custom-class 设置 body 的自定义类 string - -
body-style 设置 body 的样式 object - {}
footer-custom-class 设置 footer 的自定义类 string - -
footer-style 设置 footer 的样式 object - {}
show-cancel 是否显示取消的文本内容 boolean - true
cancel-text 取消的文本内容 string - 取消
confirm-text 确定的文本内容 string - 确定
width Dialog 的宽度 string - 50%
fullscreen 是否为全屏 Dialog boolean - false
top Dialog CSS 中的 margin-top 值 string - 15vh
center 是否对头部和底部采用居中布局 boolean - false
modal 是否需要遮罩层 boolean - true
modal-append-to-body 遮罩层是否插入至 body 元素上,若为 false,则遮罩层会插入至 Dialog 的父元素上 boolean - true
append-to-body Dialog 自身是否插入至 body 元素上。嵌套的 Dialog 必须指定该属性并赋值为 true boolean - true
lock-scroll 是否在 Dialog 出现时将 body 滚动锁定 boolean - true
close-on-click-modal 是否可以通过点击 modal 关闭 Dialog boolean - true
close-on-press-escape 是否可以通过按下 ESC 关闭 Dialog boolean - true
destroy-on-close 关闭时销毁 Dialog 中的元素 boolean - false

Dialog 插槽(Slots)

参数 说明
- 设置 Dialog 主体区的内容
title 设置 Dialog 标题区的内容
footer 设置 Dialog 按钮操作区的内容

Dialog 事件(Events)

事件名称 说明 回调参数
cancel 用户点击取消后触发 -
confirm 用户点击确定后触发 -
before-close 关闭前的回调,会暂停 Dialog 的关闭 -
open Dialog 打开的回调 -
opened Dialog 打开动画结束时的回调 -
close Dialog 关闭的回调 -
closed Dialog 关闭动画结束时的回调 -
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容