vue 的ant design的Switch开关不支持二次确认,自定义封装

原因:切换switch会直接触发状态改变,二次弹窗无法使用

实现效果图:

一、使用步骤

1.封装SwitchButton组件

代码如下:新建一个SwitchButton.vue的文件,最好放在公共组件中,也是方便后续使用

<template>
  <div :class="['d-switch', { 'is-checked': checked }]">
    <input
      class="d-switch__input"
      ref="input"
      type="checkbox"
      :checked="checked"
      @change="handleInput"
      :true-value="props.trueValue"
      :false-value="props.falseValue"
    />
    <span :class="['d-switch__label', { 'is-checked': checked }]">
      {{ checked ? props.trueName : props.falseName }}
    </span>
    <span class="d-switch_action"></span>
  </div>
</template>

<script setup>
import { computed, ref, nextTick } from 'vue';

const props = defineProps({
  modelValue: {
    // 绑定值
    type: [Number, String, Boolean],
  },
  modelRecord: {
    // 绑定的json
    type: Object,
  },
  trueValue: {
    // switch 打开时的值
    type: [Number, String, Boolean],
    default: true,
  },
  falseValue: {
    // switch 关闭时的值
    type: [Number, String, Boolean],
    default: true,
  },
  trueName: {
    // switch 打开时显示的文字
    type: String,
    default: '开',
  },
  falseName: {
    // switch 关闭时显示的文字
    type: String,
    default: '关',
  },
});

const emits = defineEmits(['update:modelValue', 'change']);

const input = ref(null);

// 判断当前组件是否是打开状态
const checked = computed(() => {
  // 因为可以自定义打开和关闭的值 所以这里必须判断 v-model绑定的值 === 组件自定义打开的值
  return props.modelValue === props.trueValue;
});

//input事件 获取当前input事件
const handleInput = () => {
  nextTick(() => {
    const val = input.value.checked;
    emits('update:modelValue', val);
    emits('change', props.modelRecord);
  });
};
</script>

<style lang="less" scoped>
.d-switch {
  position: relative;
  height: 22px;
  transition: background 0.2s;
  width: 44px;
  background: #00000040;
  border-radius: 100px;
  display: inline-flex;
  align-items: center;
  vertical-align: middle;
  .d-switch__input {
    position: relative;
    z-index: 1;
    margin: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
  }
  .d-switch_action {
    position: absolute;
    transition: 0.2s;
    left: 2px;
    top: 2px;
    z-index: 0;
    height: 18px;
    width: 18px;
    background: #fff;
    border-radius: 50%;
  }
  &.is-checked {
    background-color: #1890ff;
    .d-switch_action {
      left: 100%;
      background: #fff;
      margin-left: -20px;
    }
  }
  .d-switch__label {
    position: absolute;
    margin: 0 7px 0 25px;
    color: #fff;
    font-size: 12px;
    transition: margin 0.2s;
    &.is-checked {
      margin: 0 25px 0 7px;
    }
  }
}
</style>

2.引入SwitchButton

import SwitchButton from '@/views/Common/SwitchButton';

3.html中SwitchButton使用

<SwitchButton
            :modelValue="record.status"
            :modelRecord="record"
            :trueValue="1"
            :falseValue="0"
            @change="change(record)"
          ></SwitchButton>

 siwtch中调用@change中的方法

const change = async (record) => {
  let str = record.status ? '关闭' : '启用';
  Modal.confirm({
    title: `确认${str}状态吗?`,
    icon: createVNode(ExclamationCircleOutlined),
    async onOk() {
      let params = {
        status: record.status ? 0 : 1,
        id: record.id,
      };
      let res = await setEditAccessInfo(params).catch(() => {
        message.error(`${str}失败`);
      });
      if (res) {
        record.status = record.status ? 0 : 1;
        message.success(`${str}成功`);
      }
    },
    onCancel() {},
  });
  return record;
};

总结

先二次确认提示,再改变状态

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该如何实现这个功能? 可以通过在 Select 组件内部添加一个 Modal 对话框来实现二次确认功能。具体实现方式是,在 Select 组件的 onChange 事件,首先判断当前选的值是否需要进行二次确认,如果是,则弹出 Modal 对话框让用户确认,否则直接更新 Select 的值。 以下是示例代码: ```vue <template> <div> <a-select v-model="selectedValue" @change="handleChange"> <a-select-option v-for="option in options" :key="option.value" :value="option.value">{{ option.label }}</a-select-option> </a-select> <a-modal v-if="showConfirm" @ok="handleConfirm" @cancel="handleCancel"> <span slot="title">确认</span> <p>你确定要选择 "{{ confirmValue }}" 吗?</p> </a-modal> </div> </template> <script> import { Select, Modal } from "ant-design-vue"; export default { components: { "a-select": Select, "a-select-option": Select.Option, "a-modal": Modal, }, data() { return { selectedValue: "", options: [ { label: "选项1", value: "1", needConfirm: true, // 需要进行二次确认 }, { label: "选项2", value: "2", }, ], showConfirm: false, confirmValue: "", }; }, methods: { handleChange(value) { const selectedOption = this.options.find((option) => option.value === value); if (!selectedOption.needConfirm) { this.selectedValue = value; } else { this.showConfirm = true; this.confirmValue = selectedOption.label; } }, handleConfirm() { this.selectedValue = this.confirmValue; this.showConfirm = false; }, handleCancel() { this.showConfirm = false; }, }, }; </script> ``` 在上述代码,需要进行二次确认的选项需要设置一个 `needConfirm` 属性为 `true`,在 `handleChange` 方法判断选的值是否需要进行二次确认,如果需要,则弹出 Modal 对话框;否则直接更新 Select 的值。在 Modal 对话框,用户可以点击“确认”或“取消”按钮,分别更新 Select 的值或关闭对话框。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值