vue3 + ts 封装v-model模态框

<template>
  <div class="container">
    <transition name="modal-fade">
      <div class="modal-container" v-show="visible" :style="{ width: width }">
        <!-- 头部标题 -->
        <div class="modal-header">
          <div class="modal-title">{{ title }}</div>
          <i class="iconfont close-icon" @click="close">&#xe7fc;</i>
        </div>
        <!-- 内容区域 -->
        <div class="modal-content">
          <slot></slot>
        </div>
        <!-- 底部按钮 -->
        <div class="modal-footer" v-show="showOperation">
          <div class="modal-btn">
            <button class="cancel" ref="cancelBtn" @click="close" @mousedown="cancelMouseDown" @mouseup="cancelMouseUp">取消</button>
            <button class="ok" @click="ok">确认</button>
          </div>
        </div>
      </div>
    </transition>
    <!-- 遮罩层 -->
    <div class="mask" v-show="visible" @click="close"></div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, watch, defineEmits, defineProps } from 'vue'
const props = defineProps({
  // 模态框标题
  title: {
    type: String,
    default: () => {
      return '模态框标题'
    },
  },
  // 显示隐藏控件
  visible: {
    type: Boolean,
    default: () => {
      return false
    },
  },
  // 隐藏底部区域
  showOperation: {
    type: Boolean,
    dafault: () => {
      return true
    },
  },
  // 宽度
  width: {
    type: String,
    default: '250px',
  },
})
const emit = defineEmits(['update:visible', 'submit', 'cancel'])
// 取消
const close = function () {
  emit('cancel')
  emit('update:visible', false)
}
// 确认
const ok = function () {
  emit('submit')
  emit('update:visible', false)
}

// 获取dom
const cancelBtn = ref<any>('')
// 取消按钮 鼠标按下事件
const cancelMouseDown = function () {
  cancelBtn.value.style.color = '#096dd9'
  cancelBtn.value.style.border = '1px solid #096dd9'
}
// 取消按钮 鼠标松开事件
const cancelMouseUp = function () {
  cancelBtn.value.style.color = '#595959'
  cancelBtn.value.style.border = '1px solid #d9d9d9'
}
// watch: {
//   // 操作遮罩层的展示/隐藏
//   visible() {
//     if (this.visible == true) {
//       document.querySelector('body').setAttribute('style', 'overflow:hidden !important;')
//     } else {
//       document.querySelector('body').removeAttribute('style')
//     }
//   },
// },

watch(
  () => props.visible,
  () => {
    if (props.visible == true) {
      (document.querySelector('body') as HTMLElement).setAttribute('style', 'overflow:hidden !important;')
    } else {
      (document.querySelector('body') as HTMLElement).removeAttribute('style')
    }
  }
)
</script>

<style lang="less" scoped>
.modal-container {
  z-index: 999;
  background-color: #fff;
  min-width: 250px;
  min-height: 180px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border: none;
  border-radius: 4px;
  transition: 0.5s;
  // box-shadow: 5px 5px 5px #e4e4e4, 5px -5px 5px #e4e4e4, -5px 5px 5px #e4e4e4, -5px -5px 5px #e4e4e4;
  // 不需要知道宽高 水平垂直居中
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  // 头部标题
  .modal-header {
    width: 100%;
    height: 50px;
    border: none;
    border-bottom: 1px solid #e8e8e8;
    padding: 20px 30px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    // background-color: aquamarine;
    .modal-title {
      color: #262626;
      font-weight: 600;
      font-size: 17px;
    }
    .close-icon {
      color: #4d4d4d;
      cursor: pointer;
      width: 80px;
      text-align: right;
    }
  }
  // 内容
  .modal-content {
    width: 100%;
    min-height: 100px;
    border: none;
    border-radius: none;
    padding: 20px 30px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  // 底部按钮
  .modal-footer {
    width: 100%;
    height: 60px;
    border: none;
    border-top: 1px solid #e8e8e8;
    padding: 0 30px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    .modal-btn {
      width: 150px;
      display: flex;
      justify-content: space-between;
      .cancel {
        border: 1px solid #d9d9d9;
        background-color: #fff;
        color: #595959;
        width: 70px;
        height: 32px;
        border-radius: 4px;
        font-size: 14px;
        transition: 0.5s;
        &:hover {
          border: 1px solid #40a9ff;
          color: #40a9ff;
        }
      }
      .ok {
        border: 1px solid #1890ff;
        background-color: #1890ff;
        color: #ffffff;
        width: 70px;
        height: 32px;
        border-radius: 4px;
        font-size: 14px;
        transition: 0.5s;
        &:hover {
          border: 1px solid #40a9ff;
          background-color: #40a9ff;
        }
      }
    }
  }
}
// 遮罩层
.mask {
  z-index: 998;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  // opacity: 0.4 !important;
  // background: #000000 !important;
  background-color: rgba(0, 0, 0, 0.3);
}
// 模态框展示隐藏的动画
.modal-fade-enter-active {
  transition: all 0.3s ease;
}
.modal-fade-leave-active {
  transition: all 0.3s ease;
}
.modal-fade-enter-from,
.modal-fade-leave-to {
  transform: translateX(-400px);
  opacity: 0;
}
@font-face {
  font-family: 'iconfont'; /* Project id 3147059 */
  src: url('//at.alicdn.com/t/font_3147059_ypw5b7teii.woff2?t=1648289780095') format('woff2'), url('//at.alicdn.com/t/font_3147059_ypw5b7teii.woff?t=1648289780095') format('woff'),
    url('//at.alicdn.com/t/font_3147059_ypw5b7teii.ttf?t=1648289780095') format('truetype');
}
.iconfont {
  font-family: 'iconfont' !important;
  font-size: 18px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -webkit-text-stroke-width: 0.2px;
  -moz-osx-font-smoothing: grayscale;
}
</style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用 Vue3 和 TypeScript 封装的多选框函数示例: ```typescript import { defineComponent, PropType } from 'vue' interface Option { label: string value: string | number } export default defineComponent({ name: 'MultiSelect', props: { options: { type: Array as PropType<Option[]>, required: true }, value: { type: Array as PropType<(string | number)[]>, default: () => [] } }, emits: ['update:value'], setup(props, { emit }) { const isSelected = (value: string | number) => { return props.value.includes(value) } const toggleOption = (value: string | number) => { const idx = props.value.indexOf(value) if (idx === -1) { emit('update:value', [...props.value, value]) } else { const newValue = [...props.value] newValue.splice(idx, 1) emit('update:value', newValue) } } return { isSelected, toggleOption } }, template: ` <div> <label v-for="option in options" :key="option.value"> <input type="checkbox" :value="option.value" v-model="isSelected(option.value)" @change="toggleOption(option.value)"> {{ option.label }} </label> </div> ` }) ``` 使用时可以传入一个选项数组和一个值数组,选项数组包含多个选项对象,每个选项对象包含 `label` 和 `value` 两个属性,值数组包含已选的选项的值。组件会根据选项数组和值数组自动渲染多个复选框,并根据值数组设置复选框的选中状态。当用户勾选或取消勾选某个复选框时,组件会触发 `update:value` 事件并传递更新后的值数组。通过监听该事件,可以在父组件中更新值数组并实现多选框的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值