Skip to content

Commit 3b1b661

Browse files
committed
新增弹窗宽度、位置、可全屏三个配置
1 parent 189826e commit 3b1b661

File tree

6 files changed

+120
-23
lines changed

6 files changed

+120
-23
lines changed

components/dialog-wxapp/dialog.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Github: https://github.com/simsir-lin
44
* Email: 15986907592@163.com
55
*/
6-
6+
77
Component({
88
properties: {
99
visible: {
@@ -42,27 +42,65 @@ Component({
4242
closeOnClickModal: {
4343
type: Boolean,
4444
value: true
45+
},
46+
fullscreen: {
47+
type: Boolean,
48+
value: false
49+
},
50+
width: {
51+
type: Number,
52+
value: 85
53+
},
54+
position: {
55+
type: String,
56+
value: 'center',
57+
observer: function (newVal) {
58+
this.setData({
59+
_posttion: this.checkPosition(newVal) ? newVal : 'center'
60+
})
61+
}
4562
}
4663
},
4764
data: {
65+
positions: ['center', 'top', 'bottom'],
66+
_posttion: 'center'
4867
},
4968
attached: function () {
69+
this.setData({
70+
_posttion: this.checkPosition(this.data.position) ? this.data.position : 'center'
71+
})
72+
if (!this.dataset.model) {
73+
console.warn("dialog-wxapp: dataset 'model' undefined")
74+
}
5075
},
5176
moved: function () {
5277
},
5378
detached: function () {
5479
},
5580

5681
methods: {
82+
checkPosition: function (val) {
83+
return this.data.positions.indexOf(val) >= 0;
84+
},
5785
touchstart: function () {
5886
if (this.data.closeOnClickModal) {
59-
this.triggerEvent('close');
87+
this.close();
88+
}
89+
},
90+
closedialog: function () {
91+
if (this.dataset.model) {
92+
let currentPage = getCurrentPages().pop();
93+
let data = {};
94+
data[this.dataset.model] = false;
95+
currentPage.setData(data);
6096
}
6197
},
6298
close: function () {
99+
this.closedialog();
63100
this.triggerEvent('close');
64101
},
65102
confirm: function () {
103+
this.closedialog();
66104
this.triggerEvent('confirm');
67105
}
68106
}

components/dialog-wxapp/dialog.wxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
<view class="dialog-wxapp" wx:if="{{visible}}">
88
<view class='dialog-wxapp-mask' style="background: rgba(0, 0, 0, {{opacity}});" catchtouchstart="touchstart"></view>
9-
<view class="dialog-wxapp-main">
10-
<view class="dialog-wxapp-container {{animation ? 'dialog-wxapp-animation':''}}">
9+
<view class="dialog-wxapp-main" style="height:{{fullscreen ? '100%':'auto'}};{{position == 'center' ? 'top: 50%; transform: translateY(-50%);' : position == 'top' ? 'top:0;' : 'bottom:0;'}}">
10+
<view class="dialog-wxapp-container {{animation ? 'dialog-wxapp-animation':''}}" style="width: {{fullscreen ? '100' : width}}%; height:{{fullscreen ? '100%':'auto'}};">
1111
<view class='dialog-wxapp-container__title' wx:if="{{title.length>0}}">
1212
<view>{{ title }}</view>
1313
<view>

components/dialog-wxapp/dialog.wxss

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,12 @@ view, input, text {
4040
}
4141

4242
.dialog-wxapp-main {
43-
position: fixed;
44-
top: 50%;
45-
transform: translateY(-50%);
43+
position: fixed;
4644
z-index: 10001;
4745
width: 100vw;
4846
}
4947

50-
.dialog-wxapp-container {
51-
width: 85vw;
48+
.dialog-wxapp-container {
5249
margin: 0 auto;
5350
background: #fff;
5451
z-index: 10001;

pages/index/index.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,41 @@ Page({
44
options: {
55
showclose: true,
66
showfooter: true,
7-
closeonclickmodal: true
7+
closeonclickmodal: true,
8+
fullscreen: false
89
},
910
title: 'dialog title',
10-
opacity: '0.4'
11+
opacity: '0.4',
12+
width: '85',
13+
position: 'center',
14+
positions: [{
15+
title: '居中',
16+
value: 'center'
17+
}, {
18+
title: '顶部',
19+
value: 'top'
20+
}, {
21+
title: '底部',
22+
value: 'bottom'
23+
}],
24+
positionIndex: 0
25+
},
26+
handlePickerChange: function (e) {
27+
this.setData({
28+
position: this.data.positions[e.detail.value].value,
29+
positionIndex: e.detail.value
30+
})
1131
},
1232
handleSliderChange: function (e) {
1333
this.setData({
1434
opacity: e.detail.value
1535
})
1636
},
37+
handleWidthInput: function (e) {
38+
this.setData({
39+
width: e.detail.value
40+
})
41+
},
1742
handleInput: function (e) {
1843
this.setData({
1944
title: e.detail.value
@@ -27,12 +52,13 @@ Page({
2752
},
2853
showDialog: function () {
2954
this.setData({
30-
dialogvisible: !this.data.dialogvisible
55+
dialogvisible: true
3156
})
3257
},
3358
handleClose: function () {
34-
this.setData({
35-
dialogvisible: false
59+
wx.showToast({
60+
title: 'close dialog',
61+
icon: 'none'
3662
})
3763
},
3864
handleOpen: function () {
@@ -42,8 +68,9 @@ Page({
4268
})
4369
},
4470
handleConfirm: function () {
45-
this.setData({
46-
dialogvisible: false
71+
wx.showToast({
72+
title: 'confirm',
73+
icon: 'none'
4774
})
4875
},
4976
onLoad: function (options) {

pages/index/index.wxml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
<view class="container">
2-
<dialog visible="{{dialogvisible}}" title="{{title}}" opacity="{{opacity}}" show-close="{{options.showclose}}" show-footer="{{options.showfooter}}" close-on-click-modal="{{options.closeonclickmodal}}" bindclose="handleClose" bindopen="handleOpen" bindconfirm="handleConfirm">
2+
<dialog data-model="dialogvisible" visible="{{dialogvisible}}" position="{{position}}" title="{{title}}" fullscreen="{{options.fullscreen}}" opacity="{{opacity}}" show-close="{{options.showclose}}" show-footer="{{options.showfooter}}" width="{{width}}"
3+
close-on-click-modal="{{options.closeonclickmodal}}" bindclose="handleClose" bindopen="handleOpen" bindconfirm="handleConfirm">
34
<view style='height:20vh;'>
45
这是插入的内容
56
</view>
67
</dialog>
7-
<view class='title'>
8-
<input value='{{title}}' bindinput='handleInput' placeholder='弹窗标题'></input>
8+
<view class='inputs'>
9+
<view>
10+
<text>弹窗标题:</text>
11+
<input value='{{title}}' bindinput='handleInput' placeholder='弹窗标题'></input>
12+
</view>
13+
<view>
14+
<text>弹窗宽度:</text>
15+
<input value='{{width}}' type='number' style='width:100rpx' bindinput='handleWidthInput' placeholder='弹窗宽度'></input>
16+
<text>%</text>
17+
</view>
918
</view>
1019
<view class='form'>
1120
<view class='form-group'>
@@ -20,16 +29,33 @@
2029
<switch checked="{{options.showfooter}}" bindchange='handleChange' data-model="showfooter" />
2130
</view>
2231
</view>
32+
<view class='form-group'>
33+
<view>是否全屏</view>
34+
<view>
35+
<switch checked="{{options.fullscreen}}" bindchange='handleChange' data-model="fullscreen" />
36+
</view>
37+
</view>
2338
<view class='form-group'>
2439
<view>是否可以点击modal关闭Dialog</view>
2540
<view>
2641
<switch checked="{{options.closeonclickmodal}}" bindchange='handleChange' data-model="closeonclickmodal" />
2742
</view>
2843
</view>
44+
<view class='form-group'>
45+
<view>弹窗位置</view>
46+
<view>
47+
<picker bindchange="handlePickerChange" value="{{positionIndex}}" range="{{positions}}" range-key="title">
48+
<view>
49+
{{ positions[positionIndex].title }}
50+
<text style='margin-left: 10rpx;'>></text>
51+
</view>
52+
</picker>
53+
</view>
54+
</view>
2955
<view>
3056
<view>遮罩层透明度</view>
3157
<view style='padding:30rpx 0;'>
32-
<slider bindchange="handleSliderChange" value="{{opacity}}" min="0.1" step="0.1" max="1"/>
58+
<slider bindchange="handleSliderChange" value="{{opacity}}" min="0.1" step="0.1" max="1" />
3359
</view>
3460
</view>
3561
<button bindtap='showDialog'>打开弹窗(Open Dialog)</button>

pages/index/index.wxss

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@
1717
font-size: 32rpx;
1818
}
1919

20-
.title {
20+
.inputs {
2121
height: 60vw;
2222
background: #eee;
2323
display: flex;
2424
justify-content: center;
2525
align-items: center;
26+
flex-direction: column;
2627
}
27-
.title input {
28+
29+
.inputs input {
2830
border-bottom: 1px solid #ccc;
29-
}
31+
}
32+
33+
.inputs>view {
34+
width: 70vw;
35+
display: flex;
36+
align-items: center;
37+
margin: 10rpx 0;
38+
}

0 commit comments

Comments
 (0)