PopupWrapper.vue
3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<template>
<nut-popup
:visible="visibleModel"
:position="position"
:style="popupStyle"
:closeable="closeable"
:close-on-click-overlay="closeOnClickOverlay"
:round="round"
:z-index="zIndex"
@close="handleClose"
@open="handleOpen"
@opened="handleOpened"
@closed="handleClosed"
>
<!-- 自定义头部 -->
<view v-if="showHeader" class="popup-header">
<view class="flex items-center justify-between p-4 border-b border-gray-200">
<view class="text-lg font-medium">{{ title }}</view>
<view v-if="showCloseButton" @tap="handleClose" class="w-8 h-8 flex items-center justify-center"></view>
</view>
</view>
<!-- 内容区域 -->
<view class="popup-content" :style="contentStyle">
<slot></slot>
</view>
</nut-popup>
</template>
<script setup>
import { ref, computed, defineProps, defineEmits } from 'vue';
import { Close } from '@nutui/icons-vue-taro';
// 定义props
const props = defineProps({
// 控制弹窗显示/隐藏
visible: {
type: Boolean,
default: false
},
// 弹窗位置
position: {
type: String,
default: 'center',
validator: (value) => ['center', 'top', 'bottom', 'left', 'right'].includes(value)
},
// 弹窗标题
title: {
type: String,
default: ''
},
// 是否显示头部
showHeader: {
type: Boolean,
default: true
},
// 是否显示关闭按钮
showCloseButton: {
type: Boolean,
default: true
},
// 是否可关闭
closeable: {
type: Boolean,
default: true
},
// 点击遮罩是否关闭
closeOnClickOverlay: {
type: Boolean,
default: false
},
// 是否圆角
round: {
type: Boolean,
default: false
},
// 层级
zIndex: {
type: Number,
default: 2000
},
// 自定义宽度
width: {
type: String,
default: '100%'
},
// 自定义高度
height: {
type: String,
default: '100%'
},
// 是否全屏
fullscreen: {
type: Boolean,
default: false
}
});
// 定义emits
const emit = defineEmits(['update:visible', 'close', 'open', 'opened', 'closed']);
// 计算属性处理双向绑定
const visibleModel = computed({
get() {
return props.visible;
},
set(value) {
emit('update:visible', value);
}
});
// 计算弹窗样式
const popupStyle = computed(() => {
const style = {};
if (props.fullscreen) {
style.width = '100%';
style.height = '100%';
} else {
if (props.width) style.width = props.width;
if (props.height) style.height = props.height;
}
return style;
});
// 计算内容区域样式
const contentStyle = computed(() => {
const style = {};
if (props.showHeader) {
style.height = 'calc(100% - 60px)';
} else {
style.height = '100%';
}
return style;
});
/**
* 处理关闭事件
*/
const handleClose = () => {
emit('update:visible', false);
emit('close');
};
/**
* 处理打开事件
*/
const handleOpen = () => {
emit('open');
};
/**
* 处理已打开事件
*/
const handleOpened = () => {
emit('opened');
};
/**
* 处理已关闭事件
*/
const handleClosed = () => {
emit('closed');
};
</script>
<style lang="less" scoped>
.popup-header {
background-color: #fff;
border-bottom: 1px solid #e5e7eb;
}
.popup-content {
background-color: #f8f9fa;
overflow-y: auto;
}
</style>