vue组件封装:tool-tip &popconfirm

前言

往期基于vue-popper封装了sss-popper组件,用来作为popper组件的基础,现在尝试两个简单的popper组件,分别是tool tip 提示框、popconfirm 气泡确认框。

由于许多props都是直接传入sss-popper组件内部的,所以不过多介绍

展示

tool-tip + popconfirm

正文

tool tip

提示框基本就是popper元素里面放一段文字,因此会多一个prop:title

不设置事件和提供给外部调用的方法

html

<template>

    <sss-popper
        :disabled="disabled"
        :trigger="trigger"
        :placement="placement"
        :dark="dark"
        :maxwidth="maxwidth"
        :transition="transition"
        :gpu-acceleration="gpuAcceleration"
        :offset="offset"
    >

        <span ref="popper" slot="popper">{{ title }}</span>
        <slot slot="reference"></slot>
    </sss-popper>


</template>

js

<script>
export default {
    name: "sss-tool-tip",
    props: {
        placement: {
            type: String,
            default: "top"
        },
        dark: {
            type: Boolean,
            default: false,
        },
        trigger: {
            type: String,
            default: "hover",
            validator(value) {
                return ['hover', 'clickToOpen'].indexOf(value) !== -1;
            }
        },
        disabled: {
            type: Boolean,
            default: false
        },
        title: {
            type: String,
            default: ""
        },
        maxwidth: {
            type: String,
            default: "2000px"
        },
        transition: {
            type: String,
            default: "fade"
        },
        gpuAcceleration: {
            type: Boolean,
            default: true
        },
        offset: {
            type: Number,
            default: 10
        }

    },


}
</script>

css

甚至没有css👻

popconfirm

作为气泡确认框,自然应该具有header content footer

  • hedaer: 展示图标和标题,图标往往提示用户这个一个什么类型的确认框
  • content: 展示用户需要确认的内容, 此组件将内容作为标题
  • footer: 展示取消确认按钮

使用

attributes
属性名描述类型可选值默认值
type图标类型stringsuccess / info / warn / errorinfo
title确认框的标题string
cancelBtnType取消按钮的类型stringnormal
cancelBtnText取消按钮的文本string取消
confirmBtnType确认按钮类型stringmain
confirmBtnText确认按钮文本string确认
events
事件名描述回调参数
cancel点击取消按钮之后的回调
confirm点击确认按钮之后的回调

html

<template>

    <sss-popper
        :disabled="disabled"
        :force-show="vis"
        :trigger="trigger"
        :placement="placement"
        :offset="offset"
        :gpu-acceleration="gpuAcceleration"
        :max-width="maxWidth"
        :minwidth="minWidth"

    >
        <slot slot="reference"></slot>

        <div class="class-popconfirm-popper" slot="popper">
            <!--            标题-->
            <h3 class="sss-popconfirm-title">
                <span class="iconfont" ref="icon"></span>
                {{ title }}
            </h3>

            <!--            按钮-->
            <div class="sss-popconfirm-btn-list">
                <sss-button class="sss-confirm-cancel-btn" style="font-size: 7px"
                            :type="canclBtnType"
                            @click.stop="__cancel()"
                >{{ cancelBtnText }}
                </sss-button>
                <sss-button class="sss-confirm-confirm-btn" style="font-size: 7px"
                            :type="confirmBtnType"
                            @click.stop="__confirm()"

                >{{ confirmBtnText }}
                </sss-button>
            </div>

        </div>


    </sss-popper>

</template>

js

<script>
export default {
    name: "sss-popconfirm",
    props: {
        placement: {
            type: String,
            default: "bottom"
        },
        trigger: {
            type: String,
            default: "clickToOpen",

        },
        offset: {
            type: Number,
            default: 10
        },
        gpuAcceleration: {
            type: Boolean,
            default: true
        },
        disabled: {
            type: Boolean,
            default: false
        },
        type: {
            type: String,
            default: "info",
            validator(value) {
                return ['success', 'error', 'warn', 'info'].indexOf(value) !== -1;
            }
        },
        title: {
            type: String,
        },
        canclBtnType: {
            type: String,
            default: "normal"
        },
        cancelBtnText: {
            type: String,
            default: "取消"
        },
        confirmBtnType: {
            type: String,
            default: "main",
        },
        confirmBtnText: {
            type: String,
            default: "确认"
        },
        maxWidth: {
            type: String,
            default: "2000px"
        },
        minWidth: {
            type: String,
            default: '250px'
        }


    },
    data() {
        return {
            vis: false
        }
    },
    methods: {
        __addIcon() {
            if (this.type === 'success') {
                this.$refs.icon.classList.add('icon-chenggong');
                this.$refs.icon.style.color = '#db5b6c';

            } else if (this.type === 'error') {
                this.$refs.icon.classList.add('icon-error-filling');
                this.$refs.icon.style.color = '#e1122a';

            } else if (this.type === 'warn') {
                this.$refs.icon.classList.add('icon-gantanhao1');
                this.$refs.icon.style.color = '#E6A23C';

            } else if (this.type === 'info') {
                this.$refs.icon.classList.add('icon-yiwen');
                this.$refs.icon.style.color = '#1ba784';

            }
        },
        __cancel() {
            this.vis = true;
            this.$nextTick(() => {
                this.vis = false
            })

            this.$emit("cancel");
        },
        __confirm() {
            this.vis = true;
            this.$nextTick(() => {
                this.vis = false
            })

            this.$emit("confirm")
        }

    },
    mounted() {
        this.__addIcon();
    }
}
</script>

css

<style lang="less">
@import "@/assets/style/sss-var.less";

.class-popconfirm-popper {
    & > .sss-popconfirm-title {
        margin-top: 10px;
        margin-bottom: 20px;

        & > .iconfont {
            background: white;
            border-radius: 50%;
            overflow: hidden;


        }
    }

    color: @color-black1;
    display: flex;
    flex-flow: column nowrap;
    align-items: start;

    .sss-popconfirm-btn-list {
        display: flex;
        width: 100%;
        justify-content: end;

    }
}

</style>

写在最后

感谢看到最后😃

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值