uni-popup-dialog.vue 5.6 KB
Newer Older
1 2 3
<template>
	<view class="uni-popup-dialog">
		<view class="uni-dialog-title">
4
			<text class="uni-dialog-title-text" :class="['uni-popup__'+dialogType]">{{titleText}}</text>
5
		</view>
6 7 8 9 10 11 12 13
		<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>
				<input class="uni-dialog-input" v-model="val" type="text" :placeholder="placeholderText" :focus="focus" >
DCloud_JSON's avatar
DCloud_JSON 已提交
14
			</slot>
15 16
		</view>
		<view class="uni-dialog-button-group">
DCloud_JSON's avatar
DCloud_JSON 已提交
17
			<view class="uni-dialog-button" @click="closeDialog">
study夏羽's avatar
update  
study夏羽 已提交
18
				<text class="uni-dialog-button-text">{{closeText}}</text>
19 20
			</view>
			<view class="uni-dialog-button uni-border-left" @click="onOk">
21
				<text class="uni-dialog-button-text uni-button-color">{{okText}}</text>
22 23 24 25 26 27
			</view>
		</view>

	</view>
</template>

28
<script>
DCloud_JSON's avatar
DCloud_JSON 已提交
29
	import popup from '../uni-popup/popup.js'
30 31 32 33
	import {
	initVueI18n
	} from '@dcloudio/uni-i18n'
	import messages from '../uni-popup/i18n/index.js'
study夏羽's avatar
update  
study夏羽 已提交
34
	const {	t } = initVueI18n(messages)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	/**
	 * PopUp 弹出层-对话框样式
	 * @description 弹出层-对话框样式
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=329
	 * @property {String} value input 模式下的默认值
	 * @property {String} placeholder input 模式下输入提示
	 * @property {String} type = [success|warning|info|error] 主题样式
	 *  @value success 成功
	 * 	@value warning 提示
	 * 	@value info 消息
	 * 	@value error 错误
	 * @property {String} mode = [base|input] 模式、
	 * 	@value base 基础对话框
	 * 	@value input 可输入对话框
	 * @property {String} content 对话框内容
	 * @property {Boolean} beforeClose 是否拦截取消事件
	 * @event {Function} confirm 点击确认按钮触发
	 * @event {Function} close 点击取消按钮触发
	 */

	export default {
56
		name: "uniPopupDialog",
DCloud_JSON's avatar
DCloud_JSON 已提交
57
		mixins: [popup],
58
		emits:['confirm','close'],
59 60 61 62 63 64 65
		props: {
			value: {
				type: [String, Number],
				default: ''
			},
			placeholder: {
				type: [String, Number],
66
				default: ''
67 68 69 70 71 72 73 74 75 76 77
			},
			type: {
				type: String,
				default: 'error'
			},
			mode: {
				type: String,
				default: 'base'
			},
			title: {
				type: String,
78
				default: ''
79 80 81 82 83 84 85 86
			},
			content: {
				type: String,
				default: ''
			},
			beforeClose: {
				type: Boolean,
				default: false
87 88 89 90 91 92 93 94
			},
			cancelText:{
				type: String,
				default: ''
			},
			confirmText:{
				type: String,
				default: ''
95 96 97 98 99 100 101 102 103
			}
		},
		data() {
			return {
				dialogType: 'error',
				focus: false,
				val: ""
			}
		},
104 105
		computed: {
			okText() {
study夏羽's avatar
update  
study夏羽 已提交
106
				return this.confirmText || t("uni-popup.ok")
107
			},
study夏羽's avatar
update  
study夏羽 已提交
108 109
			closeText() {
				return this.cancelText || t("uni-popup.cancel")
110 111 112 113 114 115 116 117
			},
			placeholderText() {
				return this.placeholder || t("uni-popup.placeholder")
			},
			titleText() {
				return this.title || t("uni-popup.title")
			}
		},
118 119 120 121 122 123 124 125 126 127 128 129 130
		watch: {
			type(val) {
				this.dialogType = val
			},
			mode(val) {
				if (val === 'input') {
					this.dialogType = 'info'
				}
			},
			value(val) {
				this.val = val
			}
		},
131
		created() {
132
			// 对话框遮罩不可点击
133
			this.popup.disableMask()
DCloud_JSON's avatar
DCloud_JSON 已提交
134
			// this.popup.closeMask()
135 136 137 138 139 140 141 142 143 144 145 146 147 148
			if (this.mode === 'input') {
				this.dialogType = 'info'
				this.val = this.value
			} else {
				this.dialogType = this.type
			}
		},
		mounted() {
			this.focus = true
		},
		methods: {
			/**
			 * 点击确认按钮
			 */
149 150 151 152 153 154 155
			onOk() {
				if (this.mode === 'input'){
					this.$emit('confirm', this.val)
				}else{
					this.$emit('confirm')
				}
				if(this.beforeClose) return
DCloud_JSON's avatar
DCloud_JSON 已提交
156
				this.popup.close()
157 158 159 160
			},
			/**
			 * 点击取消按钮
			 */
161 162
			closeDialog() {
				this.$emit('close')
DCloud_JSON's avatar
DCloud_JSON 已提交
163
				if(this.beforeClose) return
164
				this.popup.close()
165 166 167
			},
			close(){
				this.popup.close()
168 169 170 171 172
			}
		}
	}
</script>

study夏羽's avatar
update  
study夏羽 已提交
173
<style lang="scss" >
174 175 176 177 178
	$uni-primary: #007aff !default;
	$uni-success: #4cd964 !default;
	$uni-warning: #f0ad4e !default;
	$uni-error: #dd524d !default;
	
179 180
	.uni-popup-dialog {
		width: 300px;
181
		border-radius: 11px;
182 183 184 185 186 187 188 189 190
		background-color: #fff;
	}

	.uni-dialog-title {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
191
		padding-top: 25px;
192 193 194 195 196 197 198 199 200 201 202 203 204 205
	}

	.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;
206
		padding: 20px;
207 208 209 210
	}

	.uni-dialog-content-text {
		font-size: 14px;
211
		color: #6C6C6C;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	}

	.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 {
study夏羽's avatar
update  
study夏羽 已提交
243
		font-size: 16px;
244
		color: #333;
245 246 247
	}

	.uni-button-color {
248
		color: $uni-primary;
249 250 251 252
	}

	.uni-dialog-input {
		flex: 1;
253 254 255 256 257
		font-size: 14px;
		border: 1px #eee solid;
		height: 40px;
		padding: 0 10px;
		border-radius: 5px;
DCloud_JSON's avatar
DCloud_JSON 已提交
258
		color: #555;
259 260 261
	}

	.uni-popup__success {
262
		color: $uni-success;
263 264 265
	}

	.uni-popup__warn {
266
		color: $uni-warning;
267 268 269
	}

	.uni-popup__error {
270
		color: $uni-error;
271 272 273 274 275
	}

	.uni-popup__info {
		color: #909399;
	}
276
</style>