Skip to content
This repository was archived by the owner on Oct 27, 2021. It is now read-only.

Commit 43c3757

Browse files
author
pengshanglong
committed
feat: add Message
1 parent 1dcd22b commit 43c3757

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/components/message/index.jsx

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import Taro from '@tarojs/taro'
2+
import classNames from 'classnames'
3+
4+
export default {
5+
name: 'Message',
6+
props: {
7+
customStyle: {
8+
type: [Object, String],
9+
default: '',
10+
},
11+
className: {
12+
type: [Array, String],
13+
default: '',
14+
},
15+
},
16+
data() {
17+
return {
18+
timer: null,
19+
state: {
20+
_isOpened: false,
21+
_message: '',
22+
_type: 'info',
23+
_duration: 3000,
24+
},
25+
}
26+
},
27+
mounted() {
28+
this.bindMessageListener()
29+
},
30+
beforeDestroy() {
31+
Taro.eventCenter.off('atMessage')
32+
},
33+
methods: {
34+
setState(newState, fn) {
35+
const ks = Object.keys(newState)
36+
if (Array.isArray(ks)) {
37+
ks.forEach((k) => {
38+
if (k in this.state) {
39+
this.state[k] = newState[k]
40+
}
41+
})
42+
}
43+
typeof fn === 'function' && fn.call(this)
44+
},
45+
bindMessageListener() {
46+
Taro.eventCenter.on('atMessage', (options = {}) => {
47+
const { message, type, duration } = options
48+
const newState = {
49+
_isOpened: true,
50+
_message: message,
51+
_type: type,
52+
_duration: duration || this.state._duration,
53+
}
54+
this.setState(newState, () => {
55+
clearTimeout(this.timer)
56+
this.timer = setTimeout(() => {
57+
this.setState({
58+
_isOpened: false,
59+
})
60+
}, this.state._duration)
61+
})
62+
})
63+
// 绑定函数
64+
Taro.atMessage = Taro.eventCenter.trigger.bind(Taro.eventCenter, 'atMessage')
65+
},
66+
},
67+
render() {
68+
const { className, customStyle } = this
69+
const { _message, _isOpened, _type } = this.state
70+
const rootCls = classNames(
71+
{
72+
'at-message': true,
73+
'at-message--show': _isOpened,
74+
'at-message--hidden': !_isOpened,
75+
},
76+
`at-message--${_type}`,
77+
className
78+
)
79+
80+
return (
81+
<view class={rootCls} style={customStyle}>
82+
{_message}
83+
</view>
84+
)
85+
},
86+
}

src/pages/index/index.vue

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
<view
33
class="index"
44
>
5+
<Message />
6+
<button @tap="showMessage">
7+
show Message
8+
</button>
9+
<button @tap="showMessage('success')">
10+
show success Message
11+
</button>
512
<SwipeAction
613
:options="SAOptions"
714
>
@@ -134,6 +141,7 @@
134141
</template>
135142

136143
<script>
144+
import Taro from '@tarojs/taro'
137145
// import Button from '../../components/button/index.jsx'
138146
import Badge from '../../components/badge/index'
139147
import Icon from '../../components/icon/index'
@@ -155,6 +163,7 @@ import ModalContent from '../../components/modal/content/index.jsx'
155163
import ModalAction from '../../components/modal/action/index.jsx'
156164
import Toast from '../../components/toast/index.jsx'
157165
import SwipeAction from '../../components/swipe-action/index.jsx'
166+
import Message from '../../components/message/index.jsx'
158167
159168
export default {
160169
name: 'Index',
@@ -180,6 +189,7 @@ export default {
180189
ModalAction,
181190
Toast,
182191
SwipeAction,
192+
Message,
183193
},
184194
data() {
185195
return {
@@ -210,6 +220,12 @@ export default {
210220
},
211221
onClick(e) {
212222
console.log(e)
223+
},
224+
showMessage(type) {
225+
Taro.atMessage({
226+
'message': '消息通知',
227+
'type': type,
228+
})
213229
}
214230
},
215231
}

0 commit comments

Comments
 (0)