shop-applet/uni_modules/uni-popup/components/uni-popup-dialog/uni-popup-dialog.vue

314 lines
6.0 KiB
Vue
Raw Normal View History

2023-09-20 18:16:59 +08:00
<template>
<view class="uni-popup-dialog">
<view class="uni-dialog-title">
<text class="uni-dialog-title-text" :class="['uni-popup__'+dialogType]">{{titleText}}</text>
</view>
<view v-if="mode === 'base'" class="uni-dialog-content">
<slot>
<text class="uni-dialog-content-text">{{content}}</text>
</slot>
</view>
<view v-else class="uni-dialog-content">
<slot>
2024-03-22 15:49:25 +08:00
<input class="uni-dialog-input" :maxlength="maxlength" v-model="val" :type="inputType"
:placeholder="placeholderText" :focus="focus">
2023-09-20 18:16:59 +08:00
</slot>
</view>
<view class="uni-dialog-button-group">
2024-03-22 15:49:25 +08:00
<view class="uni-dialog-button" v-if="showClose" @click="closeDialog">
<text class="uni-dialog-button-text">{{closeText}}</text>
2023-09-20 18:16:59 +08:00
</view>
<view class="uni-dialog-button uni-border-left" @click="onOk">
<text class="uni-dialog-button-text uni-button-color">{{okText}}</text>
</view>
</view>
</view>
</template>
<script>
import popup from '../uni-popup/popup.js'
import {
2024-03-22 15:49:25 +08:00
initVueI18n
2023-09-20 18:16:59 +08:00
} from '@dcloudio/uni-i18n'
import messages from '../uni-popup/i18n/index.js'
2024-03-22 15:49:25 +08:00
const {
t
} = initVueI18n(messages)
2023-09-20 18:16:59 +08:00
/**
* PopUp 弹出层-对话框样式
* @description 弹出层-对话框样式
* @tutorial https://ext.dcloud.net.cn/plugin?id=329
* @property {String} value input 模式下的默认值
* @property {String} placeholder input 模式下输入提示
2024-03-22 15:49:25 +08:00
* @property {Boolean} focus input模式下是否自动聚焦默认为true
2023-09-20 18:16:59 +08:00
* @property {String} type = [success|warning|info|error] 主题样式
* @value success 成功
* @value warning 提示
* @value info 消息
* @value error 错误
* @property {String} mode = [base|input] 模式
* @value base 基础对话框
* @value input 可输入对话框
2024-03-22 15:49:25 +08:00
* @showClose {Boolean} 是否显示关闭按钮
2023-09-20 18:16:59 +08:00
* @property {String} content 对话框内容
* @property {Boolean} beforeClose 是否拦截取消事件
2024-03-22 15:49:25 +08:00
* @property {Number} maxlength 输入
2023-09-20 18:16:59 +08:00
* @event {Function} confirm 点击确认按钮触发
* @event {Function} close 点击取消按钮触发
*/
export default {
name: "uniPopupDialog",
mixins: [popup],
2024-03-22 15:49:25 +08:00
emits: ['confirm', 'close', 'update:modelValue', 'input'],
2023-09-20 18:16:59 +08:00
props: {
2024-03-22 15:49:25 +08:00
inputType: {
type: String,
default: 'text'
},
showClose: {
type: Boolean,
default: true
},
// #ifdef VUE2
2023-09-20 18:16:59 +08:00
value: {
type: [String, Number],
default: ''
},
2024-03-22 15:49:25 +08:00
// #endif
// #ifdef VUE3
modelValue: {
type: [Number, String],
default: ''
},
// #endif
2023-09-20 18:16:59 +08:00
placeholder: {
type: [String, Number],
default: ''
},
type: {
type: String,
default: 'error'
},
mode: {
type: String,
default: 'base'
},
title: {
type: String,
default: ''
},
content: {
type: String,
default: ''
},
beforeClose: {
type: Boolean,
default: false
2024-03-22 15:49:25 +08:00
},
cancelText: {
type: String,
default: ''
},
confirmText: {
type: String,
default: ''
},
maxlength: {
type: Number,
default: -1,
},
focus: {
type: Boolean,
default: true,
2023-09-20 18:16:59 +08:00
}
},
data() {
return {
dialogType: 'error',
val: ""
}
},
computed: {
okText() {
2024-03-22 15:49:25 +08:00
return this.confirmText || t("uni-popup.ok")
2023-09-20 18:16:59 +08:00
},
2024-03-22 15:49:25 +08:00
closeText() {
return this.cancelText || t("uni-popup.cancel")
2023-09-20 18:16:59 +08:00
},
placeholderText() {
return this.placeholder || t("uni-popup.placeholder")
},
titleText() {
return this.title || t("uni-popup.title")
}
},
watch: {
type(val) {
this.dialogType = val
},
mode(val) {
if (val === 'input') {
this.dialogType = 'info'
}
},
value(val) {
2024-03-22 15:49:25 +08:00
if (this.maxlength != -1 && this.mode === 'input') {
this.val = val.slice(0, this.maxlength);
} else {
this.val = val
}
},
val(val) {
// #ifdef VUE2
// TODO 兼容 vue2
this.$emit('input', val);
// #endif
// #ifdef VUE3
// TODO 兼容 vue3
this.$emit('update:modelValue', val);
// #endif
2023-09-20 18:16:59 +08:00
}
},
created() {
// 对话框遮罩不可点击
this.popup.disableMask()
// this.popup.closeMask()
if (this.mode === 'input') {
this.dialogType = 'info'
this.val = this.value
} else {
this.dialogType = this.type
}
},
methods: {
/**
* 点击确认按钮
*/
onOk() {
2024-03-22 15:49:25 +08:00
if (this.mode === 'input') {
2023-09-20 18:16:59 +08:00
this.$emit('confirm', this.val)
2024-03-22 15:49:25 +08:00
} else {
2023-09-20 18:16:59 +08:00
this.$emit('confirm')
}
2024-03-22 15:49:25 +08:00
if (this.beforeClose) return
2023-09-20 18:16:59 +08:00
this.popup.close()
},
/**
* 点击取消按钮
*/
closeDialog() {
this.$emit('close')
2024-03-22 15:49:25 +08:00
if (this.beforeClose) return
2023-09-20 18:16:59 +08:00
this.popup.close()
},
2024-03-22 15:49:25 +08:00
close() {
2023-09-20 18:16:59 +08:00
this.popup.close()
}
}
}
</script>
2024-03-22 15:49:25 +08:00
<style lang="scss">
2023-09-20 18:16:59 +08:00
.uni-popup-dialog {
width: 300px;
2024-03-22 15:49:25 +08:00
border-radius: 11px;
2023-09-20 18:16:59 +08:00
background-color: #fff;
}
.uni-dialog-title {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
justify-content: center;
2024-03-22 15:49:25 +08:00
padding-top: 25px;
2023-09-20 18:16:59 +08:00
}
.uni-dialog-title-text {
font-size: 16px;
font-weight: 500;
}
.uni-dialog-content {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
justify-content: center;
align-items: center;
2024-03-22 15:49:25 +08:00
padding: 20px;
2023-09-20 18:16:59 +08:00
}
.uni-dialog-content-text {
font-size: 14px;
2024-03-22 15:49:25 +08:00
color: #6C6C6C;
2023-09-20 18:16:59 +08:00
}
.uni-dialog-button-group {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
border-top-color: #f5f5f5;
border-top-style: solid;
border-top-width: 1px;
}
.uni-dialog-button {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex: 1;
flex-direction: row;
justify-content: center;
align-items: center;
height: 45px;
}
.uni-border-left {
border-left-color: #f0f0f0;
border-left-style: solid;
border-left-width: 1px;
}
.uni-dialog-button-text {
2024-03-22 15:49:25 +08:00
font-size: 16px;
color: #333;
2023-09-20 18:16:59 +08:00
}
.uni-button-color {
color: #007aff;
}
.uni-dialog-input {
flex: 1;
font-size: 14px;
border: 1px #eee solid;
height: 40px;
padding: 0 10px;
border-radius: 5px;
color: #555;
}
.uni-popup__success {
color: #4cd964;
}
.uni-popup__warn {
color: #f0ad4e;
}
.uni-popup__error {
color: #dd524d;
}
.uni-popup__info {
color: #909399;
}
</style>